diff --git a/packages/babel-generator/test/fixtures/typescript/types-type-operator/input.js b/packages/babel-generator/test/fixtures/typescript/types-type-operator/input.js index 2027543c4e..1d23f962ef 100644 --- a/packages/babel-generator/test/fixtures/typescript/types-type-operator/input.js +++ b/packages/babel-generator/test/fixtures/typescript/types-type-operator/input.js @@ -1 +1,2 @@ let x: keyof T; +let y: unique symbol; diff --git a/packages/babel-generator/test/fixtures/typescript/types-type-operator/output.js b/packages/babel-generator/test/fixtures/typescript/types-type-operator/output.js index 30a9e239e6..642d99c42b 100644 --- a/packages/babel-generator/test/fixtures/typescript/types-type-operator/output.js +++ b/packages/babel-generator/test/fixtures/typescript/types-type-operator/output.js @@ -1 +1,2 @@ -let x: keyof T; \ No newline at end of file +let x: keyof T; +let y: unique symbol; \ No newline at end of file diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index 3116d52a70..693edfe59f 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -609,8 +609,8 @@ export default (superClass: Class): Class => return type; } - tsParseTypeOperator(operator: "keyof"): N.TsTypeOperator { - const node = this.startNode(); + tsParseTypeOperator(operator: "keyof" | "unique"): N.TsTypeOperator { + const node: N.TsTypeOperator = this.startNode(); this.expectContextual(operator); node.operator = operator; node.typeAnnotation = this.tsParseTypeOperatorOrHigher(); @@ -618,10 +618,10 @@ export default (superClass: Class): Class => } tsParseTypeOperatorOrHigher(): N.TsType { - if (this.isContextual("keyof")) { - return this.tsParseTypeOperator("keyof"); - } - return this.tsParseArrayTypeOrHigher(); + const operator = ["keyof", "unique"].find(kw => this.isContextual(kw)); + return operator + ? this.tsParseTypeOperator(operator) + : this.tsParseArrayTypeOrHigher(); } tsParseUnionOrIntersectionType( diff --git a/packages/babylon/src/types.js b/packages/babylon/src/types.js index 47b9c856e1..7b30b56604 100644 --- a/packages/babylon/src/types.js +++ b/packages/babylon/src/types.js @@ -1149,7 +1149,7 @@ export type TsParenthesizedType = TsTypeBase & { export type TsTypeOperator = TsTypeBase & { type: "TSTypeOperator", - operator: "keyof", + operator: "keyof" | "unique", typeAnnotation: TsType, }; diff --git a/packages/babylon/test/fixtures/typescript/types/type-operator/input.js b/packages/babylon/test/fixtures/typescript/types/type-operator/input.js index 2027543c4e..1d23f962ef 100644 --- a/packages/babylon/test/fixtures/typescript/types/type-operator/input.js +++ b/packages/babylon/test/fixtures/typescript/types/type-operator/input.js @@ -1 +1,2 @@ let x: keyof T; +let y: unique symbol; diff --git a/packages/babylon/test/fixtures/typescript/types/type-operator/output.json b/packages/babylon/test/fixtures/typescript/types/type-operator/output.json index 2ad318fae1..e1d2461e11 100644 --- a/packages/babylon/test/fixtures/typescript/types/type-operator/output.json +++ b/packages/babylon/test/fixtures/typescript/types/type-operator/output.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 15, + "end": 37, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 15 + "line": 2, + "column": 21 } }, "program": { "type": "Program", "start": 0, - "end": 15, + "end": 37, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, - "column": 15 + "line": 2, + "column": 21 } }, "sourceType": "module", @@ -141,6 +141,103 @@ } ], "kind": "let" + }, + { + "type": "VariableDeclaration", + "start": 16, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 21 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 20, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "id": { + "type": "Identifier", + "start": 20, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "y" + }, + "name": "y", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 21, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "typeAnnotation": { + "type": "TSTypeOperator", + "start": 23, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "operator": "unique", + "typeAnnotation": { + "type": "TSSymbolKeyword", + "start": 30, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 20 + } + } + } + } + } + }, + "init": null + } + ], + "kind": "let" } ], "directives": []