Skip to content

Commit 52eec48

Browse files
authoredJun 27, 2022
feat: skip processing code blocks on specific languages like stylelint-prettier (#415)
1 parent 42bfe88 commit 52eec48

File tree

8 files changed

+1495
-997
lines changed

8 files changed

+1495
-997
lines changed
 

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
!.eslintrc.js
2+
test/fixtures

‎.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
node-version: [12, 14, 16]
1616

1717
steps:
18-
- uses: actions/checkout@v2
18+
- uses: actions/checkout@v3
1919

2020
- name: Use Node.js ${{ matrix.node-version }}
21-
uses: actions/setup-node@v2
21+
uses: actions/setup-node@v3
2222
with:
2323
node-version: ${{ matrix.node-version }}
2424

‎eslint-plugin-prettier.js

+46-15
Original file line numberDiff line numberDiff line change
@@ -179,22 +179,53 @@ module.exports = {
179179
// * Prettier supports parsing the file type
180180
// * There is an ESLint processor that extracts JavaScript snippets
181181
// from the file type.
182-
const parserBlocklist = [null, 'markdown', 'html'];
183-
184-
let inferParserToBabel =
185-
parserBlocklist.indexOf(inferredParser) !== -1;
186-
187-
if (
188-
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
189-
inferredParser === 'graphql' &&
190-
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
191-
source.startsWith('ESLintPluginGraphQLFile`')
192-
) {
193-
inferParserToBabel = true;
194-
}
182+
if (filepath === onDiskFilepath) {
183+
// The following list means the plugin process source into js content
184+
// but with same filename, so we need to change the parser to `babel`
185+
// by default.
186+
// Related ESLint plugins are:
187+
// 1. `eslint-plugin-graphql` (replacement: `@graphql-eslint/eslint-plugin`)
188+
// 2. `eslint-plugin-markdown@1` (replacement: `eslint-plugin-markdown@2+`)
189+
// 3. `eslint-plugin-html`
190+
const parserBlocklist = [null, 'markdown', 'html'];
191+
192+
let inferParserToBabel =
193+
parserBlocklist.indexOf(inferredParser) !== -1;
194+
195+
if (
196+
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
197+
inferredParser === 'graphql' &&
198+
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
199+
source.startsWith('ESLintPluginGraphQLFile`')
200+
) {
201+
inferParserToBabel = true;
202+
}
195203

196-
if (filepath === onDiskFilepath && inferParserToBabel) {
197-
initialOptions.parser = 'babel';
204+
if (inferParserToBabel) {
205+
initialOptions.parser = 'babel';
206+
}
207+
} else {
208+
// Similar to https://github.com/prettier/stylelint-prettier/pull/22
209+
// In all of the following cases ESLint extracts a part of a file to
210+
// be formatted and there exists a prettier parser for the whole file.
211+
// If you're interested in prettier you'll want a fully formatted file so
212+
// you're about to run prettier over the whole file anyway.
213+
// Therefore running prettier over just the style section is wasteful, so
214+
// skip it.
215+
const parserBlocklist = [
216+
'babel',
217+
'babylon',
218+
'flow',
219+
'typescript',
220+
'vue',
221+
'markdown',
222+
'html',
223+
'mdx',
224+
'angular',
225+
];
226+
if (parserBlocklist.indexOf(inferredParser) !== -1) {
227+
return;
228+
}
198229
}
199230

200231
const prettierOptions = Object.assign(

‎package.json

+16-9
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,33 @@
3535
"prettier": ">=2.0.0"
3636
},
3737
"devDependencies": {
38-
"@graphql-eslint/eslint-plugin": "^2.3.0",
38+
"@graphql-eslint/eslint-plugin": "^2.5.0",
3939
"@not-an-aardvark/node-release-script": "^0.1.0",
40-
"eslint": "^8.1.0",
40+
"@typescript-eslint/parser": "^5.29.0",
41+
"eslint": "^8.18.0",
4142
"eslint-config-not-an-aardvark": "^2.1.0",
42-
"eslint-config-prettier": "^8.3.0",
43-
"eslint-plugin-eslint-plugin": "^4.0.2",
43+
"eslint-config-prettier": "^8.5.0",
44+
"eslint-mdx": "^1.17.0",
45+
"eslint-plugin-eslint-plugin": "^4.3.0",
46+
"eslint-plugin-mdx": "^1.17.0",
4447
"eslint-plugin-node": "^11.1.0",
48+
"eslint-plugin-prettier": "link:.",
4549
"eslint-plugin-self": "^1.2.1",
46-
"graphql": "^15.7.1",
47-
"mocha": "^6.2.3",
48-
"prettier": "^2.4.1",
49-
"vue-eslint-parser": "^8.0.0"
50+
"graphql": "^16.5.0",
51+
"mocha": "^9.2.2",
52+
"prettier": "^2.7.1",
53+
"vue-eslint-parser": "^8.3.0"
5054
},
5155
"peerDependenciesMeta": {
5256
"eslint-config-prettier": {
5357
"optional": true
5458
}
5559
},
60+
"resolutions": {
61+
"@babel/traverse": "^7.18.5"
62+
},
5663
"engines": {
57-
"node": ">=6.0.0"
64+
"node": ">=12.0.0"
5865
},
5966
"license": "MIT"
6067
}

‎test/fixtures/mdx.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Fragment } from 'react'
2+
3+
<div>Hello World!</div>
4+
5+
```ts
6+
export declare const x = 1
7+
```

‎test/invalid/mdx.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
CODE:
2+
import React from 'react'
3+
4+
<div>Hello World</div>
5+
6+
OUTPUT:
7+
import React from 'react';
8+
9+
<div>Hello World</div>
10+
11+
OPTIONS:
12+
[]
13+
14+
ERRORS:
15+
[
16+
{
17+
message: 'Insert `;`',
18+
line: 1, column: 26, endLine: 1, endColumn: 26,
19+
},
20+
]

‎test/prettier.js

+107-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,38 @@ const path = require('path');
1818
const eslintPluginPrettier = require('..');
1919

2020
const rule = eslintPluginPrettier.rules.prettier;
21-
const RuleTester = require('eslint').RuleTester;
21+
22+
const assert = require('assert');
23+
const { ESLint, RuleTester } = require('eslint');
2224

2325
// ------------------------------------------------------------------------------
2426
// Tests
2527
// ------------------------------------------------------------------------------
2628

29+
const eslint = new ESLint({
30+
baseConfig: {
31+
parserOptions: {
32+
ecmaVersion: 2021,
33+
ecmaFeatures: {
34+
jsx: true,
35+
},
36+
sourceType: 'module',
37+
},
38+
extends: 'plugin:prettier/recommended',
39+
overrides: [
40+
{
41+
files: '*.mdx',
42+
extends: 'plugin:mdx/recommended',
43+
settings: {
44+
'mdx/code-block': true,
45+
},
46+
},
47+
],
48+
},
49+
useEslintrc: false,
50+
ignore: false,
51+
});
52+
2753
const ruleTester = new RuleTester();
2854

2955
ruleTester.run('prettier', rule, {
@@ -154,6 +180,69 @@ eslintPluginGraphqlRuleTester.run('eslint-plugin-graphql', rule, {
154180
invalid: [],
155181
});
156182

183+
const mdxRuleTester = new RuleTester({
184+
parser: require.resolve('eslint-mdx'),
185+
parserOptions: {
186+
...require('eslint-mdx').DEFAULT_PARSER_OPTIONS,
187+
// mdx-js/eslint-mdx#366
188+
ecmaVersion: 2021,
189+
},
190+
});
191+
192+
mdxRuleTester.run('eslint-plugin-mdx', rule, {
193+
valid: [
194+
{
195+
code: [
196+
"import React from 'react';",
197+
'',
198+
'<div>Hello World</div>',
199+
'',
200+
].join('\n'),
201+
filename: 'valid.mdx',
202+
},
203+
],
204+
invalid: [
205+
Object.assign(loadInvalidFixture('mdx'), {
206+
filename: 'invalid.mdx',
207+
}),
208+
],
209+
});
210+
211+
runFixture('mdx', [
212+
[
213+
{
214+
column: 33,
215+
endColumn: 33,
216+
endLine: 1,
217+
fix: {
218+
range: [32, 32],
219+
text: ';',
220+
},
221+
line: 1,
222+
message: 'Insert `;`',
223+
messageId: 'insert',
224+
nodeType: null,
225+
ruleId: 'prettier/prettier',
226+
severity: 2,
227+
},
228+
{
229+
column: 27,
230+
endColumn: 27,
231+
endLine: 6,
232+
fix: {
233+
range: [91, 91],
234+
text: ';',
235+
},
236+
line: 6,
237+
message: 'Insert `;`',
238+
messageId: 'insert',
239+
nodeType: null,
240+
ruleId: 'prettier/prettier',
241+
severity: 2,
242+
},
243+
],
244+
]);
245+
157246
// ------------------------------------------------------------------------------
158247
// Helpers
159248
// ------------------------------------------------------------------------------
@@ -193,3 +282,20 @@ function loadInvalidFixture(name) {
193282
function getPrettierRcJsFilename(dir, file = 'dummy.js') {
194283
return path.resolve(__dirname, `./prettierrc/${dir}/${file}`);
195284
}
285+
286+
function runFixture(name, asserts) {
287+
return eslint
288+
.lintFiles(`test/fixtures/${name}.*`)
289+
.then((results) =>
290+
assert.deepStrictEqual(
291+
asserts,
292+
results.map(({ messages }) => messages)
293+
)
294+
)
295+
.catch((err) => {
296+
// eslint-disable-next-line no-console
297+
console.error(err);
298+
// eslint-disable-next-line no-process-exit
299+
process.exit(1);
300+
});
301+
}

‎yarn.lock

+1,296-970
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.