Handle .mts and .cts files in @babel/preset-typescript (#13838)
This commit is contained in:
parent
381277ae35
commit
718c6cb7de
@ -64,7 +64,7 @@ function shouldIgnore(name, ignore?: Array<string>) {
|
||||
);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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 `<T,>() => ...`.",
|
||||
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<Parser>): Class<Parser> =>
|
||||
tsParseDelimitedList<T: N.Node>(
|
||||
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<Parser>): Class<Parser> =>
|
||||
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<Parser>): Class<Parser> =>
|
||||
result.push(element);
|
||||
|
||||
if (this.eat(tt.comma)) {
|
||||
trailingCommaPos = this.state.lastTokStart;
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -406,6 +416,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (refTrailingCommaPos) {
|
||||
refTrailingCommaPos.value = trailingCommaPos;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -414,6 +428,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
parseElement: () => T,
|
||||
bracket: boolean,
|
||||
skipFirstToken: boolean,
|
||||
refTrailingCommaPos?: { value: number },
|
||||
): T[] {
|
||||
if (!skipFirstToken) {
|
||||
if (bracket) {
|
||||
@ -423,7 +438,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
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<Parser>): Class<Parser> =>
|
||||
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<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
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<Parser>): Class<Parser> =>
|
||||
|
||||
// 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<Parser>): Class<Parser> =>
|
||||
}, 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 <T>(x) => 2, we don't consider <T>(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<Parser>): Class<Parser> =>
|
||||
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<Parser>): Class<Parser> =>
|
||||
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("<")) {
|
||||
|
||||
3
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["typescript", { "disallowAmbiguousJSXLike": true }]]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T>x;
|
||||
38
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/output.json
vendored
Normal file
38
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-assertion/output.json
vendored
Normal file
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
<T>() => 1;
|
||||
<T>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false
|
||||
}
|
||||
@ -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 `<T,>() => ...`. (1:0)",
|
||||
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
<T,>() => 1;
|
||||
<T,>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": false
|
||||
}
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
<T,>() => 1;
|
||||
<T,>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true
|
||||
}
|
||||
@ -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": []
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
<T>() => 1;
|
||||
<T>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"BABEL_8_BREAKING": true
|
||||
}
|
||||
95
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json
vendored
Normal file
95
packages/babel-parser/test/fixtures/typescript/disallow-jsx-ambiguity/type-parameter/output.json
vendored
Normal file
@ -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 `<T,>() => ...`. (1:0)",
|
||||
"SyntaxError: This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (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": []
|
||||
}
|
||||
}
|
||||
@ -185,6 +185,7 @@ export interface FlowPluginOptions {
|
||||
|
||||
export interface TypeScriptPluginOptions {
|
||||
dts?: boolean;
|
||||
disallowAmbiguousJSXLike?: boolean;
|
||||
}
|
||||
|
||||
export const tokTypes: {
|
||||
|
||||
@ -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"
|
||||
|
||||
@ -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
|
||||
|
||||
3
packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/options.json
vendored
Normal file
3
packages/babel-plugin-syntax-typescript/test/fixtures/disallow-jsx-ambiguity/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": [["syntax-typescript", { "disallowAmbiguousJSXLike": true }]]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T>x;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)"
|
||||
}
|
||||
@ -0,0 +1,2 @@
|
||||
<T,>() => 1;
|
||||
<T,>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
<T>() => 1;
|
||||
|
||||
<T>(x) => 1;
|
||||
@ -0,0 +1,2 @@
|
||||
<T>() => 1;
|
||||
<T>(x) => 1;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (1:0)"
|
||||
}
|
||||
3
packages/babel-plugin-syntax-typescript/test/index.js
Normal file
3
packages/babel-plugin-syntax-typescript/test/index.js
Normal file
@ -0,0 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(import.meta.url);
|
||||
@ -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
|
||||
: // 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)]],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@ -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,
|
||||
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/input.cts
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/input.cts
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "x";
|
||||
3
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json
vendored
Normal file
3
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-cts/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
|
||||
}
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/input.mts
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/input.mts
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "x";
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/output.mjs
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/import-in-mts/output.mjs
vendored
Normal file
@ -0,0 +1 @@
|
||||
import "x";
|
||||
4
packages/babel-preset-typescript/test/fixtures/node-extensions/options.json
vendored
Normal file
4
packages/babel-preset-typescript/test/fixtures/node-extensions/options.json
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"DO_NOT_SET_SOURCE_TYPE": true,
|
||||
"presets": ["typescript"]
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T> x;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T> x;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Use an `as` expression instead. (1:0)"
|
||||
}
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/input.ts
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/type-assertion-in-ts/input.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
<T> x;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"DO_NOT_SET_SOURCE_TYPE": false
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
x;
|
||||
@ -0,0 +1 @@
|
||||
<T>() => 0;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (1:0)"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T>() => 0;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "This syntax is reserved in files with the .mts or .cts extension. Add a trailing comma, as in `<T,>() => ...`. (1:0)"
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
<T>() => 0;
|
||||
@ -0,0 +1,3 @@
|
||||
{
|
||||
"DO_NOT_SET_SOURCE_TYPE": false
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
() => 0;
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/input.cts
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/input.cts
vendored
Normal file
@ -0,0 +1 @@
|
||||
with (x) {}
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/output.js
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-cts/output.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
with (x) {}
|
||||
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/input.mts
vendored
Normal file
1
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/input.mts
vendored
Normal file
@ -0,0 +1 @@
|
||||
with (x) {}
|
||||
3
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/options.json
vendored
Normal file
3
packages/babel-preset-typescript/test/fixtures/node-extensions/with-in-mts/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "'with' in strict mode. (1:0)"
|
||||
}
|
||||
@ -30,16 +30,17 @@ describe("normalize options", () => {
|
||||
});
|
||||
it("default values", () => {
|
||||
expect(normalizeOptions({})).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
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 {
|
||||
Object {
|
||||
"allExtensions": false,
|
||||
"allowNamespaces": true,
|
||||
"disallowAmbiguousJSXLike": false,
|
||||
"isTSX": false,
|
||||
"jsxPragma": undefined,
|
||||
"jsxPragmaFrag": "React.Fragment",
|
||||
"onlyRemoveTypeImports": undefined,
|
||||
"optimizeConstEnums": false,
|
||||
}
|
||||
`);
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -2021,6 +2021,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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user