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

babel-parser: Add new internal ESLint rule to consistent error messages #13130

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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .eslintrc.cjs
Expand Up @@ -109,6 +109,7 @@ module.exports = {
),
},
],
"@babel/development-internal/report-error-message-format": "error",
},
},
{
Expand Down
39 changes: 39 additions & 0 deletions eslint/babel-eslint-plugin-development-internal/README.md
Expand Up @@ -68,3 +68,42 @@ and
}
}
```

### `@babel/development-internal/report-error-message-format`

This rule is inspired by https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/blob/master/docs/rules/report-message-format.md.

Intended for use in `packages/babel-parser/src/**/*`. When enabled, this rule warns for inconsistently error messages format in arguments of `makeErrorTemplates` function calls.

Basically, it starts with an uppercase Latin letter(A~Z) and ends with a period(.) or a question(?). But it can start with `'keyword'` or `` `code` `` to include JavaScript keywords or code in error messages.

valid:

```js
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?" });
```

invalid:

```js
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" });
```

Example configuration:

```js
{
rules: {
"@babel/development-internal/report-error-message-format": "error"
}
}
```
2 changes: 2 additions & 0 deletions eslint/babel-eslint-plugin-development-internal/src/index.js
@@ -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 };
@@ -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,
});
},
};
},
};
@@ -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" }],
},
],
});
@@ -1 +1 @@
SyntaxError: <CWD>\test.js: Missing semicolon (2:10)
SyntaxError: <CWD>\test.js: Missing semicolon. (2:10)
@@ -1 +1 @@
SyntaxError: <CWD>/test.js: Missing semicolon (2:10)
SyntaxError: <CWD>/test.js: Missing semicolon. (2:10)
@@ -1,3 +1,3 @@
{
"throws": "Missing semicolon (2:10)"
"throws": "Missing semicolon. (2:10)"
}