Skip to content

Commit

Permalink
New: Add recommended config (fixes #151) (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmills authored Aug 2, 2020
1 parent 0311640 commit eb66833
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ npm install --save-dev eslint eslint-plugin-markdown@next

### Configuring

Extending the `plugin:markdown/recommended` config will enable the Markdown processor on all `.md` files:

```js
// .eslintrc.js
module.exports = {
extends: "plugin:markdown/recommended"
};
```

#### Advanced Configuration

Add the plugin to your `.eslintrc` and use the `processor` option in an `overrides` entry to enable the plugin's `markdown/markdown` processor on Markdown files.
Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path.
The virtual filename's extension will match the fenced code block's syntax tag, so for example, <code>```js</code> code blocks in <code>README.md</code> would match <code>README.md/*.js</code>.
Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@

"use strict";

// https://github.com/mysticatea/eslint-plugin-node/issues/193
// eslint-disable-next-line node/no-unpublished-require
module.exports = require("./lib");
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
const processor = require("./processor");

module.exports = {
configs: {
recommended: {
plugins: ["markdown"],
overrides: [
{
files: ["*.md"],
processor: "markdown/markdown"
}
]
}
},
processors: {
markdown: processor
}
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/recommended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"root": true,
"extends": "plugin:markdown/recommended",
"rules": {
"no-console": "error"
}
}
62 changes: 58 additions & 4 deletions tests/lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,82 @@
"use strict";

const assert = require("chai").assert;
const execSync = require("child_process").execSync;
const CLIEngine = require("eslint").CLIEngine;
const path = require("path");
const plugin = require("../..");

/**
* Helper function which creates CLIEngine instance with enabled/disabled autofix feature.
* @param {string} fixtureConfigName ESLint JSON config fixture filename.
* @param {boolean} [isAutofixEnabled=false] Whether to enable autofix feature.
* @returns {CLIEngine} CLIEngine instance to execute in tests.
*/
function initCLI(isAutofixEnabled) {
function initCLI(fixtureConfigName, isAutofixEnabled) {
const fix = isAutofixEnabled || false;
const cli = new CLIEngine({
fix,
ignore: false,
useEslintrc: false,
configFile: path.resolve(__dirname, "../fixtures/eslintrc.json")
configFile: path.resolve(__dirname, "../fixtures/", fixtureConfigName)
});

cli.addPlugin("markdown", plugin);
return cli;
}

describe("recommended config", () => {

let cli;
const shortText = [
"```js",
"console.log(42);",
"```"
].join("\n");

before(function() {
try {

// The tests for the recommended config will have ESLint import
// the plugin, so we need to make sure it's resolvable and link it
// if not.
// eslint-disable-next-line node/no-extraneous-require
require.resolve("eslint-plugin-markdown");
} catch (error) {
if (error.code === "MODULE_NOT_FOUND") {

// The npm link step can take longer than Mocha's default 2s
// timeout, so give it more time. Mocha's API for customizing
// hook-level timeouts uses `this`, so disable the rule.
// https://mochajs.org/#hook-level
// eslint-disable-next-line no-invalid-this
this.timeout(9999);

execSync("npm link && npm link eslint-plugin-markdown");
} else {
throw error;
}
}

cli = initCLI("recommended.json");
});

it("should include the plugin", () => {
const config = cli.getConfigForFile("test.md");

assert.include(config.plugins, "markdown");
});

it("overrides configure processor to parse .md file code blocks", () => {
const report = cli.executeOnText(shortText, "test.md");

assert.strictEqual(report.results.length, 1);
assert.strictEqual(report.results[0].messages.length, 1);
assert.strictEqual(report.results[0].messages[0].ruleId, "no-console");
});

});

describe("plugin", () => {

let cli;
Expand All @@ -38,7 +92,7 @@ describe("plugin", () => {
].join("\n");

before(() => {
cli = initCLI();
cli = initCLI("eslintrc.json");
});

it("should run on .md files", () => {
Expand Down Expand Up @@ -209,7 +263,7 @@ describe("plugin", () => {
describe("should fix code", () => {

before(() => {
cli = initCLI(true);
cli = initCLI("eslintrc.json", true);
});

it("in the simplest case", () => {
Expand Down

0 comments on commit eb66833

Please sign in to comment.