Skip to content

Commit

Permalink
Breaking: Implement new processor API (fixes #138) (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
btmills committed Mar 15, 2020
1 parent 7eeafb8 commit 0d4dbe8
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 82 deletions.
25 changes: 14 additions & 11 deletions README.md
Expand Up @@ -17,23 +17,29 @@ Install the plugin:
npm install --save-dev eslint eslint-plugin-markdown
```

Add it to your `.eslintrc`:
Add it to your `.eslintrc` and enable the processor on Markdown files:

```json
{
"plugins": [
"markdown"
],
"overrides": [
{
"files": ["*.md"],
"processor": "markdown/markdown"
}
]
}
```

Run ESLint on `.md` files:

```sh
eslint --ext md .
eslint --ext js,md .
```

It will lint `js`, `javascript`, `jsx`, or `node` [fenced code blocks](https://help.github.com/articles/github-flavored-markdown/#fenced-code-blocks) in your Markdown documents:
It will lint [fenced code blocks](https://help.github.com/articles/github-flavored-markdown/#fenced-code-blocks) in your Markdown documents:

````markdown
```js
Expand All @@ -42,7 +48,9 @@ var answer = 6 * 7;
console.log(answer);
```

```JavaScript
Here is some regular Markdown text that will be ignored.

```js
// This also gets linted

/* eslint quotes: [2, "double"] */
Expand All @@ -54,17 +62,12 @@ hello();
```

```jsx
// This gets linted too
// This gets linted too if you enable --ext jsx
var div = <div className="jsx"></div>;
```

```node
// And this
console.log(process.version);
```
````

Blocks that don't specify either `js`, `javascript`, `jsx`, or `node` syntax are ignored:
Blocks that don't specify a syntax are ignored:

````markdown
```
Expand Down
5 changes: 1 addition & 4 deletions lib/index.js
Expand Up @@ -9,9 +9,6 @@ const processor = require("./processor");

module.exports = {
processors: {
".markdown": processor,
".mdown": processor,
".mkdn": processor,
".md": processor
markdown: processor
}
};
16 changes: 9 additions & 7 deletions lib/processor.js
Expand Up @@ -8,7 +8,6 @@
const unified = require("unified");
const remarkParse = require("remark-parse");

const SUPPORTED_SYNTAXES = ["js", "javascript", "node", "jsx"];
const UNSATISFIABLE_RULES = [
"eol-last", // The Markdown parser strips trailing newlines in code fences
"unicode-bom" // Code blocks will begin in the middle of Markdown files
Expand Down Expand Up @@ -222,7 +221,7 @@ function preprocess(text) {
code(node, parent) {
const comments = [];

if (node.lang && SUPPORTED_SYNTAXES.indexOf(node.lang.split(" ")[0].toLowerCase()) >= 0) {
if (node.lang) {
let index = parent.children.indexOf(node) - 1;
let previousNode = parent.children[index];

Expand Down Expand Up @@ -252,11 +251,14 @@ function preprocess(text) {
}
});

return blocks.map(block => [
...block.comments,
block.value,
""
].join("\n"));
return blocks.map((block, index) => ({
filename: `${index}.${block.lang}`,
text: [
...block.comments,
block.value,
""
].join("\n")
}));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/fixtures/eslintrc.json
@@ -0,0 +1,20 @@
{
"root": true,
"env": {
"browser": true
},
"plugins": ["markdown"],
"overrides": [
{
"files": ["*.md", "*.mkdn", "*.mdown", "*.markdown", "*.custom"],
"processor": "markdown/markdown"
}
],
"rules": {
"eol-last": "error",
"no-console": "error",
"no-undef": "error",
"quotes": "error",
"spaced-comment": "error"
}
}
6 changes: 3 additions & 3 deletions tests/fixtures/long.md
Expand Up @@ -10,7 +10,7 @@ This is some code:
console.log(42);
```

```JavaScript
```js
// Comment
function foo() {
console.log("Hello");
Expand All @@ -20,7 +20,7 @@ function foo() {
<!-- eslint-env node -->
<!-- eslint-disable eol-last, quotes -->

```node
```js
console.log(process.version);
```

Expand All @@ -34,7 +34,7 @@ How about some JSX?
-->
<!--eslint-disable no-console-->

```jsx
```js
console.log("Error!");
```

Expand Down
43 changes: 11 additions & 32 deletions tests/lib/plugin.js
Expand Up @@ -18,19 +18,10 @@ const plugin = require("../..");
function initCLI(isAutofixEnabled) {
const fix = isAutofixEnabled || false;
const cli = new CLIEngine({
envs: ["browser"],
extensions: ["md", "mkdn", "mdown", "markdown"],
plugins: ["markdown"],
fix,
ignore: false,
rules: {
"eol-last": 2,
"no-console": 2,
"no-undef": 2,
quotes: 2,
"spaced-comment": 2
},
useEslintrc: false
useEslintrc: false,
configFile: path.resolve(__dirname, "../fixtures/eslintrc.json")
});

cli.addPlugin("markdown", plugin);
Expand Down Expand Up @@ -133,6 +124,15 @@ describe("plugin", () => {
assert.strictEqual(report.results[0].messages[0].line, 2);
});

it("should run on files with any custom extension", () => {
const report = cli.executeOnText(shortText, "test.custom");

assert.strictEqual(report.results.length, 1);
assert.strictEqual(report.results[0].messages.length, 1);
assert.strictEqual(report.results[0].messages[0].message, "Unexpected console statement.");
assert.strictEqual(report.results[0].messages[0].line, 2);
});

it("should extract blocks and remap messages", () => {
const report = cli.executeOnFiles([path.resolve(__dirname, "../fixtures/long.md")]);

Expand Down Expand Up @@ -342,27 +342,6 @@ describe("plugin", () => {
assert.strictEqual(actual, expected);
});

it("in blocks with uncommon tags", () => {
const input = [
"This is Markdown.",
"",
"```JavaScript",
"console.log('Hello, world!')",
"```"
].join("\n");
const expected = [
"This is Markdown.",
"",
"```JavaScript",
"console.log(\"Hello, world!\")",
"```"
].join("\n");
const report = cli.executeOnText(input, "test.md");
const actual = report.results[0].output;

assert.strictEqual(actual, expected);
});

it("in blocks with extra backticks", () => {
const input = [
"This is Markdown.",
Expand Down

0 comments on commit 0d4dbe8

Please sign in to comment.