Skip to content

Commit 161026d

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 d71c8b3 commit 161026d

24 files changed

+348
-359
lines changed

.eslintrc.js

+29-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
module.exports = {
44
root: true,
5-
plugins: ['node', 'self'],
5+
plugins: ['node'],
66
extends: [
77
'not-an-aardvark/node',
88
'plugin:node/recommended',
9-
'plugin:self/all',
109
'plugin:unicorn/recommended',
1110
],
1211
parserOptions: {
@@ -24,10 +23,6 @@ module.exports = {
2423
],
2524
'require-jsdoc': 'error',
2625

27-
'self/meta-property-ordering': 'off',
28-
'self/require-meta-docs-url': 'off',
29-
'self/report-message-format': ['error', '^[^a-z].*.$'],
30-
3126
'unicorn/consistent-function-scoping': 'off',
3227
'unicorn/no-array-callback-reference': 'off',
3328
'unicorn/no-array-for-each': 'off',
@@ -37,9 +32,37 @@ module.exports = {
3732
'unicorn/prevent-abbreviations': 'off',
3833
},
3934
overrides: [
35+
{
36+
// Apply eslint-plugin rules to our own rules/tests (but not docs).
37+
files: ['lib/**/*.js', 'tests/**/*.js'],
38+
plugins: ['self'],
39+
extends: ['plugin:self/all'],
40+
rules: {
41+
'self/meta-property-ordering': 'off',
42+
'self/report-message-format': ['error', '^[^a-z].*.$'],
43+
'self/require-meta-docs-url': 'off',
44+
},
45+
},
4046
{
4147
files: ['tests/**/*.js'],
4248
env: { mocha: true },
4349
},
50+
{
51+
files: ['**/*.md'],
52+
processor: 'markdown/markdown',
53+
},
54+
{
55+
// Markdown JS code samples in documentation:
56+
files: ['**/*.md/*.js'],
57+
plugins: ['markdown'],
58+
noInlineConfig: true,
59+
rules: {
60+
'no-undef': 'off',
61+
'no-unused-vars': 'off',
62+
strict: 'off',
63+
64+
'unicorn/filename-case': 'off',
65+
},
66+
},
4467
],
4568
};

docs/rules/consistent-output.md

+9-11
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ new RuleTester().run('example-rule', rule, {
2121
{
2222
code: 'foo',
2323
output: 'bar',
24-
errors: ['baz']
24+
errors: ['baz'],
2525
},
2626
{
2727
code: 'bar',
28-
errors: ['baz']
29-
}
30-
]
28+
errors: ['baz'],
29+
},
30+
],
3131
});
32-
3332
```
3433

3534
Examples of **correct** code for this rule:
@@ -43,21 +42,20 @@ new RuleTester().run('example-rule', rule, {
4342
{
4443
code: 'foo',
4544
output: 'bar',
46-
errors: ['baz']
45+
errors: ['baz'],
4746
},
4847
{
4948
code: 'bar',
5049
output: 'qux',
51-
errors: ['baz']
50+
errors: ['baz'],
5251
},
5352
{
5453
code: 'foo',
5554
output: null, // asserts that there is no autofix
56-
errors: ['baz']
57-
}
58-
]
55+
errors: ['baz'],
56+
},
57+
],
5958
});
60-
6159
```
6260

6361
## Options

docs/rules/fixer-return.md

+16-14
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,31 @@ Examples of **incorrect** code for this rule:
1010

1111
```js
1212
/* eslint eslint-plugin/fixer-return: error */
13+
1314
module.exports = {
14-
create: function(context) {
15-
context.report( {
16-
fix: function(fixer) {
17-
fixer.foo();
18-
}
19-
});
20-
}
15+
create (context) {
16+
context.report({
17+
fix (fixer) {
18+
fixer.foo();
19+
},
20+
});
21+
},
2122
};
2223
```
2324

2425
Examples of **correct** code for this rule:
2526

2627
```js
2728
/* eslint eslint-plugin/fixer-return: error */
29+
2830
module.exports = {
29-
create: function(context) {
30-
context.report( {
31-
fix: function(fixer) {
32-
return fixer.foo();
33-
}
34-
});
35-
}
31+
create (context) {
32+
context.report({
33+
fix (fixer) {
34+
return fixer.foo();
35+
},
36+
});
37+
},
3638
};
3739
```
3840

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

+16-18
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,29 @@ 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+
/* 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: [],
2020
});
21-
2221
```
2322

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

2625
```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: []
26+
/* eslint eslint-plugin/no-identical-tests: error */
27+
28+
new RuleTester().run('foo', bar, {
29+
valid: [
30+
{ code: 'foo' },
31+
{ code: 'bar' },
32+
],
33+
invalid: [],
3534
});
36-
3735
```
3836

3937
## When Not To Use It

docs/rules/no-missing-placeholders.md

+11-13
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,47 @@ 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*/
24+
/* eslint eslint-plugin/no-missing-placeholders: error*/
2525

2626
module.exports = {
27-
create(context) {
27+
create (context) {
2828
context.report({
2929
node,
30-
message: '{{something}} is wrong.'
30+
message: '{{something}} is wrong.',
3131
});
3232

3333
context.report({
3434
node,
3535
message: '{{something}} is wrong.',
36-
data: { somethingElse: 'foo' }
36+
data: { somethingElse: 'foo' },
3737
});
3838

3939
context.report(node, '{{something}} is wrong.', { somethingElse: 'foo' });
40-
}
40+
},
4141
};
42-
4342
```
4443

4544
Examples of **correct** code for this rule:
4645

4746
```js
48-
/*eslint eslint-plugin/no-missing-placeholders: error*/
47+
/* eslint eslint-plugin/no-missing-placeholders: error*/
4948

5049
module.exports = {
51-
create(context) {
50+
create (context) {
5251
context.report({
5352
node,
54-
message: 'something is wrong.'
53+
message: 'something is wrong.',
5554
});
5655

5756
context.report({
5857
node,
5958
message: '{{something}} is wrong.',
60-
data: { something: 'foo' }
59+
data: { something: 'foo' },
6160
});
6261

6362
context.report(node, '{{something}} is wrong.', { something: 'foo' });
64-
}
63+
},
6564
};
66-
6765
```
6866

6967
## When Not To Use It

0 commit comments

Comments
 (0)