mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 15:33:56 +08:00
refactor(js): share the type-only import predicate (#17331)
This commit is contained in:
@@ -8,7 +8,7 @@ import { stripQueryAndFragment } from "../artifactPathSyntax.js";
|
||||
import type { ElectronNativeAddonBindingFinding } from "./electronStaticAnalysisTypes.js";
|
||||
import {
|
||||
isTypeOnlyExportSpecifier,
|
||||
isTypeOnlyImportSpecifier,
|
||||
isTypeOnlyImport,
|
||||
semanticStaticModuleSource,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
import { addLocatedFinding } from "./javascriptStaticAnalysisFindings.js";
|
||||
@@ -107,8 +107,7 @@ const inspectStaticBinding = (
|
||||
let hasRuntimeSpecifier = node.specifiers.length === 0;
|
||||
for (const specifier of node.specifiers) {
|
||||
if (
|
||||
(t.isImportSpecifier(specifier) &&
|
||||
isTypeOnlyImportSpecifier(specifier)) ||
|
||||
(t.isImportSpecifier(specifier) && isTypeOnlyImport(specifier)) ||
|
||||
(t.isExportSpecifier(specifier) && isTypeOnlyExportSpecifier(specifier))
|
||||
)
|
||||
continue;
|
||||
|
||||
@@ -20,9 +20,9 @@ export const isUnmodeledJavaScriptRuntimeScope = (node: t.Node): boolean =>
|
||||
export const TYPE_ONLY_MODULE_SIDE_EFFECT_LIMITATION =
|
||||
"Compiler options are unavailable; inline type-only imports or re-exports may preserve a module side-effect dependency.";
|
||||
|
||||
/** Whether TypeScript explicitly erases this entire import declaration. */
|
||||
export const isTypeOnlyImportDeclaration = (
|
||||
node: t.ImportDeclaration,
|
||||
/** Whether an import declaration or binding is explicitly type-only. */
|
||||
export const isTypeOnlyImport = (
|
||||
node: t.ImportDeclaration | t.ImportSpecifier,
|
||||
): boolean => node.importKind === "type" || node.importKind === "typeof";
|
||||
|
||||
/** Whether TypeScript explicitly erases this entire export declaration. */
|
||||
@@ -39,7 +39,7 @@ export const semanticStaticModuleSource = (
|
||||
node: t.Node,
|
||||
): t.StringLiteral | null => {
|
||||
if (t.isImportDeclaration(node))
|
||||
return isTypeOnlyImportDeclaration(node) ? null : node.source;
|
||||
return isTypeOnlyImport(node) ? null : node.source;
|
||||
if (t.isExportAllDeclaration(node) || t.isExportNamedDeclaration(node))
|
||||
return isTypeOnlyExportDeclaration(node) ? null : (node.source ?? null);
|
||||
return null;
|
||||
@@ -58,10 +58,6 @@ export const semanticImportEqualsSpecifier = (
|
||||
? node.moduleReference.expression.value
|
||||
: null;
|
||||
|
||||
/** Whether this import binding is explicitly type-only. */
|
||||
export const isTypeOnlyImportSpecifier = (node: t.ImportSpecifier): boolean =>
|
||||
node.importKind === "type" || node.importKind === "typeof";
|
||||
|
||||
/** Whether this exported binding is explicitly type-only. */
|
||||
export const isTypeOnlyExportSpecifier = (node: t.ExportSpecifier): boolean =>
|
||||
node.exportKind === "type";
|
||||
@@ -101,11 +97,11 @@ export const semanticRuntimeExportValueSources = (
|
||||
export const hasOnlyInlineTypeImportSpecifiers = (
|
||||
node: t.ImportDeclaration,
|
||||
): boolean =>
|
||||
!isTypeOnlyImportDeclaration(node) &&
|
||||
!isTypeOnlyImport(node) &&
|
||||
node.specifiers.length > 0 &&
|
||||
node.specifiers.every(
|
||||
(specifier) =>
|
||||
t.isImportSpecifier(specifier) && isTypeOnlyImportSpecifier(specifier),
|
||||
t.isImportSpecifier(specifier) && isTypeOnlyImport(specifier),
|
||||
);
|
||||
|
||||
/** A regular re-export whose bindings are all erased but may retain side effects. */
|
||||
|
||||
@@ -113,8 +113,7 @@ import { isUnmodifiedGlobalReference } from "./javascriptSemanticGlobalReference
|
||||
import {
|
||||
hasUncertainTypeOnlyModuleSideEffect,
|
||||
TYPE_ONLY_MODULE_SIDE_EFFECT_LIMITATION,
|
||||
isTypeOnlyImportDeclaration,
|
||||
isTypeOnlyImportSpecifier,
|
||||
isTypeOnlyImport,
|
||||
semanticImportEqualsSpecifier,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
import {
|
||||
@@ -2260,10 +2259,9 @@ const bindImports = (
|
||||
scope: JavaScriptSemanticScopeState,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): void => {
|
||||
if (isTypeOnlyImportDeclaration(node)) return;
|
||||
if (isTypeOnlyImport(node)) return;
|
||||
for (const specifier of node.specifiers) {
|
||||
if (t.isImportSpecifier(specifier) && isTypeOnlyImportSpecifier(specifier))
|
||||
continue;
|
||||
if (t.isImportSpecifier(specifier) && isTypeOnlyImport(specifier)) continue;
|
||||
const importedPath = t.isImportDefaultSpecifier(specifier)
|
||||
? ["default"]
|
||||
: t.isImportNamespaceSpecifier(specifier)
|
||||
|
||||
@@ -37,8 +37,7 @@ import {
|
||||
isErasedJavaScriptDeclaration,
|
||||
isTypeOnlyExportDeclaration,
|
||||
isTypeOnlyExportSpecifier,
|
||||
isTypeOnlyImportDeclaration,
|
||||
isTypeOnlyImportSpecifier,
|
||||
isTypeOnlyImport,
|
||||
semanticImportEqualsSpecifier,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
import {
|
||||
@@ -366,7 +365,7 @@ const collectImports = (
|
||||
addModuleLink(
|
||||
state,
|
||||
{
|
||||
kind: isTypeOnlyImportDeclaration(node) ? "type-import" : "import",
|
||||
kind: isTypeOnlyImport(node) ? "type-import" : "import",
|
||||
specifier: node.source.value,
|
||||
importedName: null,
|
||||
localName: null,
|
||||
@@ -380,9 +379,8 @@ const collectImports = (
|
||||
state,
|
||||
{
|
||||
kind:
|
||||
isTypeOnlyImportDeclaration(node) ||
|
||||
(t.isImportSpecifier(specifier) &&
|
||||
isTypeOnlyImportSpecifier(specifier))
|
||||
isTypeOnlyImport(node) ||
|
||||
(t.isImportSpecifier(specifier) && isTypeOnlyImport(specifier))
|
||||
? "type-import"
|
||||
: "import",
|
||||
specifier: node.source.value,
|
||||
|
||||
@@ -3,8 +3,7 @@ import {
|
||||
isErasedJavaScriptDeclaration,
|
||||
isTypeOnlyExportDeclaration,
|
||||
isTypeOnlyExportSpecifier,
|
||||
isTypeOnlyImportDeclaration,
|
||||
isTypeOnlyImportSpecifier,
|
||||
isTypeOnlyImport,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
|
||||
/** Whether this child and its descendants contribute no JavaScript value. */
|
||||
@@ -23,10 +22,10 @@ export const isErasedJavaScriptValueChild = (
|
||||
t.isTSClassImplements(node) ||
|
||||
t.isTSInterfaceHeritage(node) ||
|
||||
t.isTSInterfaceBody(node) ||
|
||||
(t.isImportDeclaration(node) && isTypeOnlyImportDeclaration(node)) ||
|
||||
(t.isImportDeclaration(node) && isTypeOnlyImport(node)) ||
|
||||
((t.isExportNamedDeclaration(node) || t.isExportAllDeclaration(node)) &&
|
||||
isTypeOnlyExportDeclaration(node)) ||
|
||||
(t.isImportSpecifier(node) && isTypeOnlyImportSpecifier(node)) ||
|
||||
(t.isImportSpecifier(node) && isTypeOnlyImport(node)) ||
|
||||
(t.isExportSpecifier(node) && isTypeOnlyExportSpecifier(node)) ||
|
||||
(t.isTSImportEqualsDeclaration(node) && node.importKind === "type")
|
||||
)
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
hasOnlyInlineTypeExportSpecifiers,
|
||||
hasOnlyInlineTypeImportSpecifiers,
|
||||
isTypeOnlyExportDeclaration,
|
||||
isTypeOnlyImportDeclaration,
|
||||
isTypeOnlyImport,
|
||||
isErasedJavaScriptDeclaration,
|
||||
semanticImportEqualsSpecifier,
|
||||
} from "./javascriptModuleDeclaration.js";
|
||||
@@ -130,8 +130,7 @@ const hasRuntimeModuleDeclaration = (program: t.Program): boolean =>
|
||||
return semanticImportEqualsSpecifier(node) !== null;
|
||||
if (t.isImportDeclaration(node))
|
||||
return (
|
||||
!isTypeOnlyImportDeclaration(node) &&
|
||||
!hasOnlyInlineTypeImportSpecifiers(node)
|
||||
!isTypeOnlyImport(node) && !hasOnlyInlineTypeImportSpecifiers(node)
|
||||
);
|
||||
if (t.isExportNamedDeclaration(node) || t.isExportAllDeclaration(node))
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user