Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support @graphql-eslint/eslint-plugin out of box #413

Merged
merged 3 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
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
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
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
@@ -0,0 +1,4 @@
{
"semi": false
}