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

Parser features for 7.14.0 #13098

Merged
merged 12 commits into from Apr 28, 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
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
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
FLOW_COMMIT = a1f9a4c709dcebb27a5084acf47755fbae699c25
TEST262_COMMIT = eca69e2c95972a4c5780ef58fe1f1e53e871b9b1
TYPESCRIPT_COMMIT = da8633212023517630de5f3620a23736b63234b1
TYPESCRIPT_COMMIT = 3de706a8525c2ded782fc032fa4afe2e485100d3

# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967
export FORCE_COLOR = true
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)"
}
20 changes: 20 additions & 0 deletions packages/babel-generator/src/generators/flow.ts
Expand Up @@ -738,3 +738,23 @@ export function Variance(this: Printer, node: t.Variance) {
export function VoidTypeAnnotation(this: Printer) {
this.word("void");
}

export function IndexedAccessType(this: Printer, node: t.IndexedAccessType) {
this.print(node.objectType, node);
this.token("[");
this.print(node.indexType, node);
this.token("]");
}

export function OptionalIndexedAccessType(
this: Printer,
node: t.OptionalIndexedAccessType,
) {
this.print(node.objectType, node);
if (node.optional) {
this.token("?.");
}
this.token("[");
this.print(node.indexType, node);
this.token("]");
}
15 changes: 14 additions & 1 deletion packages/babel-generator/src/generators/typescript.ts
Expand Up @@ -127,13 +127,22 @@ export function tsPrintPropertyOrMethodName(this: Printer, node) {
}

export function TSMethodSignature(this: Printer, node: t.TSMethodSignature) {
const { kind } = node;
if (kind === "set" || kind === "get") {
this.word(kind);
this.space();
}
this.tsPrintPropertyOrMethodName(node);
this.tsPrintSignatureDeclarationBase(node);
this.token(";");
}

export function TSIndexSignature(this: Printer, node: t.TSIndexSignature) {
const { readonly } = node;
const { readonly, static: isStatic } = node;
if (isStatic) {
this.word("static");
this.space();
}
if (readonly) {
this.word("readonly");
this.space();
Expand Down Expand Up @@ -648,6 +657,10 @@ export function tsPrintClassMemberModifiers(this: Printer, node: any, isField) {
this.word("static");
this.space();
}
if (node.override) {
this.word("override");
this.space();
}
if (node.abstract) {
this.word("abstract");
this.space();
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-generator/src/node/parentheses.ts
Expand Up @@ -135,6 +135,10 @@ export function UnionTypeAnnotation(node: any, parent: any): boolean {

export { UnionTypeAnnotation as IntersectionTypeAnnotation };

export function OptionalIndexedAccessType(node: any, parent: any): boolean {
return t.isIndexedAccessType(parent, { objectType: node });
}

export function TSAsExpression() {
return true;
}
Expand Down
@@ -0,0 +1,9 @@
type A = Obj['a'];

type B = Array<string>[number];

type C = Obj['bar'][foo]['boz'];

type D = (Obj['bar'])['baz'];

type E = Obj['bar'][];
@@ -0,0 +1,5 @@
type A = Obj['a'];
type B = Array<string>[number];
type C = Obj['bar'][foo]['boz'];
type D = Obj['bar']['baz'];
type E = Obj['bar'][];
@@ -0,0 +1,9 @@
type A = Obj?.['a'];

type B = Array<string>?.[number];

type C = Obj?.['bar']['baz'];

type D = (Obj?.['bar'])['baz'];

type E = Obj?.['bar'][];
@@ -0,0 +1,5 @@
type A = Obj?.['a'];
type B = Array<string>?.[number];
type C = Obj?.['bar']['baz'];
type D = (Obj?.['bar'])['baz'];
type E = Obj?.['bar'][];
@@ -1,4 +1,5 @@
class C {
[x: string]: any;
readonly [x: string]: any;
static [x: string]: any;
}
@@ -1,4 +1,5 @@
class C {
[x: string]: any;
readonly [x: string]: any;
}
static [x: string]: any;
}
@@ -0,0 +1,6 @@
class MyClass extends BaseClass {
override show() {}
public override show() {}
override size = 5;
public static override readonly size = 5;
}
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": ["typescript", "classProperties"]
}
@@ -0,0 +1,8 @@
class MyClass extends BaseClass {
override show() {}

public override show() {}

override size = 5;
public static override readonly size = 5;
}
@@ -0,0 +1,4 @@
interface Foo {
get foo();
set bar(v);
}
@@ -0,0 +1,4 @@
interface Foo {
get foo();
set bar(v);
}
8 changes: 8 additions & 0 deletions packages/babel-parser/src/parser/error-codes.js
@@ -0,0 +1,8 @@
// @flow

export const ErrorCodes = Object.freeze({
SyntaxError: "BABEL_PARSER_SYNTAX_ERROR",
SourceTypeModuleError: "BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED",
});

export type ErrorCode = $Values<typeof ErrorCodes>;