From ec6fbb159e2454c6e145db55480932dc953cf7c1 Mon Sep 17 00:00:00 2001 From: JounQin Date: Sat, 21 Aug 2021 04:47:29 +0800 Subject: [PATCH] feat: support `@graphql-eslint/eslint-plugin` out of box (#413) --- .github/workflows/ci.yml | 9 +++++++++ eslint-plugin-prettier.js | 20 +++++++++++++++----- test/prettier.js | 28 +++++++++++++++++++++++++++- test/prettierrc/no-semi/.prettierrc | 4 ++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 test/prettierrc/no-semi/.prettierrc diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7474f2cc..2d9a021c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 @@ -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 diff --git a/eslint-plugin-prettier.js b/eslint-plugin-prettier.js index 3f050f07..5208d054 100644 --- a/eslint-plugin-prettier.js +++ b/eslint-plugin-prettier.js @@ -165,7 +165,7 @@ module.exports = { }) : null; - const prettierFileInfo = prettier.getFileInfo.sync( + const { ignored, inferredParser } = prettier.getFileInfo.sync( onDiskFilepath, Object.assign( {}, @@ -175,7 +175,7 @@ module.exports = { ); // Skip if file is ignored using a .prettierignore file - if (prettierFileInfo.ignored) { + if (ignored) { return; } @@ -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 diff --git a/test/prettier.js b/test/prettier.js index f2e49330..b4858c9f 100644 --- a/test/prettier.js +++ b/test/prettier.js @@ -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. @@ -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', diff --git a/test/prettierrc/no-semi/.prettierrc b/test/prettierrc/no-semi/.prettierrc new file mode 100644 index 00000000..ab7b2670 --- /dev/null +++ b/test/prettierrc/no-semi/.prettierrc @@ -0,0 +1,4 @@ +{ + "semi": false +} + \ No newline at end of file