Repository navigation
Verify that files are .ts/.tsx before treating as Typescript files. #7955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
loganfsmyth
merged 4 commits into
babel:master
from
loganfsmyth:typescript-filename-required
May 23, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ca1c98b
Allow .overrides and .env inside presets.
loganfsmyth 43aa61d
Make the typescript preset require a filename unless the user configu…
loganfsmyth cc5aeb3
Allow .ts and .tsx test fixtures.
loganfsmyth f2882d5
Tests for react/ts compat and flow/ts compat.
loganfsmyth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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"); | ||
| } | ||
|
|
||
| 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo 🙊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has been changed 06e5c46