Skip to content

Commit 1d67970

Browse files
committed
docs: add eslint-plugin-markdown for JavaScript code samples in documentation
This applies our JavaScript linting to the JavaScript code samples in the markdown documentation files. * Autofixes styling/formatting/whitespace * Fixes and prevents syntax errors
1 parent 5b0ce68 commit 1d67970

24 files changed

+332
-400
lines changed

.eslintrc.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = {
66
extends: [
77
'not-an-aardvark/node',
88
'plugin:node/recommended',
9-
'plugin:self/all',
109
],
1110
rules: {
1211
'comma-dangle': [
@@ -18,15 +17,37 @@ module.exports = {
1817
},
1918
],
2019
'require-jsdoc': 'error',
21-
22-
'self/meta-property-ordering': 'off',
23-
'self/require-meta-docs-url': 'off',
24-
'self/report-message-format': ['error', '^[^a-z].*.$'],
2520
},
2621
overrides: [
22+
{
23+
files: ['lib/**/*.js', 'tests/**/*.js'],
24+
extends: ['plugin:self/all'],
25+
rules: {
26+
'self/meta-property-ordering': 'off',
27+
'self/report-message-format': ['error', '^[^a-z].*.$'],
28+
'self/require-meta-docs-url': 'off',
29+
},
30+
},
2731
{
2832
files: ['tests/**/*.js'],
2933
env: { mocha: true },
3034
},
35+
{
36+
files: ['**/*.md'],
37+
processor: 'markdown/markdown',
38+
},
39+
{
40+
// Markdown code samples in documentation:
41+
files: ['**/*.md/*.js'],
42+
plugins: ['markdown', 'self'],
43+
parserOptions: {
44+
sourceType: 'module',
45+
},
46+
noInlineConfig: true,
47+
rules: {
48+
'no-undef': 'off',
49+
'no-unused-vars': 'off',
50+
},
51+
},
3152
],
3253
};

docs/rules/consistent-output.md

+9-15
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,45 @@ This rule aims to ensure that if any invalid test cases have output assertions,
1313
Examples of **incorrect** code for this rule:
1414

1515
```js
16-
/* eslint eslint-plugin/consistent-output: error */
17-
1816
new RuleTester().run('example-rule', rule, {
1917
valid: [],
2018
invalid: [
2119
{
2220
code: 'foo',
2321
output: 'bar',
24-
errors: ['baz']
22+
errors: ['baz'],
2523
},
2624
{
2725
code: 'bar',
28-
errors: ['baz']
29-
}
30-
]
26+
errors: ['baz'],
27+
},
28+
],
3129
});
32-
3330
```
3431

3532
Examples of **correct** code for this rule:
3633

3734
```js
38-
/* eslint eslint-plugin/consistent-output: error */
39-
4035
new RuleTester().run('example-rule', rule, {
4136
valid: [],
4237
invalid: [
4338
{
4439
code: 'foo',
4540
output: 'bar',
46-
errors: ['baz']
41+
errors: ['baz'],
4742
},
4843
{
4944
code: 'bar',
5045
output: 'qux',
51-
errors: ['baz']
46+
errors: ['baz'],
5247
},
5348
{
5449
code: 'foo',
5550
output: null, // asserts that there is no autofix
56-
errors: ['baz']
57-
}
58-
]
51+
errors: ['baz'],
52+
},
53+
],
5954
});
60-
6155
```
6256

6357
## Options

docs/rules/fixer-return.md

+14-16
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,28 @@ This rule enforces that fixer functions always return a value.
99
Examples of **incorrect** code for this rule:
1010

1111
```js
12-
/* eslint eslint-plugin/fixer-return: error */
1312
module.exports = {
14-
create: function(context) {
15-
context.report( {
16-
fix: function(fixer) {
17-
fixer.foo();
18-
}
19-
});
20-
}
13+
create (context) {
14+
context.report({
15+
fix (fixer) {
16+
fixer.foo();
17+
},
18+
});
19+
},
2120
};
2221
```
2322

2423
Examples of **correct** code for this rule:
2524

2625
```js
27-
/* eslint eslint-plugin/fixer-return: error */
2826
module.exports = {
29-
create: function(context) {
30-
context.report( {
31-
fix: function(fixer) {
32-
return fixer.foo();
33-
}
34-
});
35-
}
27+
create (context) {
28+
context.report({
29+
fix (fixer) {
30+
return fixer.foo();
31+
},
32+
});
33+
},
3634
};
3735
```
3836

docs/rules/meta-property-ordering.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ Examples of **incorrect** code for this rule:
2323
// invalid; wrong order.
2424
module.exports = {
2525
meta: {
26-
docs: "",
27-
type: "problem",
28-
fixable: "code",
26+
docs: '',
27+
type: 'problem',
28+
fixable: 'code',
2929
},
30-
create() {},
31-
}
30+
create () {},
31+
};
3232

