-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7955 from loganfsmyth/typescript-filename-required
Verify that files are .ts/.tsx before treating as Typescript files.
- Loading branch information
Showing
35 changed files
with
235 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,43 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
|
||
export default declare(api => { | ||
function removePlugin(plugins, name) { | ||
const indices = []; | ||
plugins.forEach((plugin, i) => { | ||
const n = Array.isArray(plugin) ? plugin[0] : plugin; | ||
|
||
if (n === name) { | ||
indices.unshift(i); | ||
} | ||
}); | ||
|
||
for (const i of indices) { | ||
plugins.splice(i, 1); | ||
} | ||
} | ||
|
||
export default declare((api, { isTSX }) => { | ||
api.assertVersion(7); | ||
|
||
return { | ||
manipulateOptions(opts, parserOpts) { | ||
const { plugins } = parserOpts; | ||
// If the Flow syntax plugin already ran, remove it since Typescript | ||
// takes priority. | ||
removePlugin(plugins, "flow"); | ||
|
||
// If the JSX syntax plugin already ran, remomove it because JSX handling | ||
// in TS depends on the extensions, and is purely dependent on 'isTSX'. | ||
removePlugin(plugins, "jsx"); | ||
|
||
parserOpts.plugins.push( | ||
"typescript", | ||
"objectRestSpread", | ||
"classProperties", | ||
); | ||
|
||
if (isTSX) { | ||
parserOpts.plugins.push("jsx"); | ||
} | ||
}, | ||
}; | ||
}); |
2 changes: 1 addition & 1 deletion
2
...ages/babel-plugin-transform-typescript/test/fixtures/imports/elide-preact-no/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-jsx", ["transform-typescript", { "jsxPragma": "h" }]] | ||
"plugins": [["transform-typescript", { "jsxPragma": "h", "isTSX": true }]] | ||
} |
2 changes: 1 addition & 1 deletion
2
...ges/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no-2/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-jsx", "transform-typescript"] | ||
"plugins": [["transform-typescript", { "isTSX": true }]] | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-react-no/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"plugins": ["syntax-jsx", "transform-typescript"] | ||
"plugins": [["transform-typescript", { "isTSX": true }]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,42 @@ | ||
import { declare } from "@babel/helper-plugin-utils"; | ||
import transformTypeScript from "@babel/plugin-transform-typescript"; | ||
|
||
export default declare((api, { jsxPragma }) => { | ||
api.assertVersion(7); | ||
export default declare( | ||
(api, { jsxPragma, allExtensions = false, isTSX = false }) => { | ||
api.assertVersion(7); | ||
|
||
return { | ||
plugins: [[transformTypeScript, { jsxPragma }]], | ||
}; | ||
}); | ||
if (typeof allExtensions !== "boolean") { | ||
throw new Error(".allExtensions must be a boolean, or undefined"); | ||
} | ||
if (typeof isTSX !== "boolean") { | ||
throw new Error(".allExtensions must be a boolean, or undefined"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
loganfsmyth
Author
Member
|
||
} | ||
|
||
if (isTSX && !allExtensions) { | ||
throw new Error("isTSX:true requires allExtensions:true"); | ||
} | ||
|
||
return { | ||
overrides: allExtensions | ||
? [ | ||
{ | ||
plugins: [[transformTypeScript, { jsxPragma, isTSX }]], | ||
}, | ||
] | ||
: [ | ||
{ | ||
// Only set 'test' if explicitly requested, since it requires that | ||
// Babel is being called` | ||
test: /\.ts$/, | ||
plugins: [[transformTypeScript, { jsxPragma }]], | ||
}, | ||
{ | ||
// Only set 'test' if explicitly requested, since it requires that | ||
// Babel is being called` | ||
test: /\.tsx$/, | ||
plugins: [[transformTypeScript, { jsxPragma, isTSX: true }]], | ||
}, | ||
], | ||
}; | ||
}, | ||
); |
5 changes: 5 additions & 0 deletions
5
packages/babel-preset-typescript/test/fixtures/flow-compat/js-invalid/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum Example { | ||
Value | ||
} | ||
|
||
foo; |
8 changes: 8 additions & 0 deletions
8
packages/babel-preset-typescript/test/fixtures/flow-compat/js-invalid/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ | ||
["flow", {}, "before"], | ||
"typescript", | ||
["flow", {}, "after"] | ||
], | ||
"throws": "enum is a reserved word (1:0)" | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-preset-typescript/test/fixtures/flow-compat/js-valid/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type Foo = {||}; | ||
|
||
foo; |
1 change: 1 addition & 0 deletions
1
packages/babel-preset-typescript/test/fixtures/flow-compat/js-valid/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo; |
7 changes: 7 additions & 0 deletions
7
packages/babel-preset-typescript/test/fixtures/flow-compat/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"presets": [ | ||
["flow", {}, "before"], | ||
"typescript", | ||
["flow", {}, "after"] | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-preset-typescript/test/fixtures/flow-compat/ts-invalid/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
type Foo = {||}; | ||
|
||
foo; |
8 changes: 8 additions & 0 deletions
8
packages/babel-preset-typescript/test/fixtures/flow-compat/ts-invalid/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"presets": [ | ||
["flow", {}, "before"], | ||
"typescript", | ||
["flow", {}, "after"] | ||
], | ||
"throws": "Unexpected token (1:12)" | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/babel-preset-typescript/test/fixtures/flow-compat/ts-valid/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum Example { | ||
Value | ||
} | ||
|
||
foo; |
Oops, something went wrong.
isTSX must be a boolean, or undefined