Skip to content

Commit

Permalink
feat: support @graphql-eslint/eslint-plugin out of box (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Aug 20, 2021
1 parent 76bd45e commit ec6fbb1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
matrix:
eslint-version: [7.x, 6.x, 5.x]
node-version: [16.x, 14.x, 12.x, 10.x, 8.x, 6.x]
test-graphql: [true, false]

exclude:
# eslint 7 does not support node 6 or 8
Expand All @@ -26,6 +27,11 @@ jobs:
node-version: 8.x
- eslint-version: 6.x
node-version: 6.x
# the version of graphql-config used in @graphql-eslint/eslint-plugin does not support node 8
- test-graphql: true
node-version: 8.x
- test-graphql: true
node-version: 6.x

steps:
- uses: actions/checkout@v2
Expand All @@ -41,5 +47,8 @@ jobs:
- name: Install
run: yarn install

- if: matrix.test-graphql == true
run: yarn add -D @graphql-eslint/eslint-plugin graphql

- name: Test
run: yarn run test
20 changes: 15 additions & 5 deletions eslint-plugin-prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ module.exports = {
})
: null;

const prettierFileInfo = prettier.getFileInfo.sync(
const { ignored, inferredParser } = prettier.getFileInfo.sync(
onDiskFilepath,
Object.assign(
{},
Expand All @@ -175,7 +175,7 @@ module.exports = {
);

// Skip if file is ignored using a .prettierignore file
if (prettierFileInfo.ignored) {
if (ignored) {
return;
}

Expand Down Expand Up @@ -206,11 +206,21 @@ module.exports = {
// * Prettier supports parsing the file type
// * There is an ESLint processor that extracts JavaScript snippets
// from the file type.
const parserBlocklist = [null, 'graphql', 'markdown', 'html'];
const parserBlocklist = [null, 'markdown', 'html'];

let inferParserToBabel =
parserBlocklist.indexOf(inferredParser) !== -1;

if (
filepath === onDiskFilepath &&
parserBlocklist.indexOf(prettierFileInfo.inferredParser) !== -1
// it could be processed by `@graphql-eslint/eslint-plugin` or `eslint-plugin-graphql`
inferredParser === 'graphql' &&
// for `eslint-plugin-graphql`, see https://github.com/apollographql/eslint-plugin-graphql/blob/master/src/index.js#L416
source.startsWith('ESLintPluginGraphQLFile`')
) {
inferParserToBabel = true;
}

if (filepath === onDiskFilepath && inferParserToBabel) {
// Prettier v1.16.0 renamed the `babylon` parser to `babel`
// Use the modern name if available
const supportBabelParser = prettier
Expand Down
28 changes: 27 additions & 1 deletion test/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const RuleTester = require('eslint').RuleTester;

const ruleTester = new RuleTester();

let graphqlEslintParserPath;

try {
graphqlEslintParserPath = require.resolve('@graphql-eslint/eslint-plugin');
} catch (e) {
// ignore
}

ruleTester.run('prettier', rule, {
valid: [
// Correct style.
Expand Down Expand Up @@ -77,8 +85,26 @@ ruleTester.run('prettier', rule, {
{
code: `('');\n`,
filename: path.join(__filename, '0_fake_virtual_name.js')
},
{
code: 'ESLintPluginGraphQLFile`type Query {\n foo: String!\n}`\n',
filename: getPrettierRcJsFilename('no-semi', 'dummy.graphql'),
parserOptions: {
ecmaVersion: 2015
}
}
],
].concat(
graphqlEslintParserPath
? {
code: `type Query {
foo: String!
}
`,
filename: 'valid.graphql',
parser: graphqlEslintParserPath
}
: []
),
invalid: [
'01',
'02',
Expand Down
4 changes: 4 additions & 0 deletions test/prettierrc/no-semi/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": false
}

0 comments on commit ec6fbb1

Please sign in to comment.