3333
// invalid; extra properties must be placed afterwards.
3434
module.exports = {
3535
meta: {
36-
type: "problem",
37-
fooooooooo: "foo",
38-
docs: "",
39-
fixable: "code",
36+
type: 'problem',
37+
fooooooooo: 'foo',
38+
docs: '',
39+
fixable: 'code',
4040
},
41-
create() {},
42-
}
41+
create () {},
42+
};
4343
```
4444

4545
Examples of **correct** code for this rule:
@@ -52,13 +52,13 @@ Examples of **correct** code for this rule:
5252
// valid;
5353
module.exports = {
5454
meta: {
55-
type: "bar",
56-
docs: "foo",
57-
messages: ["zoo"],
58-
fooooooooo: "foo",
55+
type: 'bar',
56+
docs: 'foo',
57+
messages: ['zoo'],
58+
fooooooooo: 'foo',
5959
},
60-
create() {},
61-
}
60+
create () {},
61+
};
6262
```
6363

6464
## When Not To Use It

docs/rules/no-deprecated-context-methods.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ Examples of **incorrect** code for this rule:
3535

3636
```js
3737
module.exports = {
38-
create(context) {
38+
create (context) {
3939
return {
40-
Program(node) {
40+
Program (node) {
4141
const firstToken = context.getFirstToken(node);
42-
}
43-
}
44-
}
45-
}
42+
},
43+
};
44+
},
45+
};
4646
```
4747

4848
Examples of **correct** code for this rule:
4949

5050
```js
5151
module.exports = {
52-
create(context) {
52+
create (context) {
5353
const sourceCode = context.getSourceCode();
5454

5555
return {
56-
Program(node) {
56+
Program (node) {
5757
const firstToken = sourceCode.getFirstToken(node);
58-
}
59-
}
60-
}
58+
},
59+
};
60+
},
6161
};
6262
```
6363

docs/rules/no-deprecated-report-api.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ The following patterns are considered warnings:
1212

1313
```js
1414
module.exports = {
15-
create(context) {
16-
15+
create (context) {
1716
context.report(node, 'This node is bad.');
18-
19-
context.report(node, loc, 'This node is bad.');
20-
21-
}
17+
},
2218
};
2319

2420
```
@@ -27,13 +23,11 @@ The following patterns are not warnings:
2723

2824
```js
2925
module.exports = {
30-
create(context) {
31-
26+
create (context) {
3227
context.report({ node, message: 'This node is bad.' });
3328

3429
context.report({ node, loc, message: 'This node is bad.' });
35-
36-
}
30+
},
3731
};
3832
```
3933

docs/rules/no-identical-tests.md

+12-18
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,25 @@ When a rule has a lot of tests, it's sometimes difficult to tell if any tests ar
99
Examples of **incorrect** code for this rule:
1010

1111
```js
12-
/* eslint eslint-plugin/no-identical-tests: error */
13-
14-
new RuleTester().run('foo', bar, {
15-
valid: [
16-
{ code: 'foo' },
17-
{ code: 'foo' }
18-
],
19-
invalid: []
12+
new RuleTester().run('foo', bar, {
13+
valid: [
14+
{ code: 'foo' },
15+
{ code: 'foo' },
16+
],
17+
invalid: [],
2018
});
21-
2219
```
2320

2421
Examples of **correct** code for this rule:
2522

2623
```js
27-
/* eslint eslint-plugin/no-identical-tests: error */
28-
29-
new RuleTester().run('foo', bar, {
30-
valid: [
31-
{ code: 'foo' },
32-
{ code: 'bar' }
33-
],
34-
invalid: []
24+
new RuleTester().run('foo', bar, {
25+
valid: [
26+
{ code: 'foo' },
27+
{ code: 'bar' },
28+
],
29+
invalid: [],
3530
});
36-
3731
```
3832

3933
## When Not To Use It

docs/rules/no-missing-placeholders.md

+9-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Report messages in rules can have placeholders surrounded by curly brackets.
66
context.report({
77
node,
88
message: '{{disallowedNode}} nodes are not allowed.',
9-
data: { disallowedNode: node.type }
9+
data: { disallowedNode: node.type },
1010
});
1111

1212
// Resulting message: e.g. 'IfStatement nodes are not allowed.'
@@ -21,49 +21,43 @@ This rule aims to disallow missing placeholders in rule report messages.
2121
Examples of **incorrect** code for this rule:
2222

2323
```js
24-
/*eslint eslint-plugin/no-missing-placeholders: error*/
25-
2624
module.exports = {
27-
create(context) {
25+
create (context) {
2826
context.report({
2927
node,
30-
message: '{{something}} is wrong.'
28+
message: '{{something}} is wrong.',
3129
});
3230

3331
context.report({
3432
node,
3533
message: '{{something}} is wrong.',
36-
data: { somethingElse: 'foo' }
34+
data: { somethingElse: 'foo' },
3735
});
3836

3937
context.report(node, '{{something}} is wrong.', { somethingElse: 'foo' });
40-
}
38+
},
4139
};
42-
4340
```
4441

4542
Examples of **correct** code for this rule:
4643

4744
```js
48-
/*eslint eslint-plugin/no-missing-placeholders: error*/
49-
5045
module.exports = {
51-
create(context) {
46+
create (context) {
5247
context.report({
5348
node,
54-
message: 'something is wrong.'
49+
message: 'something is wrong.',
5550
});
5651

5752
context.report({
5853
node,
5954
message: '{{something}} is wrong.',
60-
data: { something: 'foo' }
55+
data: { something: 'foo' },
6156
});
6257

6358
context.report(node, '{{something}} is wrong.', { something: 'foo' });
64-
}
59+
},
6560
};
66-
6761
```
6862

6963
## When Not To Use It

0 commit comments

Comments
 (0)