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
Refactor isImportTypeOnly helper function #10047
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,7 @@ function isInType(path) { | |
} | ||
} | ||
|
||
interface State { | ||
programPath: any; | ||
} | ||
|
||
const PARSED_PARAMS = new WeakSet(); | ||
const PRAGMA_KEY = "@babel/plugin-transform-typescript/jsxPragma"; | ||
|
||
export default declare((api, { jsxPragma = "React" }) => { | ||
api.assertVersion(7); | ||
|
@@ -39,16 +34,15 @@ export default declare((api, { jsxPragma = "React" }) => { | |
Identifier: visitPattern, | ||
RestElement: visitPattern, | ||
|
||
Program(path, state: State) { | ||
state.programPath = path; | ||
|
||
Program(path, state) { | ||
const { file } = state; | ||
let fileJsxPragma = null; | ||
|
||
if (file.ast.comments) { | ||
for (const comment of (file.ast.comments: Array<Object>)) { | ||
const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value); | ||
if (jsxMatches) { | ||
file.set(PRAGMA_KEY, jsxMatches[1]); | ||
fileJsxPragma = jsxMatches[1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of storing this in a shared mutable state under some artificial key I think it's better to just pass it as explicit argument to |
||
} | ||
} | ||
} | ||
|
@@ -76,7 +70,11 @@ export default declare((api, { jsxPragma = "React" }) => { | |
// import anyway. | ||
if ( | ||
binding && | ||
isImportTypeOnly(file, binding, state.programPath) | ||
isImportTypeOnly({ | ||
binding, | ||
programPath: path, | ||
jsxPragma: fileJsxPragma || jsxPragma, | ||
}) | ||
) { | ||
importsToRemove.push(binding.path); | ||
} else { | ||
|
@@ -327,15 +325,14 @@ export default declare((api, { jsxPragma = "React" }) => { | |
// 'access' and 'readonly' are only for parameter properties, so constructor visitor will handle them. | ||
} | ||
|
||
function isImportTypeOnly(file, binding, programPath) { | ||
function isImportTypeOnly({ binding, programPath, jsxPragma }) { | ||
for (const path of binding.referencePaths) { | ||
if (!isInType(path)) { | ||
return false; | ||
} | ||
} | ||
|
||
const fileJsxPragma = file.get(PRAGMA_KEY) || jsxPragma; | ||
if (binding.identifier.name !== fileJsxPragma) { | ||
if (binding.identifier.name !== jsxPragma) { | ||
return true; | ||
} | ||
|
||
|
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.
this was some leftover, not used anymore