-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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(gatsby): add required eslint rules even if user has custom eslint config #28911
Merged
LekoArts
merged 7 commits into
fast-refresh/ensure-named-page-templates
from
fast-refresh/merge-eslint-rules-with-custom-ones
Jan 13, 2021
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
479af2c
feat(gatsby): add required eslint rules even if user has custom eslin…
pieh f5ad45a
handle case when extends is single string, add some tests
pieh cd016ff
some extra existance checking
pieh f477549
more exact checks for existance of eslint-loader rule
pieh 67d085b
let's see if slash fixes win32 + env var for rules
LekoArts 8fd1faf
another try
LekoArts 5c6bedc
don't use slash after all
LekoArts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if
.extends
exists but is not an array then theeslintRequirePreset
part is silently dropped? Smells like a bit like a footgun.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, I thought
extends
only accept arrays but it also accept just strings, so I will add handling for itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added handling for single string case in f5ad45a + some tests for it