Refactor isImportTypeOnly helper function (#10047)
This commit is contained in:
parent
3144ed131a
commit
84961ebb05
@ -17,12 +17,7 @@ function isInType(path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface State {
|
|
||||||
programPath: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
const PARSED_PARAMS = new WeakSet();
|
const PARSED_PARAMS = new WeakSet();
|
||||||
const PRAGMA_KEY = "@babel/plugin-transform-typescript/jsxPragma";
|
|
||||||
|
|
||||||
export default declare((api, { jsxPragma = "React" }) => {
|
export default declare((api, { jsxPragma = "React" }) => {
|
||||||
api.assertVersion(7);
|
api.assertVersion(7);
|
||||||
@ -39,16 +34,15 @@ export default declare((api, { jsxPragma = "React" }) => {
|
|||||||
Identifier: visitPattern,
|
Identifier: visitPattern,
|
||||||
RestElement: visitPattern,
|
RestElement: visitPattern,
|
||||||
|
|
||||||
Program(path, state: State) {
|
Program(path, state) {
|
||||||
state.programPath = path;
|
|
||||||
|
|
||||||
const { file } = state;
|
const { file } = state;
|
||||||
|
let fileJsxPragma = null;
|
||||||
|
|
||||||
if (file.ast.comments) {
|
if (file.ast.comments) {
|
||||||
for (const comment of (file.ast.comments: Array<Object>)) {
|
for (const comment of (file.ast.comments: Array<Object>)) {
|
||||||
const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
|
const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
|
||||||
if (jsxMatches) {
|
if (jsxMatches) {
|
||||||
file.set(PRAGMA_KEY, jsxMatches[1]);
|
fileJsxPragma = jsxMatches[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +70,11 @@ export default declare((api, { jsxPragma = "React" }) => {
|
|||||||
// import anyway.
|
// import anyway.
|
||||||
if (
|
if (
|
||||||
binding &&
|
binding &&
|
||||||
isImportTypeOnly(file, binding, state.programPath)
|
isImportTypeOnly({
|
||||||
|
binding,
|
||||||
|
programPath: path,
|
||||||
|
jsxPragma: fileJsxPragma || jsxPragma,
|
||||||
|
})
|
||||||
) {
|
) {
|
||||||
importsToRemove.push(binding.path);
|
importsToRemove.push(binding.path);
|
||||||
} else {
|
} 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.
|
// '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) {
|
for (const path of binding.referencePaths) {
|
||||||
if (!isInType(path)) {
|
if (!isInType(path)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileJsxPragma = file.get(PRAGMA_KEY) || jsxPragma;
|
if (binding.identifier.name !== jsxPragma) {
|
||||||
if (binding.identifier.name !== fileJsxPragma) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user