Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add internal ESLint rule for consistent parser error messages (#13130)
- Loading branch information
1 parent
bf14a10
commit 2521c66
Showing
1,398 changed files
with
3,596 additions
and
3,312 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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import dryErrorMessages from "./rules/dry-error-messages"; | ||
import reportErrorMessageFormat from "./rules/report-error-message-format"; | ||
|
||
export const rules = { | ||
"dry-error-messages": dryErrorMessages, | ||
"report-error-message-format": reportErrorMessageFormat, | ||
}; | ||
|
||
export default { rules }; |
31 changes: 31 additions & 0 deletions
31
eslint/babel-eslint-plugin-development-internal/src/rules/report-error-message-format.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const messageId = "mustMatchPattern"; | ||
|
||
const pattern = /(('.*')|(`.*`)|[A-Z]).*(\.|\?)$/s; | ||
|
||
export default { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "enforce @babel/parser's error message formatting.", | ||
}, | ||
messages: { | ||
[messageId]: `Report message does not match the pattern ${pattern.toString()}.`, | ||
}, | ||
}, | ||
create({ report }) { | ||
return { | ||
"CallExpression[callee.type='Identifier'][callee.name='makeErrorTemplates'] > ObjectExpression > Property[value.type='Literal']"( | ||
node, | ||
) { | ||
const { value } = node; | ||
if (typeof value.value === "string" && pattern.test(value.value)) { | ||
return; | ||
} | ||
report({ | ||
node: value, | ||
messageId, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
44 changes: 44 additions & 0 deletions
44
eslint/babel-eslint-plugin-development-internal/test/rules/report-error-message-formtat.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester"; | ||
import rule from "../../src/rules/report-error-message-format"; | ||
|
||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run("report-error-message-format", rule, { | ||
valid: [ | ||
"makeErrorTemplates({});", | ||
'makeErrorTemplates({ ThisIsAnError: "This is an error." });', | ||
`makeErrorTemplates({ ThisIsAnError: "'this' is an error." });`, | ||
`makeErrorTemplates({ ThisIsAnError: "\`this\` is an error." });`, | ||
`makeErrorTemplates({ ThisIsAnError: "This is an error?" });`, | ||
`makeErrorTemplates({ ThisIsAnError: "'this' is an error?" });`, | ||
`makeErrorTemplates({ ThisIsAnError: "\`this\` is an error?" });`, | ||
'makeErrorTemplates({ ThisIsAnError: "This is\\nan error." });', | ||
`makeErrorTemplates({ ThisIsAnError: "'this' is\\nan error." });`, | ||
`makeErrorTemplates({ ThisIsAnError: "\`this\` is\\nan error." });`, | ||
`makeErrorTemplates({ ThisIsAnError: "This is\\nan error?" });`, | ||
`makeErrorTemplates({ ThisIsAnError: "'this' is\\nan error?" });`, | ||
`makeErrorTemplates({ ThisIsAnError: "\`this\` is\\nan error?" });`, | ||
], | ||
invalid: [ | ||
{ | ||
code: "makeErrorTemplates({ ThisIsAnError: 'this is an error.' });", | ||
errors: [{ messageId: "mustMatchPattern" }], | ||
}, | ||
{ | ||
code: "makeErrorTemplates({ ThisIsAnError: 'This is an error' });", | ||
errors: [{ messageId: "mustMatchPattern" }], | ||
}, | ||
{ | ||
code: "makeErrorTemplates({ ThisIsAnError: 'this is an error?' });", | ||
errors: [{ messageId: "mustMatchPattern" }], | ||
}, | ||
{ | ||
code: "makeErrorTemplates({ ThisIsAnError: '`this` is an error' });", | ||
errors: [{ messageId: "mustMatchPattern" }], | ||
}, | ||
{ | ||
code: `makeErrorTemplates({ ThisIsAnError: "'this' is an error" });`, | ||
errors: [{ messageId: "mustMatchPattern" }], | ||
}, | ||
], | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/babel-cli/test/fixtures/babel/stdin --filename windows/stderr.txt
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 |
---|---|---|
@@ -1 +1 @@ | ||
SyntaxError: <CWD>\test.js: Missing semicolon (2:10) | ||
SyntaxError: <CWD>\test.js: Missing semicolon. (2:10) |
2 changes: 1 addition & 1 deletion
2
packages/babel-cli/test/fixtures/babel/stdin --filename/stderr.txt
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 |
---|---|---|
@@ -1 +1 @@ | ||
SyntaxError: <CWD>/test.js: Missing semicolon (2:10) | ||
SyntaxError: <CWD>/test.js: Missing semicolon. (2:10) |
2 changes: 1 addition & 1 deletion
2
packages/babel-core/test/fixtures/transformation/errors/syntax/options.json
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"throws": "Missing semicolon (2:10)" | ||
"throws": "Missing semicolon. (2:10)" | ||
} |
Oops, something went wrong.