-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby): add required eslint rules even if user has custom eslin…
…t config (#28911) * feat(gatsby): add required eslint rules even if user has custom eslint config * handle case when extends is single string, add some tests * some extra existance checking * more exact checks for existance of eslint-loader rule * let's see if slash fixes win32 + env var for rules * another try * don't use slash after all Co-authored-by: LekoArts <lekoarts@gmail.com>
- Loading branch information
Showing
5 changed files
with
267 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { mergeRequiredConfigIn } from "../eslint-config" | ||
import { CLIEngine } from "eslint" | ||
import * as path from "path" | ||
|
||
describe(`eslint-config`, () => { | ||
describe(`mergeRequiredConfigIn`, () => { | ||
it(`adds rulePaths and extends if those don't exist`, () => { | ||
const conf: CLIEngine.Options = {} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf?.baseConfig).toMatchInlineSnapshot(` | ||
Object { | ||
"extends": Array [ | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint/required.js", | ||
], | ||
} | ||
`) | ||
|
||
expect(conf.rulePaths).toMatchInlineSnapshot(` | ||
Array [ | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint-rules", | ||
] | ||
`) | ||
}) | ||
|
||
it(`adds rulePath if rulePaths exist but don't contain required rules`, () => { | ||
const conf: CLIEngine.Options = { | ||
rulePaths: [`test`], | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.rulePaths).toMatchInlineSnapshot(` | ||
Array [ | ||
"test", | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint-rules", | ||
] | ||
`) | ||
}) | ||
|
||
it(`doesn't add rulePath multiple times`, () => { | ||
const conf: CLIEngine.Options = { | ||
rulePaths: [path.resolve(__dirname, `../eslint-rules`), `test`], | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.rulePaths).toMatchInlineSnapshot(` | ||
Array [ | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint-rules", | ||
"test", | ||
] | ||
`) | ||
}) | ||
|
||
it(`adds extend if extends exist (array) but don't contain required preset`, () => { | ||
const conf: CLIEngine.Options = { | ||
baseConfig: { | ||
extends: [`ext1`], | ||
}, | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.baseConfig).toMatchInlineSnapshot(` | ||
Object { | ||
"extends": Array [ | ||
"ext1", | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint/required.js", | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
it(`adds extend if extends exist (string) but don't contain required preset`, () => { | ||
const conf: CLIEngine.Options = { | ||
baseConfig: { | ||
extends: `ext1`, | ||
}, | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.baseConfig).toMatchInlineSnapshot(` | ||
Object { | ||
"extends": Array [ | ||
"ext1", | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint/required.js", | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
it(`doesn't add extend multiple times (extends is array)`, () => { | ||
const conf: CLIEngine.Options = { | ||
baseConfig: { | ||
extends: [require.resolve(`../eslint/required`), `ext1`], | ||
}, | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.baseConfig).toMatchInlineSnapshot(` | ||
Object { | ||
"extends": Array [ | ||
"<PROJECT_ROOT>/packages/gatsby/src/utils/eslint/required.js", | ||
"ext1", | ||
], | ||
} | ||
`) | ||
}) | ||
|
||
it(`doesn't add extend multiple times (extends is string)`, () => { | ||
const conf: CLIEngine.Options = { | ||
baseConfig: { | ||
extends: require.resolve(`../eslint/required`), | ||
}, | ||
} | ||
|
||
mergeRequiredConfigIn(conf) | ||
|
||
expect(conf.baseConfig).toMatchInlineSnapshot(` | ||
Object { | ||
"extends": "<PROJECT_ROOT>/packages/gatsby/src/utils/eslint/required.js", | ||
} | ||
`) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = { | ||
rules: { | ||
// Custom ESLint rules from Gatsby | ||
"no-anonymous-exports-page-templates": | ||
process.env.GATSBY_HOT_LOADER === `fast-refresh` ? `warn` : `off`, | ||
"limited-exports-page-templates": | ||
process.env.GATSBY_HOT_LOADER === `fast-refresh` ? `warn` : `off`, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters