Skip to content

Commit

Permalink
Handle .mts and .cts files in @babel/preset-typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 12, 2021
1 parent 7b1127c commit c5e8c75
Show file tree
Hide file tree
Showing 28 changed files with 108 additions and 32 deletions.
4 changes: 3 additions & 1 deletion packages/babel-helper-fixtures/src/index.ts
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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) {
Expand Down
Expand Up @@ -230,6 +230,7 @@ function run(task) {
expect: expected,
exec,
options: opts,
doNotSetSourceType,
optionsDir,
validateLogs,
ignoreOutput,
Expand All @@ -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,
Expand Down
35 changes: 25 additions & 10 deletions packages/babel-preset-typescript/src/index.js
Expand Up @@ -8,6 +8,7 @@ export default declare((api, opts) => {
const {
allExtensions,
allowNamespaces,
disallowJSXAmbiguity,
isTSX,
jsxPragma,
jsxPragmaFrag,
Expand All @@ -16,17 +17,19 @@ export default declare((api, opts) => {
} = normalizeOptions(opts);

const pluginOptions = process.env.BABEL_8_BREAKING
? isTSX => ({
? (isTSX, disallowJSXAmbiguity) => ({
allowNamespaces,
disallowJSXAmbiguity,
isTSX,
jsxPragma,
jsxPragmaFrag,
onlyRemoveTypeImports,
optimizeConstEnums,
})
: isTSX => ({
: (isTSX, disallowJSXAmbiguity) => ({
allowDeclareFields: opts.allowDeclareFields,
allowNamespaces,
disallowJSXAmbiguity,
isTSX,
jsxPragma,
jsxPragmaFrag,
Expand All @@ -38,21 +41,33 @@ export default declare((api, opts) => {
overrides: allExtensions
? [
{
plugins: [[transformTypeScript, pluginOptions(isTSX)]],
plugins: [
[transformTypeScript, pluginOptions(isTSX, disallowJSXAmbiguity)],
],
},
]
: [
: // 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)]],
// disallowJSXAmbiguity is a no-op when parsing TSX, since it's
// always disallowed.
plugins: [[transformTypeScript, pluginOptions(true, false)]],
},
],
};
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-preset-typescript/src/normalize-options.js
Expand Up @@ -7,6 +7,7 @@ export default function normalizeOptions(options = {}) {
const TopLevelOptions = {
allExtensions: "allExtensions",
allowNamespaces: "allowNamespaces",
disallowJSXAmbiguity: "disallowJSXAmbiguity",
isTSX: "isTSX",
jsxPragma: "jsxPragma",
jsxPragmaFrag: "jsxPragmaFrag",
Expand Down Expand Up @@ -54,6 +55,18 @@ export default function normalizeOptions(options = {}) {
v.invariant(allExtensions, "isTSX:true requires allExtensions:true");
}

const disallowJSXAmbiguity = v.validateBooleanOption(
TopLevelOptions.disallowJSXAmbiguity,
options.disallowJSXAmbiguity,
false,
);
if (disallowJSXAmbiguity) {
v.invariant(
allExtensions,
"disallowJSXAmbiguity:true requires allExtensions:true",
);
}

const optimizeConstEnums = v.validateBooleanOption(
TopLevelOptions.optimizeConstEnums,
options.optimizeConstEnums,
Expand All @@ -63,6 +76,7 @@ export default function normalizeOptions(options = {}) {
return {
allExtensions,
allowNamespaces,
disallowJSXAmbiguity,
isTSX,
jsxPragma,
jsxPragmaFrag,
Expand Down
@@ -0,0 +1 @@
import "x";
@@ -0,0 +1,3 @@
{
"throws": "'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
}
@@ -0,0 +1 @@
import "x";
@@ -0,0 +1 @@
import "x";
@@ -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)"
}
@@ -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;
@@ -0,0 +1 @@
with (x) {}
@@ -0,0 +1 @@
with (x) {}
@@ -0,0 +1 @@
with (x) {}
@@ -0,0 +1,3 @@
{
"throws": "'with' in strict mode. (1:0)"
}
42 changes: 22 additions & 20 deletions packages/babel-preset-typescript/test/normalize-options.spec.js
Expand Up @@ -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,
"disallowJSXAmbiguity": false,
"isTSX": false,
"jsxPragma": "React",
"jsxPragmaFrag": "React.Fragment",
"onlyRemoveTypeImports": true,
"optimizeConstEnums": false,
}
`);
});
});
(process.env.BABEL_8_BREAKING ? describe.skip : describe)("Babel 7", () => {
Expand Down Expand Up @@ -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,
"disallowJSXAmbiguity": false,
"isTSX": false,
"jsxPragma": undefined,
"jsxPragmaFrag": "React.Fragment",
"onlyRemoveTypeImports": undefined,
"optimizeConstEnums": false,
}
`);
});
});
});

0 comments on commit c5e8c75

Please sign in to comment.