Skip to content

feat(typescript-estree): support short-circuiting assignment operators #2307

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

Merged
merged 1 commit into from
Jul 19, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
318 changes: 318 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31910,6 +31910,324 @@ Object {
}
`;

exports[`typescript fixtures/basics/short-circuiting-assignment-and-and.src 1`] = `
Object {
"$id": 3,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 2,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [
Object {
"$id": 0,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "a",
"range": Array [
0,
1,
],
"type": "Identifier",
},
"kind": "rw",
"resolved": null,
"writeExpr": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
},
Object {
"$id": 1,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
"kind": "r",
"resolved": null,
"writeExpr": undefined,
},
],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "module",
"upperScope": Object {
"$ref": 3,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 2,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 3,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/basics/short-circuiting-assignment-or-or.src 1`] = `
Object {
"$id": 3,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 2,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [
Object {
"$id": 0,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "a",
"range": Array [
0,
1,
],
"type": "Identifier",
},
"kind": "rw",
"resolved": null,
"writeExpr": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
},
Object {
"$id": 1,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
"kind": "r",
"resolved": null,
"writeExpr": undefined,
},
],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "module",
"upperScope": Object {
"$ref": 3,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 2,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 3,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/basics/short-circuiting-assignment-question-question.src 1`] = `
Object {
"$id": 3,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 2,
"block": Object {
"range": Array [
0,
9,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [
Object {
"$id": 0,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "a",
"range": Array [
0,
1,
],
"type": "Identifier",
},
"kind": "rw",
"resolved": null,
"writeExpr": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
},
Object {
"$id": 1,
"from": Object {
"$ref": 2,
},
"identifier": Object {
"name": "b",
"range": Array [
6,
7,
],
"type": "Identifier",
},
"kind": "r",
"resolved": null,
"writeExpr": undefined,
},
],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "module",
"upperScope": Object {
"$ref": 3,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 2,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [
Object {
"$ref": 0,
},
Object {
"$ref": 1,
},
],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 3,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/basics/symbol-type-param.src 1`] = `
Object {
"$id": 5,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ||= b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a &&= b;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a ??= b;
17 changes: 17 additions & 0 deletions packages/types/src/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,23 @@ export interface ArrowFunctionExpression extends BaseNode {

export interface AssignmentExpression extends BinaryExpressionBase {
type: AST_NODE_TYPES.AssignmentExpression;
operator:
| '-='
| '??='
| '**='
| '*='
| '/='
| '&&='
| '&='
| '%='
| '^='
| '+='
| '<<='
| '='
| '>>='
| '>>>='
| '|='
| '||=';
}

export interface AssignmentPattern extends BaseNode {
Expand Down
15 changes: 8 additions & 7 deletions packages/typescript-estree/tests/ast-alignment/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ function createError(
function parseWithBabelParser(text: string, jsx = true): any {
const babel = require('@babel/parser');
const plugins: ParserPlugin[] = [
'typescript',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'asyncGenerators',
'bigInt',
'classProperties',
'decorators-legacy',
'dynamicImport',
'estree',
'bigInt',
'numericSeparator',
'importMeta',
'optionalChaining',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalChaining',
'typescript',
];
if (jsx) {
plugins.push('jsx');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,12 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/readonly-tuples.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/short-circuiting-assignment-and-and.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/short-circuiting-assignment-or-or.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/short-circuiting-assignment-question-question.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/symbol-type-param.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/type-alias-declaration.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Loading