diff --git a/packages/babel-helper-fixtures/src/index.ts b/packages/babel-helper-fixtures/src/index.ts index 0bdda5ab40e6..59a9c08b1d8d 100644 --- a/packages/babel-helper-fixtures/src/index.ts +++ b/packages/babel-helper-fixtures/src/index.ts @@ -64,7 +64,7 @@ function shouldIgnore(name, ignore?: Array) { ); } -const EXTENSIONS = [".js", ".mjs", ".ts", ".tsx"]; +const EXTENSIONS = [".js", ".mjs", ".ts", ".tsx", ".cts", ".mts"]; function findFile(filepath: string, allowJSON?: boolean) { const matches = []; @@ -134,6 +134,7 @@ function pushTask(taskName, taskDir, suite, suiteName) { ? taskOpts.BABEL_8_BREAKING === false : taskOpts.BABEL_8_BREAKING === true), options: taskOpts, + doNotSetSourceType: taskOpts.DO_NOT_SET_SOURCE_TYPE, externalHelpers: taskOpts.externalHelpers ?? !!tryResolve("@babel/plugin-external-helpers"), @@ -162,6 +163,7 @@ function pushTask(taskName, taskDir, suite, suiteName) { }; delete taskOpts.BABEL_8_BREAKING; + delete taskOpts.DO_NOT_SET_SOURCE_TYPE; // If there's node requirement, check it before pushing task if (taskOpts.minNodeVersion) { diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.ts b/packages/babel-helper-transform-fixture-test-runner/src/index.ts index cfbe4fd9283c..33c2c235994b 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.ts +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.ts @@ -230,6 +230,7 @@ function run(task) { expect: expected, exec, options: opts, + doNotSetSourceType, optionsDir, validateLogs, ignoreOutput, @@ -245,7 +246,7 @@ function run(task) { filename: self.loc, filenameRelative: self.filename, sourceFileName: self.filename, - sourceType: "script", + ...(doNotSetSourceType ? {} : { sourceType: "script" }), babelrc: false, configFile: false, inputSourceMap: task.inputSourceMap || undefined, diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 7824e5b37052..8dc67d9d73bb 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -135,6 +135,10 @@ const TSErrors = makeErrorTemplates( "Private elements cannot have an accessibility modifier ('%0').", ReadonlyForMethodSignature: "'readonly' modifier can only appear on a property declaration or index signature.", + ReservedArrowTypeParam: + "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`.", + ReservedTypeAssertion: + "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead.", SetAccesorCannotHaveOptionalParameter: "A 'set' accessor cannot have an optional parameter.", SetAccesorCannotHaveRestParameter: @@ -359,12 +363,14 @@ export default (superClass: Class): Class => tsParseDelimitedList( kind: ParsingContext, parseElement: () => T, + refTrailingCommaPos?: { value: number }, ): T[] { return nonNull( this.tsParseDelimitedListWorker( kind, parseElement, /* expectSuccess */ true, + refTrailingCommaPos, ), ); } @@ -377,13 +383,16 @@ export default (superClass: Class): Class => kind: ParsingContext, parseElement: () => ?T, expectSuccess: boolean, + refTrailingCommaPos?: { value: number }, ): ?(T[]) { const result = []; + let trailingCommaPos = -1; for (;;) { if (this.tsIsListTerminator(kind)) { break; } + trailingCommaPos = -1; const element = parseElement(); if (element == null) { @@ -392,6 +401,7 @@ export default (superClass: Class): Class => result.push(element); if (this.eat(tt.comma)) { + trailingCommaPos = this.state.lastTokStart; continue; } @@ -406,6 +416,10 @@ export default (superClass: Class): Class => return undefined; } + if (refTrailingCommaPos) { + refTrailingCommaPos.value = trailingCommaPos; + } + return result; } @@ -414,6 +428,7 @@ export default (superClass: Class): Class => parseElement: () => T, bracket: boolean, skipFirstToken: boolean, + refTrailingCommaPos?: { value: number }, ): T[] { if (!skipFirstToken) { if (bracket) { @@ -423,7 +438,11 @@ export default (superClass: Class): Class => } } - const result = this.tsParseDelimitedList(kind, parseElement); + const result = this.tsParseDelimitedList( + kind, + parseElement, + refTrailingCommaPos, + ); if (bracket) { this.expect(tt.bracketR); @@ -524,15 +543,21 @@ export default (superClass: Class): Class => this.unexpected(); } + const refTrailingCommaPos = { value: -1 }; + node.params = this.tsParseBracketedList( "TypeParametersOrArguments", this.tsParseTypeParameter.bind(this), /* bracket */ false, /* skipFirstToken */ true, + refTrailingCommaPos, ); if (node.params.length === 0) { this.raise(node.start, TSErrors.EmptyTypeParameters); } + if (refTrailingCommaPos.value !== -1) { + this.addExtra(node, "trailingComma", refTrailingCommaPos.value); + } return this.finishNode(node, "TSTypeParameterDeclaration"); } @@ -1403,6 +1428,10 @@ export default (superClass: Class): Class => } tsParseTypeAssertion(): N.TsTypeAssertion { + if (this.getPluginOption("typescript", "disallowAmbiguousJSXLike")) { + this.raise(this.state.start, TSErrors.ReservedTypeAssertion); + } + const node: N.TsTypeAssertion = this.startNode(); const _const = this.tsTryNextParseConstantContext(); node.typeAnnotation = _const || this.tsNextThenParseType(); @@ -2854,7 +2883,7 @@ export default (superClass: Class): Class => // Either way, we're looking at a '<': tt.jsxTagStart or relational. - let typeParameters: N.TsTypeParameterDeclaration; + let typeParameters: ?N.TsTypeParameterDeclaration; state = state || this.state.clone(); const arrow = this.tryParse(abort => { @@ -2878,7 +2907,13 @@ export default (superClass: Class): Class => }, state); /*:: invariant(arrow.node != null) */ - if (!arrow.error && !arrow.aborted) return arrow.node; + if (!arrow.error && !arrow.aborted) { + // This error is reported outside of the this.tryParse call so that + // in case of (x) => 2, we don't consider (x) as a type assertion + // because of this error. + if (typeParameters) this.reportReservedArrowTypeParam(typeParameters); + return arrow.node; + } if (!jsx) { // Try parsing a type cast instead of an arrow function. @@ -2903,6 +2938,7 @@ export default (superClass: Class): Class => if (arrow.node) { /*:: invariant(arrow.failState) */ this.state = arrow.failState; + if (typeParameters) this.reportReservedArrowTypeParam(typeParameters); return arrow.node; } @@ -2919,6 +2955,16 @@ export default (superClass: Class): Class => throw jsx?.error || arrow.error || typeCast?.error; } + reportReservedArrowTypeParam(node: any) { + if ( + node.params.length === 1 && + !node.extra?.trailingComma && + this.getPluginOption("typescript", "disallowAmbiguousJSXLike") + ) { + this.raise(node.start, TSErrors.ReservedArrowTypeParam); + } + } + // Handle type assertions parseMaybeUnary(refExpressionErrors?: ?ExpressionErrors): N.Expression { if (!this.hasPlugin("jsx") && this.isRelational("<")) { diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/options.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/options.json new file mode 100644 index 000000000000..6cb5153c3cbc --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["typescript", { "disallowAmbiguousJSXLike": true }]] +} diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/input.ts b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/input.ts new file mode 100644 index 000000000000..68e889307fc1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/input.ts @@ -0,0 +1 @@ +x; diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/output.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/output.json new file mode 100644 index 000000000000..83a8eb229b57 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/output.json @@ -0,0 +1,38 @@ +{ + "type": "File", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "errors": [ + "SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}}, + "expression": { + "type": "TSTypeAssertion", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "typeAnnotation": { + "type": "TSTypeReference", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}}, + "typeName": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"T"}, + "name": "T" + } + }, + "expression": { + "type": "Identifier", + "start":3,"end":4,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":4},"identifierName":"x"}, + "name": "x" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/input.ts b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/input.ts new file mode 100644 index 000000000000..4b2a849ad10e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/options.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/options.json new file mode 100644 index 000000000000..29a3f0e84167 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/options.json @@ -0,0 +1,3 @@ +{ + "BABEL_8_BREAKING": false +} diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/output.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/output.json new file mode 100644 index 000000000000..c293c43bdd4a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-babel-7/output.json @@ -0,0 +1,87 @@ +{ + "type": "File", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "errors": [ + "SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (1:0)", + "SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "NumericLiteral", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "params": [ + { + "type": "TSTypeParameter", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}}, + "name": "T" + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start":12,"end":24,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":12,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5},"identifierName":"x"}, + "name": "x" + } + ], + "body": { + "type": "NumericLiteral", + "start":22,"end":23,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":12,"end":15,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "params": [ + { + "type": "TSTypeParameter", + "start":13,"end":14,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "name": "T" + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/input.ts b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/input.ts new file mode 100644 index 000000000000..d9deae0f0996 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/options.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/options.json new file mode 100644 index 000000000000..29a3f0e84167 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/options.json @@ -0,0 +1,3 @@ +{ + "BABEL_8_BREAKING": false +} diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/output.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/output.json new file mode 100644 index 000000000000..00081c305bbd --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous-babel-7/output.json @@ -0,0 +1,89 @@ +{ + "type": "File", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":13}}, + "program": { + "type": "Program", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":13}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "NumericLiteral", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "params": [ + { + "type": "TSTypeParameter", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}}, + "name": "T" + } + ], + "extra": { + "trailingComma": 2 + } + } + } + }, + { + "type": "ExpressionStatement", + "start":13,"end":26,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":13,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":18,"end":19,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"x"}, + "name": "x" + } + ], + "body": { + "type": "NumericLiteral", + "start":24,"end":25,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":13,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4}}, + "params": [ + { + "type": "TSTypeParameter", + "start":14,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "name": "T" + } + ], + "extra": { + "trailingComma": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts new file mode 100644 index 000000000000..d9deae0f0996 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/options.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/options.json new file mode 100644 index 000000000000..cbf6d1595427 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/options.json @@ -0,0 +1,3 @@ +{ + "BABEL_8_BREAKING": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/output.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/output.json new file mode 100644 index 000000000000..06565d68fb94 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter-unambiguous/output.json @@ -0,0 +1,97 @@ +{ + "type": "File", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":13}}, + "program": { + "type": "Program", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":13}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":12,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":12}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "NumericLiteral", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":0,"end":4,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, + "params": [ + { + "type": "TSTypeParameter", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}}, + "name": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"T"}, + "name": "T" + } + } + ], + "extra": { + "trailingComma": 2 + } + } + } + }, + { + "type": "ExpressionStatement", + "start":13,"end":26,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":13}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":13,"end":25,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":18,"end":19,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"x"}, + "name": "x" + } + ], + "body": { + "type": "NumericLiteral", + "start":24,"end":25,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":13,"end":17,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":4}}, + "params": [ + { + "type": "TSTypeParameter", + "start":14,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "name": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2},"identifierName":"T"}, + "name": "T" + } + } + ], + "extra": { + "trailingComma": 15 + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/input.ts b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/input.ts new file mode 100644 index 000000000000..4b2a849ad10e --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/options.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/options.json new file mode 100644 index 000000000000..cbf6d1595427 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/options.json @@ -0,0 +1,3 @@ +{ + "BABEL_8_BREAKING": true +} diff --git a/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json new file mode 100644 index 000000000000..45d8a27acad0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json @@ -0,0 +1,95 @@ +{ + "type": "File", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "errors": [ + "SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (1:0)", + "SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (2:0)" + ], + "program": { + "type": "Program", + "start":0,"end":24,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":12}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "NumericLiteral", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}}, + "params": [ + { + "type": "TSTypeParameter", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}}, + "name": { + "type": "Identifier", + "start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2},"identifierName":"T"}, + "name": "T" + } + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start":12,"end":24,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":12}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":12,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":11}}, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":16,"end":17,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":5},"identifierName":"x"}, + "name": "x" + } + ], + "body": { + "type": "NumericLiteral", + "start":22,"end":23,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start":12,"end":15,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3}}, + "params": [ + { + "type": "TSTypeParameter", + "start":13,"end":14,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2}}, + "name": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":2},"identifierName":"T"}, + "name": "T" + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index 8f16aae158aa..4c314067631b 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -185,6 +185,7 @@ export interface FlowPluginOptions { export interface TypeScriptPluginOptions { dts?: boolean; + disallowAmbiguousJSXLike?: boolean; } export const tokTypes: { diff --git a/packages/babel-plugin-syntax-typescript/package.json b/packages/babel-plugin-syntax-typescript/package.json index 5478bab68a3f..a6fdee44663f 100644 --- a/packages/babel-plugin-syntax-typescript/package.json +++ b/packages/babel-plugin-syntax-typescript/package.json @@ -24,7 +24,8 @@ "@babel/core": "^7.0.0-0" }, "devDependencies": { - "@babel/core": "workspace:^" + "@babel/core": "workspace:^", + "@babel/helper-plugin-test-runner": "workspace:^" }, "engines": { "node": ">=6.9.0" diff --git a/packages/babel-plugin-syntax-typescript/src/index.js b/packages/babel-plugin-syntax-typescript/src/index.js index bc396d99bcae..3420b6470831 100644 --- a/packages/babel-plugin-syntax-typescript/src/index.js +++ b/packages/babel-plugin-syntax-typescript/src/index.js @@ -15,7 +15,7 @@ function removePlugin(plugins, name) { } } -export default declare((api, { isTSX }) => { +export default declare((api, { isTSX, disallowAmbiguousJSXLike }) => { api.assertVersion(7); return { @@ -31,7 +31,10 @@ export default declare((api, { isTSX }) => { // in TS depends on the extensions, and is purely dependent on 'isTSX'. removePlugin(plugins, "jsx"); - parserOpts.plugins.push("typescript", "classProperties"); + parserOpts.plugins.push( + ["typescript", { disallowAmbiguousJSXLike }], + "classProperties", + ); if (!process.env.BABEL_8_BREAKING) { // This is enabled by default since @babel/parser 7.1.5 diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/options.json b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/options.json new file mode 100644 index 000000000000..747f9e6083a7 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["syntax-typescript", { "disallowAmbiguousJSXLike": true }]] +} diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/input.ts b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/input.ts new file mode 100644 index 000000000000..68e889307fc1 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/input.ts @@ -0,0 +1 @@ +x; diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/options.json b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/options.json new file mode 100644 index 000000000000..d59f14ff0b1b --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-assertion/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)" +} diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts new file mode 100644 index 000000000000..d9deae0f0996 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/output.js b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/output.js new file mode 100644 index 000000000000..26d3834d427d --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter-unambiguous/output.js @@ -0,0 +1,3 @@ +() => 1; + +(x) => 1; diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/input.ts b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/input.ts new file mode 100644 index 000000000000..4b2a849ad10e --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/input.ts @@ -0,0 +1,2 @@ +() => 1; +(x) => 1; diff --git a/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/options.json b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/options.json new file mode 100644 index 000000000000..656905ed2daf --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/type-parameter/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (1:0)" +} diff --git a/packages/babel-plugin-syntax-typescript/test/index.js b/packages/babel-plugin-syntax-typescript/test/index.js new file mode 100644 index 000000000000..21a55ce6b5e7 --- /dev/null +++ b/packages/babel-plugin-syntax-typescript/test/index.js @@ -0,0 +1,3 @@ +import runner from "@babel/helper-plugin-test-runner"; + +runner(import.meta.url); diff --git a/packages/babel-preset-typescript/src/index.js b/packages/babel-preset-typescript/src/index.js index e384e41c2424..5fcefb6927ae 100644 --- a/packages/babel-preset-typescript/src/index.js +++ b/packages/babel-preset-typescript/src/index.js @@ -8,6 +8,7 @@ export default declare((api, opts) => { const { allExtensions, allowNamespaces, + disallowAmbiguousJSXLike, isTSX, jsxPragma, jsxPragmaFrag, @@ -16,17 +17,19 @@ export default declare((api, opts) => { } = normalizeOptions(opts); const pluginOptions = process.env.BABEL_8_BREAKING - ? isTSX => ({ + ? (isTSX, disallowAmbiguousJSXLike) => ({ allowNamespaces, + disallowAmbiguousJSXLike, isTSX, jsxPragma, jsxPragmaFrag, onlyRemoveTypeImports, optimizeConstEnums, }) - : isTSX => ({ + : (isTSX, disallowAmbiguousJSXLike) => ({ allowDeclareFields: opts.allowDeclareFields, allowNamespaces, + disallowAmbiguousJSXLike, isTSX, jsxPragma, jsxPragmaFrag, @@ -38,21 +41,36 @@ export default declare((api, opts) => { overrides: allExtensions ? [ { - plugins: [[transformTypeScript, pluginOptions(isTSX)]], + plugins: [ + [ + transformTypeScript, + pluginOptions(isTSX, disallowAmbiguousJSXLike), + ], + ], }, ] - : [ + : // Only set 'test' if explicitly requested, since it requires that + // Babel is being called` + [ { - // Only set 'test' if explicitly requested, since it requires that - // Babel is being called` test: /\.ts$/, - plugins: [[transformTypeScript, pluginOptions(false)]], + plugins: [[transformTypeScript, pluginOptions(false, false)]], + }, + { + test: /\.mts$/, + sourceType: "module", + plugins: [[transformTypeScript, pluginOptions(false, true)]], + }, + { + test: /\.cts$/, + sourceType: "script", + plugins: [[transformTypeScript, pluginOptions(false, true)]], }, { - // Only set 'test' if explicitly requested, since it requires that - // Babel is being called` test: /\.tsx$/, - plugins: [[transformTypeScript, pluginOptions(true)]], + // disallowAmbiguousJSXLike is a no-op when parsing TSX, since it's + // always disallowed. + plugins: [[transformTypeScript, pluginOptions(true, false)]], }, ], }; diff --git a/packages/babel-preset-typescript/src/normalize-options.js b/packages/babel-preset-typescript/src/normalize-options.js index 6812fac324b1..9d93723a5864 100644 --- a/packages/babel-preset-typescript/src/normalize-options.js +++ b/packages/babel-preset-typescript/src/normalize-options.js @@ -7,6 +7,7 @@ export default function normalizeOptions(options = {}) { const TopLevelOptions = { allExtensions: "allExtensions", allowNamespaces: "allowNamespaces", + disallowAmbiguousJSXLike: "disallowAmbiguousJSXLike", isTSX: "isTSX", jsxPragma: "jsxPragma", jsxPragmaFrag: "jsxPragmaFrag", @@ -54,6 +55,18 @@ export default function normalizeOptions(options = {}) { v.invariant(allExtensions, "isTSX:true requires allExtensions:true"); } + const disallowAmbiguousJSXLike = v.validateBooleanOption( + TopLevelOptions.disallowAmbiguousJSXLike, + options.disallowAmbiguousJSXLike, + false, + ); + if (disallowAmbiguousJSXLike) { + v.invariant( + allExtensions, + "disallowAmbiguousJSXLike:true requires allExtensions:true", + ); + } + const optimizeConstEnums = v.validateBooleanOption( TopLevelOptions.optimizeConstEnums, options.optimizeConstEnums, @@ -63,6 +76,7 @@ export default function normalizeOptions(options = {}) { return { allExtensions, allowNamespaces, + disallowAmbiguousJSXLike, isTSX, jsxPragma, jsxPragmaFrag, diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/input.cts b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/input.cts new file mode 100644 index 000000000000..856f26b34c30 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/input.cts @@ -0,0 +1 @@ +import "x"; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json new file mode 100644 index 000000000000..afbed324e92a --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)" +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/input.mts b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/input.mts new file mode 100644 index 000000000000..856f26b34c30 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/input.mts @@ -0,0 +1 @@ +import "x"; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/output.mjs b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/output.mjs new file mode 100644 index 000000000000..856f26b34c30 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/output.mjs @@ -0,0 +1 @@ +import "x"; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/options.json new file mode 100644 index 000000000000..d1e62be8eb76 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/options.json @@ -0,0 +1,4 @@ +{ + "DO_NOT_SET_SOURCE_TYPE": true, + "presets": ["typescript"] +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/input.cts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/input.cts new file mode 100644 index 000000000000..35a8b2e1277f --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/input.cts @@ -0,0 +1 @@ + x; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/options.json new file mode 100644 index 000000000000..d59f14ff0b1b --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-cts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)" +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/input.mts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/input.mts new file mode 100644 index 000000000000..35a8b2e1277f --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/input.mts @@ -0,0 +1 @@ + x; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/options.json new file mode 100644 index 000000000000..d59f14ff0b1b --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-mts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)" +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/input.ts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/input.ts new file mode 100644 index 000000000000..35a8b2e1277f --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/input.ts @@ -0,0 +1 @@ + x; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/options.json new file mode 100644 index 000000000000..55a498f5e8b2 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/options.json @@ -0,0 +1,3 @@ +{ + "DO_NOT_SET_SOURCE_TYPE": false +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/output.js b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/output.js new file mode 100644 index 000000000000..6c650fc54be1 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/output.js @@ -0,0 +1 @@ +x; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/input.mts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/input.mts new file mode 100644 index 000000000000..c6c773b424dd --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/input.mts @@ -0,0 +1 @@ +() => 0; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/options.json new file mode 100644 index 000000000000..656905ed2daf --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-cts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (1:0)" +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/input.mts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/input.mts new file mode 100644 index 000000000000..c6c773b424dd --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/input.mts @@ -0,0 +1 @@ +() => 0; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/options.json new file mode 100644 index 000000000000..656905ed2daf --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-mts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `() => ...`. (1:0)" +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/input.ts b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/input.ts new file mode 100644 index 000000000000..c6c773b424dd --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/input.ts @@ -0,0 +1 @@ +() => 0; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/options.json new file mode 100644 index 000000000000..55a498f5e8b2 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/options.json @@ -0,0 +1,3 @@ +{ + "DO_NOT_SET_SOURCE_TYPE": false +} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/output.js b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/output.js new file mode 100644 index 000000000000..d4bf221471c1 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/type-param-arrow-in-ts/output.js @@ -0,0 +1 @@ +() => 0; diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/input.cts b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/input.cts new file mode 100644 index 000000000000..66e645012e48 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/input.cts @@ -0,0 +1 @@ +with (x) {} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/output.js b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/output.js new file mode 100644 index 000000000000..66e645012e48 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/output.js @@ -0,0 +1 @@ +with (x) {} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/input.mts b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/input.mts new file mode 100644 index 000000000000..66e645012e48 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/input.mts @@ -0,0 +1 @@ +with (x) {} diff --git a/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/options.json b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/options.json new file mode 100644 index 000000000000..25577a776804 --- /dev/null +++ b/packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/options.json @@ -0,0 +1,3 @@ +{ + "throws": "'with' in strict mode. (1:0)" +} diff --git a/packages/babel-preset-typescript/test/normalize-options.spec.js b/packages/babel-preset-typescript/test/normalize-options.spec.js index d0a10d18087b..189af1ee3414 100644 --- a/packages/babel-preset-typescript/test/normalize-options.spec.js +++ b/packages/babel-preset-typescript/test/normalize-options.spec.js @@ -30,16 +30,17 @@ describe("normalize options", () => { }); it("default values", () => { expect(normalizeOptions({})).toMatchInlineSnapshot(` - Object { - "allExtensions": false, - "allowNamespaces": true, - "isTSX": false, - "jsxPragma": "React", - "jsxPragmaFrag": "React.Fragment", - "onlyRemoveTypeImports": true, - "optimizeConstEnums": false, - } - `); +Object { + "allExtensions": false, + "allowNamespaces": true, + "disallowAmbiguousJSXLike": false, + "isTSX": false, + "jsxPragma": "React", + "jsxPragmaFrag": "React.Fragment", + "onlyRemoveTypeImports": true, + "optimizeConstEnums": false, +} +`); }); }); (process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => { @@ -78,16 +79,17 @@ describe("normalize options", () => { ); it("default values", () => { expect(normalizeOptions({})).toMatchInlineSnapshot(` - Object { - "allExtensions": false, - "allowNamespaces": true, - "isTSX": false, - "jsxPragma": undefined, - "jsxPragmaFrag": "React.Fragment", - "onlyRemoveTypeImports": undefined, - "optimizeConstEnums": false, - } - `); +Object { + "allExtensions": false, + "allowNamespaces": true, + "disallowAmbiguousJSXLike": false, + "isTSX": false, + "jsxPragma": undefined, + "jsxPragmaFrag": "React.Fragment", + "onlyRemoveTypeImports": undefined, + "optimizeConstEnums": false, +} +`); }); }); }); diff --git a/yarn.lock b/yarn.lock index 33a241a4ee82..0a908eb94358 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2020,6 +2020,7 @@ __metadata: resolution: "@babel/plugin-syntax-typescript@workspace:packages/babel-plugin-syntax-typescript" dependencies: "@babel/core": "workspace:^" + "@babel/helper-plugin-test-runner": "workspace:^" "@babel/helper-plugin-utils": "workspace:^" peerDependencies: "@babel/core": ^7.0.0-0