Skip to content

Commit ec6fbb1

Browse files
authored
feat: support @graphql-eslint/eslint-plugin out of box (#413)
1 parent 76bd45e commit ec6fbb1

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

.github/workflows/ci.yml

+9
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
matrix:
1414
eslint-version: [7.x, 6.x, 5.x]
1515
node-version: [16.x, 14.x, 12.x, 10.x, 8.x, 6.x]
16+
test-graphql: [true, false]
1617

1718
exclude:
1819
# eslint 7 does not support node 6 or 8
@@ -26,6 +27,11 @@ jobs:
2627
node-version: 8.x
2728
- eslint-version: 6.x
2829
node-version: 6.x
30+
# the version of graphql-config used in @graphql-eslint/eslint-plugin does not support node 8
31+
- test-graphql: true
32+
node-version: 8.x
33+
- test-graphql: true
34+
node-version: 6.x
2935

3036
steps:
3137
- uses: actions/checkout@v2
@@ -41,5 +47,8 @@ jobs:
4147
- name: Install
4248
run: yarn install
4349

50+
- if: matrix.test-graphql == true
51+
run: yarn add -D @graphql-eslint/eslint-plugin graphql
52+
4453
- name: Test
4554
run: yarn run test

eslint-plugin-prettier.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module.exports = {
165165
})
166166
: null;
167167

168-
const prettierFileInfo = prettier.getFileInfo.sync(
168+
const { ignored, inferredParser } = prettier.getFileInfo.sync(
169169
onDiskFilepath,
170170
Object.assign(
171171
{},
@@ -175,7 +175,7 @@ module.exports = {
175175
);
176176

177177
// Skip if file is ignored using a .prettierignore file
178-
if (prettierFileInfo.ignored) {
178+
if (ignored) {
179179
return;
180180
}
181181

@@ -206,11 +206,21 @@ module.exports = {
206206
// * Prettier supports parsing the file type
207207
// * There is an ESLint processor that extracts JavaScript snippets
208208
// from the file type.
209-
const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
209+
const parserBlocklist = [null, 'markdown', 'html'];
210+
211+
let inferParserToBabel =
212+
parserBlocklist.indexOf(inferredParser) !== -1;
213+
210214
if (
211-
filepath === onDiskFilepath &&
212-
parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
215+
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
216+
inferredParser === 'graphql' &&
217+
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
218+
source.startsWith('ESLintPluginGraphQLFile`')
213219
) {
220+
inferParserToBabel = true;
221+
}
222+
223+
if (filepath === onDiskFilepath && inferParserToBabel) {
214224
// Prettier v1.16.0 renamed the `babylon` parser to `babel`
215225
// Use the modern name if available
216226
const supportBabelParser = prettier

test/prettier.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ const RuleTester = require('eslint').RuleTester;
2626

2727
const ruleTester = new RuleTester();
2828

29+
let graphqlEslintParserPath;
30+
31+
try {
32+
graphqlEslintParserPath = require.resolve('@graphql-eslint/eslint-plugin');
33+
} catch (e) {
34+
// ignore
35+
}
36+
2937
ruleTester.run('prettier', rule, {
3038
valid: [
3139
// Correct style.
@@ -77,8 +85,26 @@ ruleTester.run('prettier', rule, {
7785
{
7886
code: `('');\n`,
7987
filename: path.join(__filename, '0_fake_virtual_name.js')
88+
},
89+
{
90+
code: 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n',
91+
filename: getPrettierRcJsFilename('no-semi', 'dummy.graphql'),
92+
parserOptions: {
93+
ecmaVersion: 2015
94+
}
8095
}
81-
],
96+
].concat(
97+
graphqlEslintParserPath
98+
? {
99+
code: `type Query {
100+
foo: String!
101+
}
102+
`,
103+
filename: 'valid.graphql',
104+
parser: graphqlEslintParserPath
105+
}
106+
: []
107+
),
82108
invalid: [
83109
'01',
84110
'02',

test/prettierrc/no-semi/.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"semi": false
3+
}
4+

0 commit comments

Comments
 (0)