deslop-js 0.0.12 → 0.0.14-dev.bd94e5e
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2883 -106
- package/dist/index.d.cts +165 -2
- package/dist/index.d.mts +165 -2
- package/dist/index.mjs +2884 -107
- package/package.json +10 -1
package/dist/index.cjs
CHANGED
|
@@ -33,10 +33,10 @@ let fast_glob = require("fast-glob");
|
|
|
33
33
|
fast_glob = __toESM(fast_glob, 1);
|
|
34
34
|
let node_fs_promises = require("node:fs/promises");
|
|
35
35
|
let oxc_parser = require("oxc-parser");
|
|
36
|
-
let oxc_resolver = require("oxc-resolver");
|
|
37
|
-
let minimatch = require("minimatch");
|
|
38
36
|
let typescript = require("typescript");
|
|
39
37
|
typescript = __toESM(typescript, 1);
|
|
38
|
+
let oxc_resolver = require("oxc-resolver");
|
|
39
|
+
let minimatch = require("minimatch");
|
|
40
40
|
|
|
41
41
|
//#region src/constants.ts
|
|
42
42
|
const DEFAULT_EXTENSIONS = [
|
|
@@ -250,6 +250,7 @@ const BUILTIN_MODULES = new Set([
|
|
|
250
250
|
]);
|
|
251
251
|
const PLATFORM_SUFFIXES = [
|
|
252
252
|
".web",
|
|
253
|
+
".react-native",
|
|
253
254
|
".native",
|
|
254
255
|
".ios",
|
|
255
256
|
".android",
|
|
@@ -257,6 +258,7 @@ const PLATFORM_SUFFIXES = [
|
|
|
257
258
|
".windows",
|
|
258
259
|
".macos",
|
|
259
260
|
".any",
|
|
261
|
+
".react-server",
|
|
260
262
|
".server",
|
|
261
263
|
".client"
|
|
262
264
|
];
|
|
@@ -952,7 +954,7 @@ const visitFunctionParameters = (parameters, captures, functionName) => {
|
|
|
952
954
|
inspectTypeAnnotation(parameter.typeAnnotation, captures, "function-parameter", functionName ? `${functionName}(${parameterIdentifierName ?? "?"})` : parameterIdentifierName);
|
|
953
955
|
}
|
|
954
956
|
};
|
|
955
|
-
const visitFunctionLike = (functionNode, captures, functionName) => {
|
|
957
|
+
const visitFunctionLike$1 = (functionNode, captures, functionName) => {
|
|
956
958
|
const parameters = functionNode.params;
|
|
957
959
|
visitFunctionParameters(parameters, captures, functionName);
|
|
958
960
|
const returnTypeNode = functionNode.returnType;
|
|
@@ -967,7 +969,7 @@ const visitVariableDeclaration = (declarationNode, captures, enclosingName) => {
|
|
|
967
969
|
const declarationName = getIdentifierName(declarator.id);
|
|
968
970
|
inspectTypeAnnotation(declarator.typeAnnotation ?? (declarator.id && isOxcAstNode(declarator.id) ? declarator.id.typeAnnotation : void 0), captures, "variable-annotation", declarationName);
|
|
969
971
|
const initializerNode = declarator.init;
|
|
970
|
-
if (isOxcAstNode(initializerNode)) if (initializerNode.type === "ArrowFunctionExpression" || initializerNode.type === "FunctionExpression") visitFunctionLike(initializerNode, captures, declarationName ?? enclosingName);
|
|
972
|
+
if (isOxcAstNode(initializerNode)) if (initializerNode.type === "ArrowFunctionExpression" || initializerNode.type === "FunctionExpression") visitFunctionLike$1(initializerNode, captures, declarationName ?? enclosingName);
|
|
971
973
|
else walkExpressionForInlineTypes(initializerNode, captures, declarationName ?? enclosingName);
|
|
972
974
|
}
|
|
973
975
|
};
|
|
@@ -979,7 +981,7 @@ const walkBodyForInlineTypes = (bodyNode, captures, enclosingName, recursionDept
|
|
|
979
981
|
for (const statement of statements) {
|
|
980
982
|
if (!isOxcAstNode(statement)) continue;
|
|
981
983
|
if (statement.type === "VariableDeclaration") visitVariableDeclaration(statement, captures, enclosingName);
|
|
982
|
-
else if (statement.type === "FunctionDeclaration") visitFunctionLike(statement, captures, getIdentifierName(statement.id) ?? enclosingName);
|
|
984
|
+
else if (statement.type === "FunctionDeclaration") visitFunctionLike$1(statement, captures, getIdentifierName(statement.id) ?? enclosingName);
|
|
983
985
|
else if (statement.type === "TSTypeAliasDeclaration") {
|
|
984
986
|
const typeAliasName = getIdentifierName(statement.id);
|
|
985
987
|
captureIfTypeLiteral(statement.typeAnnotation, captures, "local-type-alias", typeAliasName);
|
|
@@ -992,7 +994,7 @@ const walkExpressionForInlineTypes = (expressionNode, captures, enclosingName, r
|
|
|
992
994
|
if (recursionDepth > 200) return;
|
|
993
995
|
if (!isOxcAstNode(expressionNode)) return;
|
|
994
996
|
if (expressionNode.type === "ArrowFunctionExpression" || expressionNode.type === "FunctionExpression") {
|
|
995
|
-
visitFunctionLike(expressionNode, captures, enclosingName);
|
|
997
|
+
visitFunctionLike$1(expressionNode, captures, enclosingName);
|
|
996
998
|
return;
|
|
997
999
|
}
|
|
998
1000
|
for (const value of Object.values(expressionNode)) if (Array.isArray(value)) for (const element of value) walkExpressionForInlineTypes(element, captures, enclosingName, recursionDepth + 1);
|
|
@@ -1003,7 +1005,7 @@ const visitTopLevelStatement = (statementNode, captures) => {
|
|
|
1003
1005
|
const innerNode = statementNode.type === "ExportNamedDeclaration" || statementNode.type === "ExportDefaultDeclaration" ? statementNode.declaration ?? statementNode : statementNode;
|
|
1004
1006
|
const targetNode = isOxcAstNode(innerNode) ? innerNode : statementNode;
|
|
1005
1007
|
if (targetNode.type === "FunctionDeclaration") {
|
|
1006
|
-
visitFunctionLike(targetNode, captures, getIdentifierName(targetNode.id));
|
|
1008
|
+
visitFunctionLike$1(targetNode, captures, getIdentifierName(targetNode.id));
|
|
1007
1009
|
return;
|
|
1008
1010
|
}
|
|
1009
1011
|
if (targetNode.type === "VariableDeclaration") {
|
|
@@ -1023,7 +1025,7 @@ const visitTopLevelStatement = (statementNode, captures) => {
|
|
|
1023
1025
|
}
|
|
1024
1026
|
if (memberCandidate.type === "MethodDefinition" || memberCandidate.type === "TSAbstractMethodDefinition") {
|
|
1025
1027
|
const methodValue = memberCandidate.value;
|
|
1026
|
-
if (isOxcAstNode(methodValue)) visitFunctionLike(methodValue, captures, qualifiedName);
|
|
1028
|
+
if (isOxcAstNode(methodValue)) visitFunctionLike$1(methodValue, captures, qualifiedName);
|
|
1027
1029
|
}
|
|
1028
1030
|
}
|
|
1029
1031
|
return;
|
|
@@ -1573,10 +1575,24 @@ const extractMdxImportsExports = (sourceText) => {
|
|
|
1573
1575
|
return statements.join("\n");
|
|
1574
1576
|
};
|
|
1575
1577
|
const ASTRO_FRONTMATTER_PATTERN = /^---\r?\n([\s\S]*?)\r?\n---/;
|
|
1576
|
-
const
|
|
1578
|
+
const ASTRO_SCRIPT_TAG_PATTERN = /<script\b([^>]*?)\/>|<script\b([^>]*)>([\s\S]*?)<\/script>/gi;
|
|
1579
|
+
const ASTRO_SCRIPT_SRC_ATTRIBUTE_PATTERN = /\bsrc\s*=\s*["']([^"']+)["']/i;
|
|
1580
|
+
const extractAstroSources = (sourceText) => {
|
|
1581
|
+
const sections = [];
|
|
1577
1582
|
const frontmatterMatch = sourceText.match(ASTRO_FRONTMATTER_PATTERN);
|
|
1578
|
-
if (
|
|
1579
|
-
|
|
1583
|
+
if (frontmatterMatch) sections.push(frontmatterMatch[1]);
|
|
1584
|
+
ASTRO_SCRIPT_TAG_PATTERN.lastIndex = 0;
|
|
1585
|
+
let scriptMatch;
|
|
1586
|
+
while ((scriptMatch = ASTRO_SCRIPT_TAG_PATTERN.exec(sourceText)) !== null) {
|
|
1587
|
+
const selfClosingAttributes = scriptMatch[1];
|
|
1588
|
+
const pairedAttributes = scriptMatch[2];
|
|
1589
|
+
const attributes = selfClosingAttributes ?? pairedAttributes ?? "";
|
|
1590
|
+
const body = selfClosingAttributes === void 0 ? scriptMatch[3] ?? "" : "";
|
|
1591
|
+
const srcMatch = attributes.match(ASTRO_SCRIPT_SRC_ATTRIBUTE_PATTERN);
|
|
1592
|
+
if (srcMatch) sections.push(`import ${JSON.stringify(srcMatch[1])};`);
|
|
1593
|
+
if (body) sections.push(body);
|
|
1594
|
+
}
|
|
1595
|
+
return sections.join("\n");
|
|
1580
1596
|
};
|
|
1581
1597
|
const VUE_SCRIPT_PATTERN = /<script[^>]*(?:lang=["'](?:ts|tsx)["'][^>]*)?>([\s\S]*?)<\/script>/gi;
|
|
1582
1598
|
const extractVueScriptContent = (sourceText) => {
|
|
@@ -1794,7 +1810,8 @@ const parseSourceFile = (filePath) => {
|
|
|
1794
1810
|
const isAstro = filePath.endsWith(".astro");
|
|
1795
1811
|
const isVue = filePath.endsWith(".vue");
|
|
1796
1812
|
const isSvelte = filePath.endsWith(".svelte");
|
|
1797
|
-
const
|
|
1813
|
+
const isPreprocessed = isMdx || isAstro || isVue || isSvelte;
|
|
1814
|
+
const textToParse = isMdx ? extractMdxImportsExports(sourceText) : isAstro ? extractAstroSources(sourceText) : isVue ? extractVueScriptContent(sourceText) : isSvelte ? extractSvelteScriptContent(sourceText) : sourceText;
|
|
1798
1815
|
const parseFileName = isMdx || isAstro || isVue || isSvelte ? filePath.replace(/\.(mdx|astro|vue|svelte)$/, ".tsx") : filePath;
|
|
1799
1816
|
let result;
|
|
1800
1817
|
try {
|
|
@@ -1818,7 +1835,7 @@ const parseSourceFile = (filePath) => {
|
|
|
1818
1835
|
if (tsxResult.errors.length === 0) result = tsxResult;
|
|
1819
1836
|
}
|
|
1820
1837
|
} catch {}
|
|
1821
|
-
if (result.errors.length > 0) return {
|
|
1838
|
+
if (result.errors.length > 0 && !isPreprocessed) return {
|
|
1822
1839
|
...createEmptyParsedSource(),
|
|
1823
1840
|
imports,
|
|
1824
1841
|
exports,
|
|
@@ -1830,6 +1847,12 @@ const parseSourceFile = (filePath) => {
|
|
|
1830
1847
|
path: filePath
|
|
1831
1848
|
})]
|
|
1832
1849
|
};
|
|
1850
|
+
if (result.errors.length > 0) earlyErrors.push(new ParseError({
|
|
1851
|
+
code: "parse-recovered-partial",
|
|
1852
|
+
severity: "info",
|
|
1853
|
+
message: `oxc-parser reported ${result.errors.length} syntax issue(s) in extracted ${isAstro ? "Astro" : isVue ? "Vue" : isSvelte ? "Svelte" : "MDX"} sources; continuing with partial AST`,
|
|
1854
|
+
path: filePath
|
|
1855
|
+
}));
|
|
1833
1856
|
const program = result.program;
|
|
1834
1857
|
if (!program?.body) return {
|
|
1835
1858
|
...createEmptyParsedSource(),
|
|
@@ -3122,6 +3145,7 @@ const EXPO_ENTRY_PATTERNS = [
|
|
|
3122
3145
|
];
|
|
3123
3146
|
const EXPO_ROUTER_ENTRY_PATTERNS = [
|
|
3124
3147
|
"app/**/*.{ts,tsx,js,jsx}",
|
|
3148
|
+
"src/app/**/*.{ts,tsx,js,jsx}",
|
|
3125
3149
|
"app.config.{ts,js,mjs,cjs}",
|
|
3126
3150
|
"metro.config.{ts,js,mjs,cjs}",
|
|
3127
3151
|
"babel.config.{ts,js,mjs,cjs}"
|
|
@@ -3164,6 +3188,196 @@ const discoverMobileEntryPoints = (directory) => {
|
|
|
3164
3188
|
}
|
|
3165
3189
|
};
|
|
3166
3190
|
|
|
3191
|
+
//#endregion
|
|
3192
|
+
//#region src/collect/expo-config-plugin-entries.ts
|
|
3193
|
+
const EXPO_CONFIG_FILE_GLOBS = ["app.config.{ts,mts,cts,js,mjs,cjs}", "app.json"];
|
|
3194
|
+
const NESTED_EXPO_CONFIG_FILE_GLOBS = [
|
|
3195
|
+
...EXPO_CONFIG_FILE_GLOBS,
|
|
3196
|
+
"**/app.config.{ts,mts,cts,js,mjs,cjs}",
|
|
3197
|
+
"**/app.json"
|
|
3198
|
+
];
|
|
3199
|
+
const EXPO_REACT_NATIVE_DEPENDENCIES = new Set(["expo", "react-native"]);
|
|
3200
|
+
const EXPO_PLUGIN_RESOLVABLE_EXTENSIONS = SOURCE_EXTENSIONS$3.map((sourceExtension) => `.${sourceExtension}`);
|
|
3201
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
3202
|
+
const isExpoOrReactNativeWorkspace = (dependencies) => [...EXPO_REACT_NATIVE_DEPENDENCIES].some((dependencyName) => dependencyName in dependencies);
|
|
3203
|
+
const isLocalExpoPluginPath = (value) => (value.startsWith("./") || value.startsWith("../")) && !value.includes("*") && !value.includes("?");
|
|
3204
|
+
const isFile = (filePath) => {
|
|
3205
|
+
try {
|
|
3206
|
+
return (0, node_fs.statSync)(filePath).isFile();
|
|
3207
|
+
} catch {
|
|
3208
|
+
return false;
|
|
3209
|
+
}
|
|
3210
|
+
};
|
|
3211
|
+
const resolveExpoPluginPath = (configDirectory, pluginPath) => {
|
|
3212
|
+
const candidatePath = (0, node_path.resolve)(configDirectory, pluginPath);
|
|
3213
|
+
if (isFile(candidatePath)) return candidatePath;
|
|
3214
|
+
for (const extension of EXPO_PLUGIN_RESOLVABLE_EXTENSIONS) {
|
|
3215
|
+
const candidatePathWithExtension = `${candidatePath}${extension}`;
|
|
3216
|
+
if (isFile(candidatePathWithExtension)) return candidatePathWithExtension;
|
|
3217
|
+
}
|
|
3218
|
+
for (const extension of EXPO_PLUGIN_RESOLVABLE_EXTENSIONS) {
|
|
3219
|
+
const indexCandidatePath = (0, node_path.join)(candidatePath, `index${extension}`);
|
|
3220
|
+
if (isFile(indexCandidatePath)) return indexCandidatePath;
|
|
3221
|
+
}
|
|
3222
|
+
};
|
|
3223
|
+
const addExpoPluginEntry = (entries, rootDirectory, configDirectory, pluginPath) => {
|
|
3224
|
+
if (!isLocalExpoPluginPath(pluginPath)) return;
|
|
3225
|
+
const resolvedPath = resolveExpoPluginPath(configDirectory, pluginPath);
|
|
3226
|
+
if (!resolvedPath) return;
|
|
3227
|
+
const relativePath = (0, node_path.relative)(rootDirectory, resolvedPath);
|
|
3228
|
+
if (relativePath !== "" && (relativePath.startsWith("..") || (0, node_path.isAbsolute)(relativePath))) return;
|
|
3229
|
+
entries.add(resolvedPath);
|
|
3230
|
+
};
|
|
3231
|
+
const getPropertyName = (name) => {
|
|
3232
|
+
if (typescript.default.isIdentifier(name) || typescript.default.isStringLiteral(name) || typescript.default.isNumericLiteral(name)) return name.text;
|
|
3233
|
+
};
|
|
3234
|
+
const unwrapExpression = (expression) => {
|
|
3235
|
+
let currentExpression = expression;
|
|
3236
|
+
while (typescript.default.isParenthesizedExpression(currentExpression)) currentExpression = currentExpression.expression;
|
|
3237
|
+
return currentExpression;
|
|
3238
|
+
};
|
|
3239
|
+
const collectExpoPluginPathsFromArray = (array, entries, rootDirectory, configDirectory) => {
|
|
3240
|
+
for (const element of array.elements) {
|
|
3241
|
+
if (typescript.default.isStringLiteral(element) || typescript.default.isNoSubstitutionTemplateLiteral(element)) {
|
|
3242
|
+
addExpoPluginEntry(entries, rootDirectory, configDirectory, element.text);
|
|
3243
|
+
continue;
|
|
3244
|
+
}
|
|
3245
|
+
if (typescript.default.isArrayLiteralExpression(element)) {
|
|
3246
|
+
const [pluginName] = element.elements;
|
|
3247
|
+
if (pluginName && (typescript.default.isStringLiteral(pluginName) || typescript.default.isNoSubstitutionTemplateLiteral(pluginName))) addExpoPluginEntry(entries, rootDirectory, configDirectory, pluginName.text);
|
|
3248
|
+
}
|
|
3249
|
+
}
|
|
3250
|
+
};
|
|
3251
|
+
const collectExpoPluginPathsFromConfigObject = (objectLiteral, entries, rootDirectory, configDirectory) => {
|
|
3252
|
+
for (const property of objectLiteral.properties) if (typescript.default.isPropertyAssignment(property) && getPropertyName(property.name) === "plugins" && typescript.default.isArrayLiteralExpression(property.initializer)) collectExpoPluginPathsFromArray(property.initializer, entries, rootDirectory, configDirectory);
|
|
3253
|
+
};
|
|
3254
|
+
const collectReturnedExpoConfigPluginPaths = (body, entries, rootDirectory, configDirectory) => {
|
|
3255
|
+
if (!typescript.default.isBlock(body)) {
|
|
3256
|
+
const expression = unwrapExpression(body);
|
|
3257
|
+
if (typescript.default.isObjectLiteralExpression(expression)) collectExpoPluginPathsFromConfigObject(expression, entries, rootDirectory, configDirectory);
|
|
3258
|
+
return;
|
|
3259
|
+
}
|
|
3260
|
+
const visit = (node) => {
|
|
3261
|
+
if (typescript.default.isFunctionDeclaration(node) || typescript.default.isFunctionExpression(node) || typescript.default.isArrowFunction(node)) return;
|
|
3262
|
+
if (typescript.default.isReturnStatement(node) && node.expression) {
|
|
3263
|
+
const expression = unwrapExpression(node.expression);
|
|
3264
|
+
if (typescript.default.isObjectLiteralExpression(expression)) collectExpoPluginPathsFromConfigObject(expression, entries, rootDirectory, configDirectory);
|
|
3265
|
+
return;
|
|
3266
|
+
}
|
|
3267
|
+
typescript.default.forEachChild(node, visit);
|
|
3268
|
+
};
|
|
3269
|
+
visit(body);
|
|
3270
|
+
};
|
|
3271
|
+
const collectExpoPluginPathsFromConfigExpression = (expression, entries, rootDirectory, configDirectory, bindings, seenIdentifiers = /* @__PURE__ */ new Set()) => {
|
|
3272
|
+
const configExpression = unwrapExpression(expression);
|
|
3273
|
+
if (typescript.default.isObjectLiteralExpression(configExpression)) {
|
|
3274
|
+
collectExpoPluginPathsFromConfigObject(configExpression, entries, rootDirectory, configDirectory);
|
|
3275
|
+
return;
|
|
3276
|
+
}
|
|
3277
|
+
if (typescript.default.isIdentifier(configExpression)) {
|
|
3278
|
+
if (seenIdentifiers.has(configExpression.text)) return;
|
|
3279
|
+
seenIdentifiers.add(configExpression.text);
|
|
3280
|
+
const boundExpression = bindings.expressions.get(configExpression.text);
|
|
3281
|
+
if (boundExpression) {
|
|
3282
|
+
collectExpoPluginPathsFromConfigExpression(boundExpression, entries, rootDirectory, configDirectory, bindings, seenIdentifiers);
|
|
3283
|
+
return;
|
|
3284
|
+
}
|
|
3285
|
+
const boundFunction = bindings.functions.get(configExpression.text);
|
|
3286
|
+
if (boundFunction?.body) collectReturnedExpoConfigPluginPaths(boundFunction.body, entries, rootDirectory, configDirectory);
|
|
3287
|
+
return;
|
|
3288
|
+
}
|
|
3289
|
+
if (typescript.default.isArrowFunction(configExpression)) {
|
|
3290
|
+
collectReturnedExpoConfigPluginPaths(configExpression.body, entries, rootDirectory, configDirectory);
|
|
3291
|
+
return;
|
|
3292
|
+
}
|
|
3293
|
+
if (typescript.default.isFunctionExpression(configExpression) && configExpression.body) collectReturnedExpoConfigPluginPaths(configExpression.body, entries, rootDirectory, configDirectory);
|
|
3294
|
+
};
|
|
3295
|
+
const hasDefaultExportModifier = (node) => Boolean(typescript.default.canHaveModifiers(node) && typescript.default.getModifiers(node)?.some((modifier) => modifier.kind === typescript.default.SyntaxKind.DefaultKeyword));
|
|
3296
|
+
const isModuleExportsAssignmentTarget = (node) => typescript.default.isPropertyAccessExpression(node) && typescript.default.isIdentifier(node.expression) && node.expression.text === "module" && node.name.text === "exports";
|
|
3297
|
+
const collectStaticConfigBindings = (sourceFile) => {
|
|
3298
|
+
const expressions = /* @__PURE__ */ new Map();
|
|
3299
|
+
const functions = /* @__PURE__ */ new Map();
|
|
3300
|
+
for (const statement of sourceFile.statements) {
|
|
3301
|
+
if (typescript.default.isVariableStatement(statement)) {
|
|
3302
|
+
for (const declaration of statement.declarationList.declarations) if (typescript.default.isIdentifier(declaration.name) && declaration.initializer) expressions.set(declaration.name.text, declaration.initializer);
|
|
3303
|
+
continue;
|
|
3304
|
+
}
|
|
3305
|
+
if (typescript.default.isFunctionDeclaration(statement) && statement.name) functions.set(statement.name.text, statement);
|
|
3306
|
+
}
|
|
3307
|
+
return {
|
|
3308
|
+
expressions,
|
|
3309
|
+
functions
|
|
3310
|
+
};
|
|
3311
|
+
};
|
|
3312
|
+
const collectExpoPluginPathsFromAppConfig = (configPath, entries, rootDirectory) => {
|
|
3313
|
+
const extension = (0, node_path.extname)(configPath);
|
|
3314
|
+
const sourceFile = typescript.default.createSourceFile(configPath, (0, node_fs.readFileSync)(configPath, "utf8"), typescript.default.ScriptTarget.Latest, true, extension === ".ts" || extension === ".mts" || extension === ".cts" ? typescript.default.ScriptKind.TS : typescript.default.ScriptKind.JS);
|
|
3315
|
+
const configDirectory = (0, node_path.dirname)(configPath);
|
|
3316
|
+
const bindings = collectStaticConfigBindings(sourceFile);
|
|
3317
|
+
const visit = (node) => {
|
|
3318
|
+
if (typescript.default.isExportAssignment(node)) {
|
|
3319
|
+
collectExpoPluginPathsFromConfigExpression(node.expression, entries, rootDirectory, configDirectory, bindings);
|
|
3320
|
+
return;
|
|
3321
|
+
}
|
|
3322
|
+
if (typescript.default.isFunctionDeclaration(node) && hasDefaultExportModifier(node) && node.body) {
|
|
3323
|
+
collectReturnedExpoConfigPluginPaths(node.body, entries, rootDirectory, configDirectory);
|
|
3324
|
+
return;
|
|
3325
|
+
}
|
|
3326
|
+
if (typescript.default.isBinaryExpression(node) && node.operatorToken.kind === typescript.default.SyntaxKind.EqualsToken && isModuleExportsAssignmentTarget(node.left)) {
|
|
3327
|
+
collectExpoPluginPathsFromConfigExpression(node.right, entries, rootDirectory, configDirectory, bindings);
|
|
3328
|
+
return;
|
|
3329
|
+
}
|
|
3330
|
+
typescript.default.forEachChild(node, visit);
|
|
3331
|
+
};
|
|
3332
|
+
visit(sourceFile);
|
|
3333
|
+
};
|
|
3334
|
+
const collectPluginPathsFromJsonValue = (value) => {
|
|
3335
|
+
if (!Array.isArray(value)) return [];
|
|
3336
|
+
const pluginPaths = [];
|
|
3337
|
+
for (const plugin of value) {
|
|
3338
|
+
if (typeof plugin === "string") {
|
|
3339
|
+
pluginPaths.push(plugin);
|
|
3340
|
+
continue;
|
|
3341
|
+
}
|
|
3342
|
+
if (Array.isArray(plugin) && typeof plugin[0] === "string") pluginPaths.push(plugin[0]);
|
|
3343
|
+
}
|
|
3344
|
+
return pluginPaths;
|
|
3345
|
+
};
|
|
3346
|
+
const collectExpoPluginPathsFromAppJson = (configPath, entries, rootDirectory) => {
|
|
3347
|
+
const parsedJson = JSON.parse((0, node_fs.readFileSync)(configPath, "utf8"));
|
|
3348
|
+
const configDirectory = (0, node_path.dirname)(configPath);
|
|
3349
|
+
if (!isRecord(parsedJson)) return;
|
|
3350
|
+
const expoConfig = parsedJson.expo;
|
|
3351
|
+
const expoPluginPaths = isRecord(expoConfig) ? collectPluginPathsFromJsonValue(expoConfig.plugins) : [];
|
|
3352
|
+
for (const pluginPath of [...expoPluginPaths, ...collectPluginPathsFromJsonValue(parsedJson.plugins)]) addExpoPluginEntry(entries, rootDirectory, configDirectory, pluginPath);
|
|
3353
|
+
};
|
|
3354
|
+
const collectExpoPluginPathsFromConfig = (configPath, entries, rootDirectory) => {
|
|
3355
|
+
try {
|
|
3356
|
+
if ((0, node_path.basename)(configPath) === "app.json") {
|
|
3357
|
+
collectExpoPluginPathsFromAppJson(configPath, entries, rootDirectory);
|
|
3358
|
+
return;
|
|
3359
|
+
}
|
|
3360
|
+
collectExpoPluginPathsFromAppConfig(configPath, entries, rootDirectory);
|
|
3361
|
+
} catch {}
|
|
3362
|
+
};
|
|
3363
|
+
const extractExpoConfigPluginEntries = (directory, dependencies, rootDirectory = directory, includeNestedConfigs = true) => {
|
|
3364
|
+
if (!isExpoOrReactNativeWorkspace(dependencies)) return [];
|
|
3365
|
+
const entries = /* @__PURE__ */ new Set();
|
|
3366
|
+
const configPaths = fast_glob.default.sync(includeNestedConfigs ? NESTED_EXPO_CONFIG_FILE_GLOBS : EXPO_CONFIG_FILE_GLOBS, {
|
|
3367
|
+
cwd: directory,
|
|
3368
|
+
absolute: true,
|
|
3369
|
+
onlyFiles: true,
|
|
3370
|
+
ignore: [
|
|
3371
|
+
"**/node_modules/**",
|
|
3372
|
+
"**/dist/**",
|
|
3373
|
+
"**/build/**"
|
|
3374
|
+
],
|
|
3375
|
+
deep: 6
|
|
3376
|
+
});
|
|
3377
|
+
for (const configPath of configPaths) collectExpoPluginPathsFromConfig(configPath, entries, rootDirectory);
|
|
3378
|
+
return [...entries];
|
|
3379
|
+
};
|
|
3380
|
+
|
|
3167
3381
|
//#endregion
|
|
3168
3382
|
//#region src/resolver/source-path.ts
|
|
3169
3383
|
const SOURCE_EXTENSIONS$1 = [
|
|
@@ -3204,6 +3418,8 @@ const resolveSourcePath = (distPath, directory) => {
|
|
|
3204
3418
|
const sourceCandidate = (0, node_path.resolve)(directory, withoutExtension + sourceExtension);
|
|
3205
3419
|
if ((0, node_fs.existsSync)(sourceCandidate)) return sourceCandidate;
|
|
3206
3420
|
}
|
|
3421
|
+
const indexPrefixedCandidate = resolveWithIndexPrefix(withoutExtension, directory);
|
|
3422
|
+
if (indexPrefixedCandidate) return indexPrefixedCandidate;
|
|
3207
3423
|
}
|
|
3208
3424
|
if (matchesOutputDirectory(relativeToDist)) for (const stem of SOURCE_INDEX_FALLBACK_STEMS) for (const sourceExtension of SOURCE_EXTENSIONS$1) {
|
|
3209
3425
|
const fallbackCandidate = (0, node_path.resolve)(directory, stem + sourceExtension);
|
|
@@ -3217,6 +3433,15 @@ const resolveSourcePath = (distPath, directory) => {
|
|
|
3217
3433
|
}
|
|
3218
3434
|
const indexCandidate = (0, node_path.resolve)(directory, withoutExtension, "index.ts");
|
|
3219
3435
|
if ((0, node_fs.existsSync)(indexCandidate)) return indexCandidate;
|
|
3436
|
+
const indexPrefixedCandidate = resolveWithIndexPrefix(withoutExtension, directory);
|
|
3437
|
+
if (indexPrefixedCandidate) return indexPrefixedCandidate;
|
|
3438
|
+
}
|
|
3439
|
+
};
|
|
3440
|
+
const resolveWithIndexPrefix = (stemPath, directory) => {
|
|
3441
|
+
const indexPrefixedStem = `${(0, node_path.dirname)(stemPath)}/index.${(0, node_path.basename)(stemPath)}`;
|
|
3442
|
+
for (const sourceExtension of SOURCE_EXTENSIONS$1) {
|
|
3443
|
+
const candidate = (0, node_path.resolve)(directory, indexPrefixedStem + sourceExtension);
|
|
3444
|
+
if ((0, node_fs.existsSync)(candidate)) return candidate;
|
|
3220
3445
|
}
|
|
3221
3446
|
};
|
|
3222
3447
|
|
|
@@ -3460,6 +3685,10 @@ const extractSectionsModuleEntries = (directory) => {
|
|
|
3460
3685
|
return [...entries];
|
|
3461
3686
|
};
|
|
3462
3687
|
|
|
3688
|
+
//#endregion
|
|
3689
|
+
//#region src/utils/to-posix-path.ts
|
|
3690
|
+
const toPosixPath = (filePath) => filePath.replace(/\\/g, "/");
|
|
3691
|
+
|
|
3463
3692
|
//#endregion
|
|
3464
3693
|
//#region src/collect/entries.ts
|
|
3465
3694
|
const collectSourceFiles = async (config) => {
|
|
@@ -3576,6 +3805,11 @@ const resolveEntries = async (config) => {
|
|
|
3576
3805
|
for (const workspacePackage of entryEligiblePackages) tsConfigIncludeEntries.push(...extractTsConfigIncludeFilesEntries(workspacePackage.directory));
|
|
3577
3806
|
const configStringEntries = extractConfigStringReferencedEntries(absoluteRoot);
|
|
3578
3807
|
for (const workspacePackage of entryEligiblePackages) configStringEntries.push(...extractConfigStringReferencedEntries(workspacePackage.directory));
|
|
3808
|
+
const expoConfigPluginEntries = extractExpoConfigPluginEntries(absoluteRoot, readPackageJsonDependencies((0, node_path.join)(absoluteRoot, "package.json")), absoluteRoot, false);
|
|
3809
|
+
for (const workspacePackage of entryEligiblePackages) {
|
|
3810
|
+
const workspacePackageDependencies = readPackageJsonDependencies((0, node_path.join)(workspacePackage.directory, "package.json"));
|
|
3811
|
+
expoConfigPluginEntries.push(...extractExpoConfigPluginEntries(workspacePackage.directory, workspacePackageDependencies, absoluteRoot));
|
|
3812
|
+
}
|
|
3579
3813
|
const sectionsModuleEntries = extractSectionsModuleEntries(absoluteRoot);
|
|
3580
3814
|
const wranglerEntries = extractWranglerEntries(absoluteRoot);
|
|
3581
3815
|
for (const workspacePackage of entryEligiblePackages) wranglerEntries.push(...extractWranglerEntries(workspacePackage.directory));
|
|
@@ -3586,7 +3820,7 @@ const resolveEntries = async (config) => {
|
|
|
3586
3820
|
const testRunnerDiscovery = discoverTestRunnerEntryPoints(absoluteRoot, entryEligiblePackages);
|
|
3587
3821
|
const toolingDiscovery = discoverToolingEntryPoints(absoluteRoot, entryEligiblePackages);
|
|
3588
3822
|
const ciEntries = extractCiWorkflowEntries(absoluteRoot);
|
|
3589
|
-
const testEntries = [...new Set([...testRunnerDiscovery.entryFiles, ...testSetupEntries])];
|
|
3823
|
+
const testEntries = [...new Set([...testRunnerDiscovery.entryFiles, ...testSetupEntries].map(toPosixPath))];
|
|
3590
3824
|
const testEntryPathSet = new Set(testEntries);
|
|
3591
3825
|
return {
|
|
3592
3826
|
productionEntries: [...new Set([
|
|
@@ -3604,14 +3838,15 @@ const resolveEntries = async (config) => {
|
|
|
3604
3838
|
...webWorkerEntries,
|
|
3605
3839
|
...tsConfigIncludeEntries,
|
|
3606
3840
|
...configStringEntries,
|
|
3841
|
+
...expoConfigPluginEntries,
|
|
3607
3842
|
...sectionsModuleEntries,
|
|
3608
3843
|
...wranglerEntries,
|
|
3609
3844
|
...pluginFileEntries,
|
|
3610
3845
|
...toolingDiscovery.entryFiles,
|
|
3611
3846
|
...ciEntries
|
|
3612
|
-
])].filter((entryPath) => !testEntryPathSet.has(entryPath)),
|
|
3847
|
+
].map(toPosixPath))].filter((entryPath) => !testEntryPathSet.has(entryPath)),
|
|
3613
3848
|
testEntries,
|
|
3614
|
-
alwaysUsedFiles: [...new Set([...toolingDiscovery.alwaysUsedFiles, ...testRunnerDiscovery.alwaysUsedFiles])]
|
|
3849
|
+
alwaysUsedFiles: [...new Set([...toolingDiscovery.alwaysUsedFiles, ...testRunnerDiscovery.alwaysUsedFiles].map(toPosixPath))]
|
|
3615
3850
|
};
|
|
3616
3851
|
};
|
|
3617
3852
|
const DEFAULT_INDEX_PATTERNS = [
|
|
@@ -3823,6 +4058,7 @@ const SCRIPT_MULTIPLEXERS = new Set([
|
|
|
3823
4058
|
"lerna",
|
|
3824
4059
|
"ultra"
|
|
3825
4060
|
]);
|
|
4061
|
+
const TSCONFIG_PROJECT_FLAGS = new Set(["--project", "-p"]);
|
|
3826
4062
|
const CONFIG_LIKE_FLAGS = new Set([
|
|
3827
4063
|
"--config",
|
|
3828
4064
|
"-c",
|
|
@@ -3968,7 +4204,8 @@ const extractScriptFileArguments = (scriptCommand, directory) => {
|
|
|
3968
4204
|
const configPath = tokens[tokenIndex + 1].replace(/^['"]|['"]$/g, "");
|
|
3969
4205
|
if (looksLikeFilePath(configPath)) {
|
|
3970
4206
|
const absoluteConfigPath = (0, node_path.resolve)(directory, configPath);
|
|
3971
|
-
if ((0, node_fs.existsSync)(absoluteConfigPath)) entries.push(absoluteConfigPath);
|
|
4207
|
+
if ((0, node_fs.existsSync)(absoluteConfigPath)) if (TSCONFIG_PROJECT_FLAGS.has(token) && TSCONFIG_PROJECT_PATTERN.test(absoluteConfigPath)) entries.push(...expandTsConfigProjectEntries(absoluteConfigPath));
|
|
4208
|
+
else entries.push(absoluteConfigPath);
|
|
3972
4209
|
}
|
|
3973
4210
|
tokenIndex++;
|
|
3974
4211
|
}
|
|
@@ -3977,9 +4214,11 @@ const extractScriptFileArguments = (scriptCommand, directory) => {
|
|
|
3977
4214
|
const equalsIndex = token.indexOf("=");
|
|
3978
4215
|
if (equalsIndex > 0 && CONFIG_LIKE_FLAGS.has(token.slice(0, equalsIndex))) {
|
|
3979
4216
|
const configValue = token.slice(equalsIndex + 1);
|
|
4217
|
+
const flagName = token.slice(0, equalsIndex);
|
|
3980
4218
|
if (configValue && looksLikeFilePath(configValue)) {
|
|
3981
4219
|
const absoluteConfigPath = (0, node_path.resolve)(directory, configValue);
|
|
3982
|
-
if ((0, node_fs.existsSync)(absoluteConfigPath)) entries.push(absoluteConfigPath);
|
|
4220
|
+
if ((0, node_fs.existsSync)(absoluteConfigPath)) if (TSCONFIG_PROJECT_FLAGS.has(flagName) && TSCONFIG_PROJECT_PATTERN.test(absoluteConfigPath)) entries.push(...expandTsConfigProjectEntries(absoluteConfigPath));
|
|
4221
|
+
else entries.push(absoluteConfigPath);
|
|
3983
4222
|
}
|
|
3984
4223
|
continue;
|
|
3985
4224
|
}
|
|
@@ -4282,6 +4521,7 @@ const extractScriptTagsFromHtmlFile = (htmlFilePath) => {
|
|
|
4282
4521
|
return entries;
|
|
4283
4522
|
};
|
|
4284
4523
|
const TSCONFIG_FILENAME_GLOBS = ["tsconfig.json", "tsconfig.*.json"];
|
|
4524
|
+
const TSCONFIG_PROJECT_PATTERN = /(?:^|[\\/])tsconfig(?:\.[^.]+)?\.json$/;
|
|
4285
4525
|
const stripJsoncCommentsLocal = (sourceText) => {
|
|
4286
4526
|
let result = "";
|
|
4287
4527
|
let insideString = false;
|
|
@@ -4353,6 +4593,34 @@ const extractTsConfigIncludeFilesEntries = (directory) => {
|
|
|
4353
4593
|
} catch {}
|
|
4354
4594
|
return entries;
|
|
4355
4595
|
};
|
|
4596
|
+
const expandTsConfigProjectEntries = (tsconfigAbsolutePath) => {
|
|
4597
|
+
const entries = [];
|
|
4598
|
+
try {
|
|
4599
|
+
const cleaned = stripJsoncCommentsLocal((0, node_fs.readFileSync)(tsconfigAbsolutePath, "utf-8"));
|
|
4600
|
+
const tsconfigJson = JSON.parse(cleaned);
|
|
4601
|
+
const tsconfigDir = (0, node_path.dirname)(tsconfigAbsolutePath);
|
|
4602
|
+
if (Array.isArray(tsconfigJson.files)) for (const fileItem of tsconfigJson.files) {
|
|
4603
|
+
if (typeof fileItem !== "string") continue;
|
|
4604
|
+
const candidatePath = (0, node_path.resolve)(tsconfigDir, fileItem);
|
|
4605
|
+
if ((0, node_fs.existsSync)(candidatePath)) entries.push(candidatePath);
|
|
4606
|
+
}
|
|
4607
|
+
if (Array.isArray(tsconfigJson.include)) for (const includePattern of tsconfigJson.include) {
|
|
4608
|
+
if (typeof includePattern !== "string") continue;
|
|
4609
|
+
const expandedFiles = fast_glob.default.sync(includePattern, {
|
|
4610
|
+
cwd: tsconfigDir,
|
|
4611
|
+
absolute: true,
|
|
4612
|
+
onlyFiles: true,
|
|
4613
|
+
ignore: [
|
|
4614
|
+
"**/node_modules/**",
|
|
4615
|
+
"**/dist/**",
|
|
4616
|
+
"**/build/**"
|
|
4617
|
+
]
|
|
4618
|
+
});
|
|
4619
|
+
entries.push(...expandedFiles);
|
|
4620
|
+
}
|
|
4621
|
+
} catch {}
|
|
4622
|
+
return entries;
|
|
4623
|
+
};
|
|
4356
4624
|
const WRANGLER_TOML_MAIN_PATTERN = /^\s*main\s*=\s*['"]([^'"\n]+)['"]/m;
|
|
4357
4625
|
const WRANGLER_JSON_MAIN_PATTERN = /"main"\s*:\s*"([^"]+)"/;
|
|
4358
4626
|
const WRANGLER_SERVICE_BINDINGS_PATTERN = /entry_point\s*=\s*['"]([^'"\n]+)['"]/g;
|
|
@@ -5446,7 +5714,7 @@ const FRAMEWORK_PATTERNS = [
|
|
|
5446
5714
|
"app/_layout.{ts,tsx,js,jsx}",
|
|
5447
5715
|
"app/index.{ts,tsx,js,jsx}"
|
|
5448
5716
|
],
|
|
5449
|
-
alwaysUsed: ["app.json", "app.config.{ts,js}"]
|
|
5717
|
+
alwaysUsed: ["app.json", "app.config.{ts,mts,cts,js,mjs,cjs}"]
|
|
5450
5718
|
},
|
|
5451
5719
|
{
|
|
5452
5720
|
enablers: ["wrangler"],
|
|
@@ -5783,6 +6051,40 @@ const discoverToolingEntryPoints = (rootDir, workspacePackages) => {
|
|
|
5783
6051
|
};
|
|
5784
6052
|
};
|
|
5785
6053
|
|
|
6054
|
+
//#endregion
|
|
6055
|
+
//#region src/utils/is-platform-builtin-or-virtual.ts
|
|
6056
|
+
const BUILTIN_SUBPATH_NODE_MODULES = new Set([
|
|
6057
|
+
"fs",
|
|
6058
|
+
"dns",
|
|
6059
|
+
"stream",
|
|
6060
|
+
"readline",
|
|
6061
|
+
"timers",
|
|
6062
|
+
"util",
|
|
6063
|
+
"test",
|
|
6064
|
+
"assert",
|
|
6065
|
+
"inspector",
|
|
6066
|
+
"path"
|
|
6067
|
+
]);
|
|
6068
|
+
/**
|
|
6069
|
+
* True for module specifiers that don't correspond to a real on-disk
|
|
6070
|
+
* package — Node / Bun / Cloudflare / Sass built-ins, the Deno `std`
|
|
6071
|
+
* bare specifier, and Vite `virtual:` modules — so they aren't mistakenly
|
|
6072
|
+
* surfaced as `unused-dependency` or `unresolved-import`.
|
|
6073
|
+
*/
|
|
6074
|
+
const isPlatformBuiltinOrVirtualSpecifier = (specifier) => {
|
|
6075
|
+
if (specifier.startsWith("virtual:")) return true;
|
|
6076
|
+
if (specifier === "bun" || specifier.startsWith("bun:")) return true;
|
|
6077
|
+
if (specifier.startsWith("cloudflare:")) return true;
|
|
6078
|
+
if (specifier.startsWith("sass:")) return true;
|
|
6079
|
+
if (specifier === "std" || specifier.startsWith("std/")) return true;
|
|
6080
|
+
const stripped = specifier.startsWith("node:") ? specifier.slice(5) : specifier;
|
|
6081
|
+
const slashIndex = stripped.indexOf("/");
|
|
6082
|
+
if (slashIndex === -1) return BUILTIN_MODULES.has(stripped);
|
|
6083
|
+
const baseName = stripped.slice(0, slashIndex);
|
|
6084
|
+
if (!BUILTIN_MODULES.has(baseName)) return false;
|
|
6085
|
+
return BUILTIN_SUBPATH_NODE_MODULES.has(baseName);
|
|
6086
|
+
};
|
|
6087
|
+
|
|
5786
6088
|
//#endregion
|
|
5787
6089
|
//#region src/resolver/resolve.ts
|
|
5788
6090
|
const fileExistsCache = /* @__PURE__ */ new Map();
|
|
@@ -6419,9 +6721,10 @@ const createResolver = (config, workspacePackages = [], options = {}) => {
|
|
|
6419
6721
|
try {
|
|
6420
6722
|
const resolverResult = activeResolver.sync(fromDir, cleanedSpecifier);
|
|
6421
6723
|
if (resolverResult.path) {
|
|
6422
|
-
const
|
|
6724
|
+
const normalizedResolvedPath = toPosixPath(resolverResult.path);
|
|
6725
|
+
const isInsideNodeModules = normalizedResolvedPath.includes("/node_modules/");
|
|
6423
6726
|
return {
|
|
6424
|
-
resolvedPath: isInsideNodeModules ? void 0 :
|
|
6727
|
+
resolvedPath: isInsideNodeModules ? void 0 : normalizedResolvedPath,
|
|
6425
6728
|
isExternal: isInsideNodeModules,
|
|
6426
6729
|
packageName: isInsideNodeModules ? extractPackageNameFromSpecifier(cleanedSpecifier) : void 0
|
|
6427
6730
|
};
|
|
@@ -6536,7 +6839,14 @@ const createResolver = (config, workspacePackages = [], options = {}) => {
|
|
|
6536
6839
|
resolveResultCache.set(cacheKey, unresolvedResult);
|
|
6537
6840
|
return unresolvedResult;
|
|
6538
6841
|
};
|
|
6539
|
-
|
|
6842
|
+
const resolveModuleWithPosixPath = (specifier, fromFile) => {
|
|
6843
|
+
const resolved = resolveModule(specifier, fromFile);
|
|
6844
|
+
return resolved.resolvedPath ? {
|
|
6845
|
+
...resolved,
|
|
6846
|
+
resolvedPath: toPosixPath(resolved.resolvedPath)
|
|
6847
|
+
} : resolved;
|
|
6848
|
+
};
|
|
6849
|
+
return { resolveModule: resolveModuleWithPosixPath };
|
|
6540
6850
|
};
|
|
6541
6851
|
const stripJsonComments = (content) => {
|
|
6542
6852
|
let result = "";
|
|
@@ -6577,21 +6887,7 @@ const stripJsonComments = (content) => {
|
|
|
6577
6887
|
}
|
|
6578
6888
|
return result.replace(/,(\s*[}\]])/g, "$1");
|
|
6579
6889
|
};
|
|
6580
|
-
const
|
|
6581
|
-
"fs",
|
|
6582
|
-
"dns",
|
|
6583
|
-
"stream",
|
|
6584
|
-
"readline",
|
|
6585
|
-
"timers",
|
|
6586
|
-
"util"
|
|
6587
|
-
]);
|
|
6588
|
-
const isBuiltinModule = (specifier) => {
|
|
6589
|
-
if (specifier.startsWith("node:")) return true;
|
|
6590
|
-
const baseName = specifier.split("/")[0];
|
|
6591
|
-
if (!BUILTIN_MODULES.has(baseName)) return false;
|
|
6592
|
-
if (!specifier.includes("/")) return true;
|
|
6593
|
-
return BUILTIN_SUBPATH_MODULES.has(baseName);
|
|
6594
|
-
};
|
|
6890
|
+
const isBuiltinModule = (specifier) => isPlatformBuiltinOrVirtualSpecifier(specifier);
|
|
6595
6891
|
const isBareSpecifier = (specifier) => !specifier.startsWith(".") && !specifier.startsWith("/");
|
|
6596
6892
|
const extractPackageNameFromSpecifier = (specifier) => {
|
|
6597
6893
|
if (specifier.startsWith("node:")) return specifier.slice(5).split("/")[0];
|
|
@@ -6616,7 +6912,7 @@ const isConfigFile = (filePath) => {
|
|
|
6616
6912
|
//#region src/linker/build.ts
|
|
6617
6913
|
const buildDependencyGraph = (inputs) => {
|
|
6618
6914
|
const fileIdMap = /* @__PURE__ */ new Map();
|
|
6619
|
-
for (const input of inputs) fileIdMap.set(input.fileId.path, input.fileId.index);
|
|
6915
|
+
for (const input of inputs) fileIdMap.set(toPosixPath(input.fileId.path), input.fileId.index);
|
|
6620
6916
|
const modules = inputs.map((input) => ({
|
|
6621
6917
|
fileId: input.fileId,
|
|
6622
6918
|
imports: input.parsed.imports,
|
|
@@ -6662,7 +6958,7 @@ const buildDependencyGraph = (inputs) => {
|
|
|
6662
6958
|
const sourceDir = node_path.default.dirname(input.fileId.path);
|
|
6663
6959
|
const globPattern = importInfo.specifier;
|
|
6664
6960
|
for (const [filePath] of fileIdMap) {
|
|
6665
|
-
const relativePath = node_path.default.relative(sourceDir, filePath);
|
|
6961
|
+
const relativePath = toPosixPath(node_path.default.relative(sourceDir, filePath));
|
|
6666
6962
|
if ((0, minimatch.minimatch)(relativePath.startsWith(".") ? relativePath : `./${relativePath}`, globPattern)) {
|
|
6667
6963
|
const targetIndex = fileIdMap.get(filePath);
|
|
6668
6964
|
if (targetIndex !== void 0) addEdge(sourceIndex, targetIndex, []);
|
|
@@ -6672,7 +6968,7 @@ const buildDependencyGraph = (inputs) => {
|
|
|
6672
6968
|
}
|
|
6673
6969
|
const resolved = input.resolvedImports.get(importInfo.specifier);
|
|
6674
6970
|
if (!resolved?.resolvedPath) continue;
|
|
6675
|
-
const targetIndex = fileIdMap.get(resolved.resolvedPath);
|
|
6971
|
+
const targetIndex = fileIdMap.get(toPosixPath(resolved.resolvedPath));
|
|
6676
6972
|
if (targetIndex === void 0) continue;
|
|
6677
6973
|
addEdge(sourceIndex, targetIndex, importInfo.importedNames.map((importedName) => ({
|
|
6678
6974
|
importedName: importedName.name,
|
|
@@ -6687,7 +6983,7 @@ const buildDependencyGraph = (inputs) => {
|
|
|
6687
6983
|
if (!exportInfo.isReExport || !exportInfo.reExportSource) continue;
|
|
6688
6984
|
const resolved = input.resolvedImports.get(exportInfo.reExportSource);
|
|
6689
6985
|
if (!resolved?.resolvedPath) continue;
|
|
6690
|
-
const targetIndex = fileIdMap.get(resolved.resolvedPath);
|
|
6986
|
+
const targetIndex = fileIdMap.get(toPosixPath(resolved.resolvedPath));
|
|
6691
6987
|
if (targetIndex === void 0) continue;
|
|
6692
6988
|
const exportedName = exportInfo.isNamespaceReExport ? "*" : exportInfo.name;
|
|
6693
6989
|
const originalName = exportInfo.isNamespaceReExport ? "*" : exportInfo.reExportOriginalName ?? exportInfo.name;
|
|
@@ -8476,75 +8772,2427 @@ const detectDuplicateInlineTypes = (graph) => {
|
|
|
8476
8772
|
};
|
|
8477
8773
|
|
|
8478
8774
|
//#endregion
|
|
8479
|
-
//#region src/
|
|
8480
|
-
const
|
|
8481
|
-
|
|
8482
|
-
|
|
8483
|
-
|
|
8484
|
-
|
|
8485
|
-
|
|
8486
|
-
|
|
8487
|
-
|
|
8775
|
+
//#region src/report/cross-file-duplicate-exports.ts
|
|
8776
|
+
const buildReExportSourceSets = (graph) => {
|
|
8777
|
+
const reExportSources = /* @__PURE__ */ new Map();
|
|
8778
|
+
for (const edge of graph.edges) {
|
|
8779
|
+
if (!edge.isReExportEdge) continue;
|
|
8780
|
+
const existing = reExportSources.get(edge.source);
|
|
8781
|
+
if (existing) existing.add(edge.target);
|
|
8782
|
+
else reExportSources.set(edge.source, new Set([edge.target]));
|
|
8783
|
+
}
|
|
8784
|
+
return reExportSources;
|
|
8785
|
+
};
|
|
8786
|
+
/**
|
|
8787
|
+
* Two duplicate-export files "share a common importer" when there exists a
|
|
8788
|
+
* third file that imports from both, OR one duplicate file imports another.
|
|
8789
|
+
* This filters out coincidental duplicates among unrelated leaf modules
|
|
8790
|
+
* (SvelteKit/Next.js route files, scripts in different parts of a monorepo,
|
|
8791
|
+
* etc.) that happen to export the same name but can never be confused at any
|
|
8792
|
+
* import site.
|
|
8793
|
+
*/
|
|
8794
|
+
const hasCommonImporter = (moduleIndices, graph) => {
|
|
8795
|
+
if (moduleIndices.length <= 1) return false;
|
|
8796
|
+
const duplicateModuleSet = new Set(moduleIndices);
|
|
8797
|
+
const importerOwner = /* @__PURE__ */ new Map();
|
|
8798
|
+
for (const moduleIndex of moduleIndices) {
|
|
8799
|
+
const importers = graph.reverseEdges.get(moduleIndex) ?? [];
|
|
8800
|
+
for (const importerIndex of importers) {
|
|
8801
|
+
if (duplicateModuleSet.has(importerIndex)) return true;
|
|
8802
|
+
const previousOwner = importerOwner.get(importerIndex);
|
|
8803
|
+
if (previousOwner === void 0) importerOwner.set(importerIndex, moduleIndex);
|
|
8804
|
+
else if (previousOwner !== moduleIndex) return true;
|
|
8805
|
+
}
|
|
8806
|
+
}
|
|
8807
|
+
return false;
|
|
8808
|
+
};
|
|
8809
|
+
/**
|
|
8810
|
+
* Cross-file duplicate exports: the same exported name lives in 2+ files.
|
|
8811
|
+
*
|
|
8812
|
+
* Filters applied (to keep the rule actionable):
|
|
8813
|
+
* - default exports are skipped (every module gets one and it's not actionable)
|
|
8814
|
+
* - re-export chains are pruned: if module A re-exports `Foo` from module B,
|
|
8815
|
+
* the (A, B) pair is one chain, not two real declarations
|
|
8816
|
+
* - TypeScript value/type namespace split: `export const X` and `export type X`
|
|
8817
|
+
* in the same file are distinct in TS's value/type namespaces; same name in a
|
|
8818
|
+
* value file and a type file is not a true duplicate either
|
|
8819
|
+
* - common-importer filter: only report duplicates where two of the duplicate
|
|
8820
|
+
* files share an importer or one imports another, so unrelated route files in
|
|
8821
|
+
* different parts of a repo don't get flagged
|
|
8822
|
+
*/
|
|
8823
|
+
const detectCrossFileDuplicateExports = (graph) => {
|
|
8824
|
+
const reExportSources = buildReExportSourceSets(graph);
|
|
8825
|
+
const exportEntriesByName = /* @__PURE__ */ new Map();
|
|
8826
|
+
for (const module of graph.modules) {
|
|
8827
|
+
if (!module.isReachable) continue;
|
|
8828
|
+
if (module.isDeclarationFile) continue;
|
|
8829
|
+
if (module.isEntryPoint) continue;
|
|
8830
|
+
for (const exportInfo of module.exports) {
|
|
8831
|
+
if (exportInfo.isDefault) continue;
|
|
8832
|
+
if (exportInfo.isSynthetic) continue;
|
|
8833
|
+
if (exportInfo.name === "*") continue;
|
|
8834
|
+
if (exportInfo.isReExport) continue;
|
|
8835
|
+
const entry = {
|
|
8836
|
+
moduleIndex: module.fileId.index,
|
|
8837
|
+
path: module.fileId.path,
|
|
8838
|
+
line: exportInfo.line,
|
|
8839
|
+
column: exportInfo.column,
|
|
8840
|
+
isTypeOnly: exportInfo.isTypeOnly
|
|
8841
|
+
};
|
|
8842
|
+
const existing = exportEntriesByName.get(exportInfo.name);
|
|
8843
|
+
if (existing) existing.push(entry);
|
|
8844
|
+
else exportEntriesByName.set(exportInfo.name, [entry]);
|
|
8845
|
+
}
|
|
8846
|
+
}
|
|
8847
|
+
const findings = [];
|
|
8848
|
+
const sortedEntries = [...exportEntriesByName.entries()].sort(([nameA], [nameB]) => nameA.localeCompare(nameB));
|
|
8849
|
+
for (const [name, entries] of sortedEntries) {
|
|
8850
|
+
if (entries.length <= 1) continue;
|
|
8851
|
+
const hasValueExport = entries.some((entry) => !entry.isTypeOnly);
|
|
8852
|
+
const hasTypeExport = entries.some((entry) => entry.isTypeOnly);
|
|
8853
|
+
if (hasValueExport && hasTypeExport) {
|
|
8854
|
+
const valueModuleIndices = new Set(entries.filter((entry) => !entry.isTypeOnly).map((entry) => entry.moduleIndex));
|
|
8855
|
+
const typeModuleIndices = new Set(entries.filter((entry) => entry.isTypeOnly).map((entry) => entry.moduleIndex));
|
|
8856
|
+
if (valueModuleIndices.size <= 1 && typeModuleIndices.size <= 1) continue;
|
|
8857
|
+
}
|
|
8858
|
+
const moduleIndexSet = new Set(entries.map((entry) => entry.moduleIndex));
|
|
8859
|
+
const independentEntries = entries.filter((entry) => {
|
|
8860
|
+
const sources = reExportSources.get(entry.moduleIndex);
|
|
8861
|
+
if (!sources) return true;
|
|
8862
|
+
for (const sourceIndex of sources) if (moduleIndexSet.has(sourceIndex)) return false;
|
|
8863
|
+
return true;
|
|
8864
|
+
});
|
|
8865
|
+
if (independentEntries.length <= 1) continue;
|
|
8866
|
+
if (!hasCommonImporter(independentEntries.map((entry) => entry.moduleIndex), graph)) continue;
|
|
8867
|
+
const locations = independentEntries.map((entry) => ({
|
|
8868
|
+
path: entry.path,
|
|
8869
|
+
line: entry.line,
|
|
8870
|
+
column: entry.column,
|
|
8871
|
+
isTypeOnly: entry.isTypeOnly
|
|
8488
8872
|
}));
|
|
8489
|
-
|
|
8873
|
+
findings.push({
|
|
8874
|
+
name,
|
|
8875
|
+
locations,
|
|
8876
|
+
confidence: "medium",
|
|
8877
|
+
reason: `"${name}" is exported from ${locations.length} files that share a common importer — consumers may import the wrong one`
|
|
8878
|
+
});
|
|
8490
8879
|
}
|
|
8880
|
+
return findings;
|
|
8491
8881
|
};
|
|
8492
8882
|
|
|
8493
8883
|
//#endregion
|
|
8494
|
-
//#region src/
|
|
8495
|
-
const
|
|
8884
|
+
//#region src/utils/compute-line-starts.ts
|
|
8885
|
+
const LINE_FEED_CHAR_CODE = 10;
|
|
8886
|
+
const computeLineStarts = (sourceText) => {
|
|
8887
|
+
const lineStarts = [0];
|
|
8888
|
+
for (let charIndex = 0; charIndex < sourceText.length; charIndex++) if (sourceText.charCodeAt(charIndex) === LINE_FEED_CHAR_CODE) lineStarts.push(charIndex + 1);
|
|
8889
|
+
return lineStarts;
|
|
8890
|
+
};
|
|
8891
|
+
|
|
8892
|
+
//#endregion
|
|
8893
|
+
//#region src/utils/offset-to-line-column.ts
|
|
8894
|
+
const offsetToLineColumn = (byteOffset, lineStarts) => {
|
|
8895
|
+
let lowIndex = 0;
|
|
8896
|
+
let highIndex = lineStarts.length - 1;
|
|
8897
|
+
while (lowIndex < highIndex) {
|
|
8898
|
+
const middleIndex = lowIndex + highIndex + 1 >>> 1;
|
|
8899
|
+
if (lineStarts[middleIndex] <= byteOffset) lowIndex = middleIndex;
|
|
8900
|
+
else highIndex = middleIndex - 1;
|
|
8901
|
+
}
|
|
8496
8902
|
return {
|
|
8497
|
-
|
|
8498
|
-
|
|
8499
|
-
error: new TypeScriptError({
|
|
8500
|
-
code: {
|
|
8501
|
-
"no-tsconfig": "tsconfig-not-found",
|
|
8502
|
-
"tsconfig-parse-error": "tsconfig-parse-failed",
|
|
8503
|
-
"program-creation-failed": "ts-program-creation-failed",
|
|
8504
|
-
"too-many-files": "ts-program-too-large",
|
|
8505
|
-
"typescript-load-failed": "ts-not-loadable"
|
|
8506
|
-
}[reason],
|
|
8507
|
-
severity: reason === "no-tsconfig" ? "info" : "warning",
|
|
8508
|
-
message,
|
|
8509
|
-
path: options.rootDir || void 0,
|
|
8510
|
-
detail: options.detail
|
|
8511
|
-
})
|
|
8903
|
+
line: lowIndex + 1,
|
|
8904
|
+
column: byteOffset - lineStarts[lowIndex]
|
|
8512
8905
|
};
|
|
8513
8906
|
};
|
|
8514
|
-
|
|
8515
|
-
|
|
8516
|
-
|
|
8517
|
-
|
|
8518
|
-
|
|
8907
|
+
|
|
8908
|
+
//#endregion
|
|
8909
|
+
//#region src/duplicate-blocks/concatenate.ts
|
|
8910
|
+
const SENTINEL_FILE_INDEX = Number.MAX_SAFE_INTEGER;
|
|
8911
|
+
/**
|
|
8912
|
+
* Rank-reduce token hashes to dense 0..K-1 integers and concatenate every
|
|
8913
|
+
* file's reduced sequence with a unique negative sentinel between files. Dense
|
|
8914
|
+
* ranks shrink the suffix-array's bucket counters from ~4 billion to a few
|
|
8915
|
+
* thousand (the standard prefix-doubling speedup), and negative sentinels
|
|
8916
|
+
* guarantee no real-token suffix can match across a file boundary.
|
|
8917
|
+
*/
|
|
8918
|
+
const rankReduceAndConcatenate = (filesHashedTokens) => {
|
|
8919
|
+
const uniqueHashes = /* @__PURE__ */ new Set();
|
|
8920
|
+
for (const fileTokens of filesHashedTokens) for (const hashedToken of fileTokens) uniqueHashes.add(hashedToken.hash);
|
|
8921
|
+
const sortedUniqueHashes = [...uniqueHashes].sort((leftHash, rightHash) => leftHash - rightHash);
|
|
8922
|
+
const hashToRank = /* @__PURE__ */ new Map();
|
|
8923
|
+
for (let rankIndex = 0; rankIndex < sortedUniqueHashes.length; rankIndex++) hashToRank.set(sortedUniqueHashes[rankIndex], rankIndex + 1);
|
|
8924
|
+
const sequenceLength = filesHashedTokens.reduce((runningSum, fileTokens) => runningSum + fileTokens.length, 0) + Math.max(0, filesHashedTokens.length - 1);
|
|
8925
|
+
const tokenSequence = new Array(sequenceLength);
|
|
8926
|
+
const fileOf = new Array(sequenceLength);
|
|
8927
|
+
const fileOffsets = new Array(filesHashedTokens.length);
|
|
8928
|
+
let writeCursor = 0;
|
|
8929
|
+
let nextSentinelValue = -1;
|
|
8930
|
+
for (let fileIndex = 0; fileIndex < filesHashedTokens.length; fileIndex++) {
|
|
8931
|
+
fileOffsets[fileIndex] = writeCursor;
|
|
8932
|
+
const fileTokens = filesHashedTokens[fileIndex];
|
|
8933
|
+
for (const hashedToken of fileTokens) {
|
|
8934
|
+
tokenSequence[writeCursor] = hashToRank.get(hashedToken.hash) ?? 0;
|
|
8935
|
+
fileOf[writeCursor] = fileIndex;
|
|
8936
|
+
writeCursor++;
|
|
8937
|
+
}
|
|
8938
|
+
if (fileIndex < filesHashedTokens.length - 1) {
|
|
8939
|
+
tokenSequence[writeCursor] = nextSentinelValue;
|
|
8940
|
+
fileOf[writeCursor] = SENTINEL_FILE_INDEX;
|
|
8941
|
+
writeCursor++;
|
|
8942
|
+
nextSentinelValue--;
|
|
8943
|
+
}
|
|
8519
8944
|
}
|
|
8520
|
-
|
|
8521
|
-
|
|
8522
|
-
|
|
8945
|
+
return {
|
|
8946
|
+
tokenSequence,
|
|
8947
|
+
fileOf,
|
|
8948
|
+
fileOffsets
|
|
8949
|
+
};
|
|
8950
|
+
};
|
|
8951
|
+
const SENTINEL_FILE_MARKER = SENTINEL_FILE_INDEX;
|
|
8952
|
+
|
|
8953
|
+
//#endregion
|
|
8954
|
+
//#region src/duplicate-blocks/extract.ts
|
|
8955
|
+
const buildRawBlock = (suffixArray, fileOf, fileOffsets, filesTokenCounts, intervalBegin, intervalEnd, tokenLength) => {
|
|
8956
|
+
const candidateInstances = [];
|
|
8957
|
+
for (let suffixIndex = intervalBegin; suffixIndex < intervalEnd; suffixIndex++) {
|
|
8958
|
+
const startPosition = suffixArray[suffixIndex];
|
|
8959
|
+
const fileIndex = fileOf[startPosition];
|
|
8960
|
+
if (fileIndex === SENTINEL_FILE_MARKER) continue;
|
|
8961
|
+
const tokenOffsetWithinFile = startPosition - fileOffsets[fileIndex];
|
|
8962
|
+
if (tokenOffsetWithinFile + tokenLength > filesTokenCounts[fileIndex]) continue;
|
|
8963
|
+
candidateInstances.push({
|
|
8964
|
+
fileIndex,
|
|
8965
|
+
tokenOffsetWithinFile
|
|
8966
|
+
});
|
|
8967
|
+
}
|
|
8968
|
+
if (candidateInstances.length < 2) return void 0;
|
|
8969
|
+
candidateInstances.sort((leftInstance, rightInstance) => {
|
|
8970
|
+
if (leftInstance.fileIndex !== rightInstance.fileIndex) return leftInstance.fileIndex - rightInstance.fileIndex;
|
|
8971
|
+
return leftInstance.tokenOffsetWithinFile - rightInstance.tokenOffsetWithinFile;
|
|
8972
|
+
});
|
|
8973
|
+
const dedupedInstances = [];
|
|
8974
|
+
for (const instance of candidateInstances) {
|
|
8975
|
+
const lastInstance = dedupedInstances[dedupedInstances.length - 1];
|
|
8976
|
+
if (lastInstance !== void 0 && lastInstance.fileIndex === instance.fileIndex && instance.tokenOffsetWithinFile < lastInstance.tokenOffsetWithinFile + tokenLength) continue;
|
|
8977
|
+
dedupedInstances.push(instance);
|
|
8523
8978
|
}
|
|
8979
|
+
if (dedupedInstances.length < 2) return void 0;
|
|
8980
|
+
return {
|
|
8981
|
+
instances: dedupedInstances,
|
|
8982
|
+
tokenLength
|
|
8983
|
+
};
|
|
8524
8984
|
};
|
|
8525
|
-
|
|
8526
|
-
|
|
8527
|
-
|
|
8528
|
-
|
|
8529
|
-
|
|
8985
|
+
/**
|
|
8986
|
+
* Walks `lcpArray` with a monotone stack to materialize every maximal
|
|
8987
|
+
* interval `[i, j]` whose minimum LCP is >= `minTokens`. Within-file
|
|
8988
|
+
* overlapping occurrences are dropped (keep the earliest non-overlapping
|
|
8989
|
+
* prefix), and any block left with fewer than two occurrences is discarded.
|
|
8990
|
+
*/
|
|
8991
|
+
const extractRawDuplicateBlocks = (suffixArray, lcpArray, fileOf, fileOffsets, filesTokenCounts, minTokens) => {
|
|
8992
|
+
const sequenceLength = suffixArray.length;
|
|
8993
|
+
if (sequenceLength < 2) return [];
|
|
8994
|
+
const rawBlocks = [];
|
|
8995
|
+
const monotoneStack = [];
|
|
8996
|
+
for (let scanIndex = 1; scanIndex <= sequenceLength; scanIndex++) {
|
|
8997
|
+
const currentLcp = scanIndex < sequenceLength ? lcpArray[scanIndex] : 0;
|
|
8998
|
+
let intervalStart = scanIndex;
|
|
8999
|
+
while (monotoneStack.length > 0 && monotoneStack[monotoneStack.length - 1].lcpValue > currentLcp) {
|
|
9000
|
+
const popped = monotoneStack.pop();
|
|
9001
|
+
intervalStart = popped.startIndex;
|
|
9002
|
+
if (popped.lcpValue >= minTokens) {
|
|
9003
|
+
const candidate = buildRawBlock(suffixArray, fileOf, fileOffsets, filesTokenCounts, intervalStart - 1, scanIndex, popped.lcpValue);
|
|
9004
|
+
if (candidate) rawBlocks.push(candidate);
|
|
9005
|
+
}
|
|
9006
|
+
}
|
|
9007
|
+
if (scanIndex < sequenceLength) monotoneStack.push({
|
|
9008
|
+
lcpValue: currentLcp,
|
|
9009
|
+
startIndex: intervalStart
|
|
9010
|
+
});
|
|
9011
|
+
}
|
|
9012
|
+
return rawBlocks;
|
|
9013
|
+
};
|
|
9014
|
+
|
|
9015
|
+
//#endregion
|
|
9016
|
+
//#region src/duplicate-blocks/clusters.ts
|
|
9017
|
+
const baseName = (filePath) => {
|
|
9018
|
+
const trailingSlashIndex = filePath.lastIndexOf("/");
|
|
9019
|
+
return trailingSlashIndex === -1 ? filePath : filePath.slice(trailingSlashIndex + 1);
|
|
9020
|
+
};
|
|
9021
|
+
const buildSuggestions = (files, blocks, totalDuplicatedLines) => {
|
|
9022
|
+
const fileBaseNames = files.map((filePath) => baseName(filePath));
|
|
9023
|
+
if (files.length >= 2 && totalDuplicatedLines >= 50) {
|
|
9024
|
+
const estimatedSavings = blocks.reduce((runningSum, block) => runningSum + block.lineCount * Math.max(0, block.instances.length - 1), 0);
|
|
9025
|
+
return [{
|
|
9026
|
+
kind: "extract-module",
|
|
9027
|
+
description: `Extract ${blocks.length} shared duplicate block${blocks.length === 1 ? "" : "s"} (${totalDuplicatedLines} lines) from ${fileBaseNames.join(", ")} into a shared module`,
|
|
9028
|
+
estimatedSavings
|
|
9029
|
+
}];
|
|
9030
|
+
}
|
|
9031
|
+
return blocks.map((block) => ({
|
|
9032
|
+
kind: "extract-function",
|
|
9033
|
+
description: `Extract shared function (${block.lineCount} lines) from ${fileBaseNames.join(", ")}`,
|
|
9034
|
+
estimatedSavings: block.lineCount * Math.max(0, block.instances.length - 1)
|
|
9035
|
+
}));
|
|
9036
|
+
};
|
|
9037
|
+
const groupDuplicateBlocksIntoClusters = (duplicateBlocks) => {
|
|
9038
|
+
if (duplicateBlocks.length === 0) return [];
|
|
9039
|
+
const fileSetKeyToBucket = /* @__PURE__ */ new Map();
|
|
9040
|
+
for (const block of duplicateBlocks) {
|
|
9041
|
+
const sortedFiles = [...new Set(block.instances.map((instance) => instance.path))].sort();
|
|
9042
|
+
const fileSetKey = sortedFiles.join("|");
|
|
9043
|
+
const existing = fileSetKeyToBucket.get(fileSetKey);
|
|
9044
|
+
if (existing) existing.blocks.push(block);
|
|
9045
|
+
else fileSetKeyToBucket.set(fileSetKey, {
|
|
9046
|
+
files: sortedFiles,
|
|
9047
|
+
blocks: [block]
|
|
9048
|
+
});
|
|
9049
|
+
}
|
|
9050
|
+
const clusters = [];
|
|
9051
|
+
for (const bucket of fileSetKeyToBucket.values()) {
|
|
9052
|
+
const totalDuplicatedLines = bucket.blocks.reduce((runningSum, block) => runningSum + block.lineCount, 0);
|
|
9053
|
+
const totalDuplicatedTokens = bucket.blocks.reduce((runningSum, block) => runningSum + block.tokenCount, 0);
|
|
9054
|
+
clusters.push({
|
|
9055
|
+
files: bucket.files,
|
|
9056
|
+
groups: bucket.blocks,
|
|
9057
|
+
totalDuplicatedLines,
|
|
9058
|
+
totalDuplicatedTokens,
|
|
9059
|
+
suggestions: buildSuggestions(bucket.files, bucket.blocks, totalDuplicatedLines)
|
|
9060
|
+
});
|
|
9061
|
+
}
|
|
9062
|
+
clusters.sort((leftCluster, rightCluster) => {
|
|
9063
|
+
if (leftCluster.totalDuplicatedLines !== rightCluster.totalDuplicatedLines) return rightCluster.totalDuplicatedLines - leftCluster.totalDuplicatedLines;
|
|
9064
|
+
return rightCluster.groups.length - leftCluster.groups.length;
|
|
9065
|
+
});
|
|
9066
|
+
return clusters;
|
|
9067
|
+
};
|
|
9068
|
+
|
|
9069
|
+
//#endregion
|
|
9070
|
+
//#region src/duplicate-blocks/shadowed-directory-pairs.ts
|
|
9071
|
+
const splitDirectoryAndFile = (filePath) => {
|
|
9072
|
+
const trailingSlashIndex = filePath.lastIndexOf("/");
|
|
9073
|
+
if (trailingSlashIndex === -1) return {
|
|
9074
|
+
directory: "",
|
|
9075
|
+
baseName: filePath
|
|
8530
9076
|
};
|
|
8531
|
-
|
|
8532
|
-
|
|
8533
|
-
|
|
8534
|
-
}
|
|
8535
|
-
|
|
8536
|
-
|
|
8537
|
-
|
|
8538
|
-
|
|
8539
|
-
|
|
8540
|
-
|
|
9077
|
+
return {
|
|
9078
|
+
directory: filePath.slice(0, trailingSlashIndex + 1),
|
|
9079
|
+
baseName: filePath.slice(trailingSlashIndex + 1)
|
|
9080
|
+
};
|
|
9081
|
+
};
|
|
9082
|
+
const toRelative = (filePath, rootDir) => {
|
|
9083
|
+
if (filePath.startsWith(rootDir + "/")) return filePath.slice(rootDir.length + 1);
|
|
9084
|
+
if (filePath === rootDir) return "";
|
|
9085
|
+
return filePath;
|
|
9086
|
+
};
|
|
9087
|
+
/**
|
|
9088
|
+
* Collapse N two-file duplicate-block clusters that share the same
|
|
9089
|
+
* `(directoryA, directoryB)` and matching basenames into a single
|
|
9090
|
+
* `ShadowedDirectoryPair` finding — the directories themselves drifted
|
|
9091
|
+
* (e.g. `src/` vs `deno/lib/`, a fork, a copy-paste of a route tree).
|
|
9092
|
+
*/
|
|
9093
|
+
const detectShadowedDirectoryPairs = (duplicateBlockClusters, rootDir) => {
|
|
9094
|
+
const directoryPairBuckets = /* @__PURE__ */ new Map();
|
|
9095
|
+
for (const cluster of duplicateBlockClusters) {
|
|
9096
|
+
if (cluster.files.length !== 2) continue;
|
|
9097
|
+
const [firstFile, secondFile] = cluster.files;
|
|
9098
|
+
const firstSplit = splitDirectoryAndFile(toRelative(firstFile, rootDir));
|
|
9099
|
+
const secondSplit = splitDirectoryAndFile(toRelative(secondFile, rootDir));
|
|
9100
|
+
if (firstSplit.baseName !== secondSplit.baseName) continue;
|
|
9101
|
+
const [smallerDirectory, largerDirectory] = firstSplit.directory <= secondSplit.directory ? [firstSplit.directory, secondSplit.directory] : [secondSplit.directory, firstSplit.directory];
|
|
9102
|
+
const pairKey = `${smallerDirectory}::${largerDirectory}`;
|
|
9103
|
+
const entry = {
|
|
9104
|
+
baseName: firstSplit.baseName,
|
|
9105
|
+
duplicatedLines: cluster.totalDuplicatedLines
|
|
8541
9106
|
};
|
|
9107
|
+
const existing = directoryPairBuckets.get(pairKey);
|
|
9108
|
+
if (existing) existing.push(entry);
|
|
9109
|
+
else directoryPairBuckets.set(pairKey, [entry]);
|
|
9110
|
+
}
|
|
9111
|
+
const shadowedDirectoryPairs = [];
|
|
9112
|
+
for (const [pairKey, entries] of directoryPairBuckets) {
|
|
9113
|
+
if (entries.length < 3) continue;
|
|
9114
|
+
const [directoryA, directoryB] = pairKey.split("::");
|
|
9115
|
+
const sharedBaseNames = [...new Set(entries.map((entry) => entry.baseName))].sort();
|
|
9116
|
+
const totalDuplicatedLines = entries.reduce((runningSum, entry) => runningSum + entry.duplicatedLines, 0);
|
|
9117
|
+
shadowedDirectoryPairs.push({
|
|
9118
|
+
directoryA,
|
|
9119
|
+
directoryB,
|
|
9120
|
+
sharedFiles: sharedBaseNames,
|
|
9121
|
+
totalDuplicatedLines
|
|
9122
|
+
});
|
|
8542
9123
|
}
|
|
8543
|
-
|
|
8544
|
-
|
|
8545
|
-
|
|
8546
|
-
|
|
8547
|
-
|
|
9124
|
+
shadowedDirectoryPairs.sort((leftPair, rightPair) => rightPair.totalDuplicatedLines - leftPair.totalDuplicatedLines);
|
|
9125
|
+
return shadowedDirectoryPairs;
|
|
9126
|
+
};
|
|
9127
|
+
|
|
9128
|
+
//#endregion
|
|
9129
|
+
//#region src/duplicate-blocks/normalize.ts
|
|
9130
|
+
/**
|
|
9131
|
+
* 32-bit FNV-1a. Collisions are tolerable: ties are broken back to the
|
|
9132
|
+
* original (path, offset) tuples downstream, so a rare collision inflates a
|
|
9133
|
+
* duplicate block with one extra spurious instance at worst.
|
|
9134
|
+
*/
|
|
9135
|
+
const FNV_OFFSET_BASIS = 2166136261;
|
|
9136
|
+
const FNV_PRIME = 16777619;
|
|
9137
|
+
const hashString = (input) => {
|
|
9138
|
+
let hash = FNV_OFFSET_BASIS;
|
|
9139
|
+
for (let charIndex = 0; charIndex < input.length; charIndex++) {
|
|
9140
|
+
hash ^= input.charCodeAt(charIndex);
|
|
9141
|
+
hash = Math.imul(hash, FNV_PRIME);
|
|
9142
|
+
}
|
|
9143
|
+
return hash >>> 0;
|
|
9144
|
+
};
|
|
9145
|
+
const resolveNormalization = (mode) => {
|
|
9146
|
+
if (mode === "strict") return {
|
|
9147
|
+
ignoreIdentifiers: false,
|
|
9148
|
+
ignoreStringValues: false,
|
|
9149
|
+
ignoreNumericValues: false
|
|
9150
|
+
};
|
|
9151
|
+
return {
|
|
9152
|
+
ignoreIdentifiers: true,
|
|
9153
|
+
ignoreStringValues: true,
|
|
9154
|
+
ignoreNumericValues: true
|
|
9155
|
+
};
|
|
9156
|
+
};
|
|
9157
|
+
const hashSourceToken = (sourceToken, normalization) => {
|
|
9158
|
+
switch (sourceToken.kind) {
|
|
9159
|
+
case "node-enter": return hashString(`n:${sourceToken.payload}`);
|
|
9160
|
+
case "identifier": return normalization.ignoreIdentifiers ? hashString("id:*") : hashString(`id:${sourceToken.payload}`);
|
|
9161
|
+
case "string-literal": return normalization.ignoreStringValues ? hashString("s:*") : hashString(`s:${sourceToken.payload}`);
|
|
9162
|
+
case "numeric-literal": return normalization.ignoreNumericValues ? hashString("num:*") : hashString(`num:${sourceToken.payload}`);
|
|
9163
|
+
case "boolean-literal": return hashString(`b:${sourceToken.payload}`);
|
|
9164
|
+
case "null-literal": return hashString("null");
|
|
9165
|
+
case "template-literal": return hashString("tpl");
|
|
9166
|
+
case "regexp-literal": return hashString("re");
|
|
9167
|
+
default: return hashString("?");
|
|
9168
|
+
}
|
|
9169
|
+
};
|
|
9170
|
+
const normalizeAndHashTokens = (tokens, mode) => {
|
|
9171
|
+
const normalization = resolveNormalization(mode);
|
|
9172
|
+
const hashedTokens = new Array(tokens.length);
|
|
9173
|
+
for (let tokenIndex = 0; tokenIndex < tokens.length; tokenIndex++) hashedTokens[tokenIndex] = {
|
|
9174
|
+
hash: hashSourceToken(tokens[tokenIndex], normalization),
|
|
9175
|
+
originalIndex: tokenIndex
|
|
9176
|
+
};
|
|
9177
|
+
return hashedTokens;
|
|
9178
|
+
};
|
|
9179
|
+
|
|
9180
|
+
//#endregion
|
|
9181
|
+
//#region src/duplicate-blocks/suffix-array.ts
|
|
9182
|
+
/**
|
|
9183
|
+
* Prefix-doubling suffix array with two-pass radix sort, O(N log N).
|
|
9184
|
+
*
|
|
9185
|
+
* Negative values in `tokenSequence` (file-separator sentinels emitted by
|
|
9186
|
+
* `rankReduceAndConcatenate`) are shifted up so all ranks are >= 0. The
|
|
9187
|
+
* shift preserves the property that sentinels sort before all real ranks,
|
|
9188
|
+
* which is what stops cross-file suffix matches.
|
|
9189
|
+
*/
|
|
9190
|
+
const buildSuffixArray = (tokenSequence) => {
|
|
9191
|
+
const sequenceLength = tokenSequence.length;
|
|
9192
|
+
if (sequenceLength === 0) return [];
|
|
9193
|
+
let minimumValue = 0;
|
|
9194
|
+
for (let scanIndex = 0; scanIndex < sequenceLength; scanIndex++) if (tokenSequence[scanIndex] < minimumValue) minimumValue = tokenSequence[scanIndex];
|
|
9195
|
+
let currentRanks = new Array(sequenceLength);
|
|
9196
|
+
for (let scanIndex = 0; scanIndex < sequenceLength; scanIndex++) currentRanks[scanIndex] = tokenSequence[scanIndex] - minimumValue;
|
|
9197
|
+
let suffixArray = new Array(sequenceLength);
|
|
9198
|
+
for (let positionIndex = 0; positionIndex < sequenceLength; positionIndex++) suffixArray[positionIndex] = positionIndex;
|
|
9199
|
+
let nextRanks = new Array(sequenceLength);
|
|
9200
|
+
let scratchSuffixArray = new Array(sequenceLength);
|
|
9201
|
+
let maximumRank = 0;
|
|
9202
|
+
for (let scanIndex = 0; scanIndex < sequenceLength; scanIndex++) if (currentRanks[scanIndex] > maximumRank) maximumRank = currentRanks[scanIndex];
|
|
9203
|
+
let stride = 1;
|
|
9204
|
+
while (stride < sequenceLength) {
|
|
9205
|
+
const bucketCount = maximumRank + 2;
|
|
9206
|
+
const buckets = new Array(bucketCount + 1).fill(0);
|
|
9207
|
+
for (let suffixIndex = 0; suffixIndex < sequenceLength; suffixIndex++) {
|
|
9208
|
+
const startPosition = suffixArray[suffixIndex];
|
|
9209
|
+
const secondaryKey = startPosition + stride < sequenceLength ? currentRanks[startPosition + stride] + 1 : 0;
|
|
9210
|
+
buckets[secondaryKey]++;
|
|
9211
|
+
}
|
|
9212
|
+
let prefixSum = 0;
|
|
9213
|
+
for (let bucketIndex = 0; bucketIndex < buckets.length; bucketIndex++) {
|
|
9214
|
+
const bucketCountValue = buckets[bucketIndex];
|
|
9215
|
+
buckets[bucketIndex] = prefixSum;
|
|
9216
|
+
prefixSum += bucketCountValue;
|
|
9217
|
+
}
|
|
9218
|
+
for (let suffixIndex = 0; suffixIndex < sequenceLength; suffixIndex++) {
|
|
9219
|
+
const startPosition = suffixArray[suffixIndex];
|
|
9220
|
+
const secondaryKey = startPosition + stride < sequenceLength ? currentRanks[startPosition + stride] + 1 : 0;
|
|
9221
|
+
scratchSuffixArray[buckets[secondaryKey]] = startPosition;
|
|
9222
|
+
buckets[secondaryKey]++;
|
|
9223
|
+
}
|
|
9224
|
+
for (let bucketIndex = 0; bucketIndex < buckets.length; bucketIndex++) buckets[bucketIndex] = 0;
|
|
9225
|
+
for (let suffixIndex = 0; suffixIndex < sequenceLength; suffixIndex++) {
|
|
9226
|
+
const startPosition = scratchSuffixArray[suffixIndex];
|
|
9227
|
+
buckets[currentRanks[startPosition]]++;
|
|
9228
|
+
}
|
|
9229
|
+
prefixSum = 0;
|
|
9230
|
+
for (let bucketIndex = 0; bucketIndex < buckets.length; bucketIndex++) {
|
|
9231
|
+
const bucketCountValue = buckets[bucketIndex];
|
|
9232
|
+
buckets[bucketIndex] = prefixSum;
|
|
9233
|
+
prefixSum += bucketCountValue;
|
|
9234
|
+
}
|
|
9235
|
+
for (let suffixIndex = 0; suffixIndex < sequenceLength; suffixIndex++) {
|
|
9236
|
+
const startPosition = scratchSuffixArray[suffixIndex];
|
|
9237
|
+
suffixArray[buckets[currentRanks[startPosition]]] = startPosition;
|
|
9238
|
+
buckets[currentRanks[startPosition]]++;
|
|
9239
|
+
}
|
|
9240
|
+
nextRanks[suffixArray[0]] = 0;
|
|
9241
|
+
for (let suffixIndex = 1; suffixIndex < sequenceLength; suffixIndex++) {
|
|
9242
|
+
const previousStart = suffixArray[suffixIndex - 1];
|
|
9243
|
+
const currentStart = suffixArray[suffixIndex];
|
|
9244
|
+
const previousSecondary = previousStart + stride < sequenceLength ? currentRanks[previousStart + stride] : -1;
|
|
9245
|
+
const currentSecondary = currentStart + stride < sequenceLength ? currentRanks[currentStart + stride] : -1;
|
|
9246
|
+
const isSameBucket = currentRanks[previousStart] === currentRanks[currentStart] && previousSecondary === currentSecondary;
|
|
9247
|
+
nextRanks[currentStart] = nextRanks[previousStart] + (isSameBucket ? 0 : 1);
|
|
9248
|
+
}
|
|
9249
|
+
const newMaximumRank = nextRanks[suffixArray[sequenceLength - 1]];
|
|
9250
|
+
[currentRanks, nextRanks] = [nextRanks, currentRanks];
|
|
9251
|
+
if (newMaximumRank === sequenceLength - 1) break;
|
|
9252
|
+
maximumRank = newMaximumRank;
|
|
9253
|
+
stride *= 2;
|
|
9254
|
+
}
|
|
9255
|
+
return suffixArray;
|
|
9256
|
+
};
|
|
9257
|
+
/**
|
|
9258
|
+
* Kasai's O(N) longest-common-prefix array. The `>= 0` check inside the inner
|
|
9259
|
+
* loop is the only non-textbook bit: it prevents a real-token LCP from
|
|
9260
|
+
* accidentally crossing a sentinel boundary (sentinels are negative).
|
|
9261
|
+
*/
|
|
9262
|
+
const buildLcpArray = (tokenSequence, suffixArray) => {
|
|
9263
|
+
const sequenceLength = tokenSequence.length;
|
|
9264
|
+
const inverseSuffixArray = new Array(sequenceLength);
|
|
9265
|
+
for (let arrayIndex = 0; arrayIndex < sequenceLength; arrayIndex++) inverseSuffixArray[suffixArray[arrayIndex]] = arrayIndex;
|
|
9266
|
+
const lcpArray = new Array(sequenceLength).fill(0);
|
|
9267
|
+
let runningLcp = 0;
|
|
9268
|
+
for (let positionIndex = 0; positionIndex < sequenceLength; positionIndex++) {
|
|
9269
|
+
if (inverseSuffixArray[positionIndex] === 0) {
|
|
9270
|
+
runningLcp = 0;
|
|
9271
|
+
continue;
|
|
9272
|
+
}
|
|
9273
|
+
const previousStart = suffixArray[inverseSuffixArray[positionIndex] - 1];
|
|
9274
|
+
while (positionIndex + runningLcp < sequenceLength && previousStart + runningLcp < sequenceLength && tokenSequence[positionIndex + runningLcp] === tokenSequence[previousStart + runningLcp] && tokenSequence[positionIndex + runningLcp] >= 0) runningLcp++;
|
|
9275
|
+
lcpArray[inverseSuffixArray[positionIndex]] = runningLcp;
|
|
9276
|
+
if (runningLcp > 0) runningLcp--;
|
|
9277
|
+
}
|
|
9278
|
+
return lcpArray;
|
|
9279
|
+
};
|
|
9280
|
+
|
|
9281
|
+
//#endregion
|
|
9282
|
+
//#region src/utils/is-ast-node.ts
|
|
9283
|
+
const isAstNode = (candidate) => typeof candidate === "object" && candidate !== null && "type" in candidate;
|
|
9284
|
+
|
|
9285
|
+
//#endregion
|
|
9286
|
+
//#region src/duplicate-blocks/token-visitor.ts
|
|
9287
|
+
const NODES_DROPPED_FROM_TOKEN_STREAM = new Set([
|
|
9288
|
+
"ImportDeclaration",
|
|
9289
|
+
"ExportAllDeclaration",
|
|
9290
|
+
"TSTypeAnnotation",
|
|
9291
|
+
"TSTypeAliasDeclaration",
|
|
9292
|
+
"TSInterfaceDeclaration",
|
|
9293
|
+
"TSTypeParameterDeclaration",
|
|
9294
|
+
"TSTypeParameterInstantiation",
|
|
9295
|
+
"TSTypeReference",
|
|
9296
|
+
"TSAnyKeyword",
|
|
9297
|
+
"TSUnknownKeyword",
|
|
9298
|
+
"TSStringKeyword",
|
|
9299
|
+
"TSNumberKeyword",
|
|
9300
|
+
"TSBooleanKeyword",
|
|
9301
|
+
"TSVoidKeyword",
|
|
9302
|
+
"TSUndefinedKeyword",
|
|
9303
|
+
"TSNullKeyword",
|
|
9304
|
+
"TSNeverKeyword",
|
|
9305
|
+
"TSUnionType",
|
|
9306
|
+
"TSIntersectionType",
|
|
9307
|
+
"TSLiteralType",
|
|
9308
|
+
"TSArrayType",
|
|
9309
|
+
"TSTupleType",
|
|
9310
|
+
"TSTypeLiteral",
|
|
9311
|
+
"TSPropertySignature",
|
|
9312
|
+
"TSMethodSignature",
|
|
9313
|
+
"TSCallSignatureDeclaration",
|
|
9314
|
+
"TSConstructSignatureDeclaration",
|
|
9315
|
+
"TSIndexSignature",
|
|
9316
|
+
"TSConditionalType",
|
|
9317
|
+
"TSMappedType",
|
|
9318
|
+
"TSInferType",
|
|
9319
|
+
"TSImportType",
|
|
9320
|
+
"TSQualifiedName",
|
|
9321
|
+
"TSTypeOperator",
|
|
9322
|
+
"TSTypePredicate",
|
|
9323
|
+
"TSFunctionType",
|
|
9324
|
+
"TSConstructorType"
|
|
9325
|
+
]);
|
|
9326
|
+
const visitChildrenRaw = (node, visit) => {
|
|
9327
|
+
if (!isAstNode(node)) return;
|
|
9328
|
+
for (const key of Object.keys(node)) {
|
|
9329
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") continue;
|
|
9330
|
+
const value = node[key];
|
|
9331
|
+
if (Array.isArray(value)) for (const item of value) visit(item);
|
|
9332
|
+
else if (value !== null && typeof value === "object") visit(value);
|
|
9333
|
+
}
|
|
9334
|
+
};
|
|
9335
|
+
const safeNumberOrZero = (candidate) => typeof candidate === "number" ? candidate : 0;
|
|
9336
|
+
/**
|
|
9337
|
+
* Walk an oxc AST and emit a flat token stream suitable for suffix-array-based
|
|
9338
|
+
* duplicate-block detection. Two structurally-identical regions of code produce the same
|
|
9339
|
+
* token sequence (modulo identifier/literal-value normalization, applied later
|
|
9340
|
+
* in `normalize.ts`).
|
|
9341
|
+
*
|
|
9342
|
+
* Implementation note: rather than a hand-written keyword/operator lexer-style
|
|
9343
|
+
* visitor, we walk the AST generically and emit one `node-enter` token per
|
|
9344
|
+
* visited node. This trades a slightly different token-density profile for
|
|
9345
|
+
* less code. AST-shape tokens still distinguish
|
|
9346
|
+
* `function add(a, b) { return a + b }` from `const add = (a, b) => a + b`.
|
|
9347
|
+
* Identifiers and value literals get dedicated tokens so semantic-mode
|
|
9348
|
+
* normalization can blind them.
|
|
9349
|
+
*
|
|
9350
|
+
* Imports and type-only constructs are dropped to keep import-block boilerplate
|
|
9351
|
+
* and ambient type declarations from inflating the noise floor.
|
|
9352
|
+
*/
|
|
9353
|
+
const tokenizeAst = (program) => {
|
|
9354
|
+
const tokens = [];
|
|
9355
|
+
const visit = (node) => {
|
|
9356
|
+
if (!isAstNode(node)) return;
|
|
9357
|
+
const nodeType = node.type;
|
|
9358
|
+
if (NODES_DROPPED_FROM_TOKEN_STREAM.has(nodeType)) return;
|
|
9359
|
+
const start = safeNumberOrZero(node.start);
|
|
9360
|
+
const end = safeNumberOrZero(node.end);
|
|
9361
|
+
if (nodeType === "Identifier" || nodeType === "PrivateIdentifier") {
|
|
9362
|
+
const identifierName = node.name;
|
|
9363
|
+
tokens.push({
|
|
9364
|
+
kind: "identifier",
|
|
9365
|
+
payload: typeof identifierName === "string" ? identifierName : "",
|
|
9366
|
+
start,
|
|
9367
|
+
end
|
|
9368
|
+
});
|
|
9369
|
+
return;
|
|
9370
|
+
}
|
|
9371
|
+
if (nodeType === "Literal") {
|
|
9372
|
+
const literalValue = node.value;
|
|
9373
|
+
if (typeof literalValue === "string") tokens.push({
|
|
9374
|
+
kind: "string-literal",
|
|
9375
|
+
payload: literalValue,
|
|
9376
|
+
start,
|
|
9377
|
+
end
|
|
9378
|
+
});
|
|
9379
|
+
else if (typeof literalValue === "number") tokens.push({
|
|
9380
|
+
kind: "numeric-literal",
|
|
9381
|
+
payload: String(literalValue),
|
|
9382
|
+
start,
|
|
9383
|
+
end
|
|
9384
|
+
});
|
|
9385
|
+
else if (typeof literalValue === "boolean") tokens.push({
|
|
9386
|
+
kind: "boolean-literal",
|
|
9387
|
+
payload: literalValue ? "true" : "false",
|
|
9388
|
+
start,
|
|
9389
|
+
end
|
|
9390
|
+
});
|
|
9391
|
+
else if (literalValue === null) tokens.push({
|
|
9392
|
+
kind: "null-literal",
|
|
9393
|
+
payload: "null",
|
|
9394
|
+
start,
|
|
9395
|
+
end
|
|
9396
|
+
});
|
|
9397
|
+
else if (node.regex) tokens.push({
|
|
9398
|
+
kind: "regexp-literal",
|
|
9399
|
+
payload: "regex",
|
|
9400
|
+
start,
|
|
9401
|
+
end
|
|
9402
|
+
});
|
|
9403
|
+
else tokens.push({
|
|
9404
|
+
kind: "node-enter",
|
|
9405
|
+
payload: nodeType,
|
|
9406
|
+
start,
|
|
9407
|
+
end
|
|
9408
|
+
});
|
|
9409
|
+
return;
|
|
9410
|
+
}
|
|
9411
|
+
if (nodeType === "TemplateLiteral") {
|
|
9412
|
+
tokens.push({
|
|
9413
|
+
kind: "template-literal",
|
|
9414
|
+
payload: "tpl",
|
|
9415
|
+
start,
|
|
9416
|
+
end
|
|
9417
|
+
});
|
|
9418
|
+
visitChildrenRaw(node, visit);
|
|
9419
|
+
return;
|
|
9420
|
+
}
|
|
9421
|
+
tokens.push({
|
|
9422
|
+
kind: "node-enter",
|
|
9423
|
+
payload: nodeType,
|
|
9424
|
+
start,
|
|
9425
|
+
end
|
|
9426
|
+
});
|
|
9427
|
+
visitChildrenRaw(node, visit);
|
|
9428
|
+
};
|
|
9429
|
+
visit(program);
|
|
9430
|
+
return tokens;
|
|
9431
|
+
};
|
|
9432
|
+
|
|
9433
|
+
//#endregion
|
|
9434
|
+
//#region src/duplicate-blocks/index.ts
|
|
9435
|
+
const isBinaryFile = (sourceText) => {
|
|
9436
|
+
const sampleEnd = Math.min(sourceText.length, BINARY_DETECTION_SAMPLE_BYTES);
|
|
9437
|
+
let nullByteCount = 0;
|
|
9438
|
+
for (let charIndex = 0; charIndex < sampleEnd; charIndex++) if (sourceText.charCodeAt(charIndex) === 0) {
|
|
9439
|
+
nullByteCount++;
|
|
9440
|
+
if (nullByteCount >= 4) return true;
|
|
9441
|
+
}
|
|
9442
|
+
return false;
|
|
9443
|
+
};
|
|
9444
|
+
const isMinifiedSource = (sourceText) => {
|
|
9445
|
+
if (sourceText.length < 5e3) return false;
|
|
9446
|
+
const lineCount = (sourceText.match(/\n/g)?.length ?? 0) + 1;
|
|
9447
|
+
return sourceText.length / lineCount > 500;
|
|
9448
|
+
};
|
|
9449
|
+
const tokenizeFile = (filePath) => {
|
|
9450
|
+
let sourceStat;
|
|
9451
|
+
try {
|
|
9452
|
+
sourceStat = (0, node_fs.statSync)(filePath);
|
|
9453
|
+
} catch {
|
|
9454
|
+
return;
|
|
9455
|
+
}
|
|
9456
|
+
if (sourceStat.size > 2e6) return void 0;
|
|
9457
|
+
let sourceText;
|
|
9458
|
+
try {
|
|
9459
|
+
sourceText = (0, node_fs.readFileSync)(filePath, "utf-8");
|
|
9460
|
+
} catch {
|
|
9461
|
+
return;
|
|
9462
|
+
}
|
|
9463
|
+
if (sourceText.length === 0) return void 0;
|
|
9464
|
+
if (isBinaryFile(sourceText)) return void 0;
|
|
9465
|
+
if (isMinifiedSource(sourceText)) return void 0;
|
|
9466
|
+
let parseResult;
|
|
9467
|
+
try {
|
|
9468
|
+
parseResult = (0, oxc_parser.parseSync)(filePath, sourceText);
|
|
9469
|
+
} catch {
|
|
9470
|
+
return;
|
|
9471
|
+
}
|
|
9472
|
+
const sourceTokens = tokenizeAst(parseResult.program);
|
|
9473
|
+
if (sourceTokens.length === 0) return void 0;
|
|
9474
|
+
const lineStarts = computeLineStarts(sourceText);
|
|
9475
|
+
return {
|
|
9476
|
+
path: filePath,
|
|
9477
|
+
sourceTokens,
|
|
9478
|
+
lineStarts,
|
|
9479
|
+
lineCount: lineStarts.length
|
|
9480
|
+
};
|
|
9481
|
+
};
|
|
9482
|
+
const buildCloneInstance = (rawInstance, tokenLength, tokenizedFiles) => {
|
|
9483
|
+
const file = tokenizedFiles[rawInstance.fileIndex];
|
|
9484
|
+
const firstToken = file.sourceTokens[rawInstance.tokenOffsetWithinFile];
|
|
9485
|
+
const lastToken = file.sourceTokens[rawInstance.tokenOffsetWithinFile + tokenLength - 1];
|
|
9486
|
+
const startSpan = offsetToLineColumn(firstToken.start, file.lineStarts);
|
|
9487
|
+
const endSpan = offsetToLineColumn(lastToken.end, file.lineStarts);
|
|
9488
|
+
return {
|
|
9489
|
+
path: file.path,
|
|
9490
|
+
startLine: startSpan.line,
|
|
9491
|
+
endLine: endSpan.line,
|
|
9492
|
+
startColumn: startSpan.column,
|
|
9493
|
+
endColumn: endSpan.column
|
|
9494
|
+
};
|
|
9495
|
+
};
|
|
9496
|
+
const directoryOf = (filePath) => (0, node_path.dirname)(filePath);
|
|
9497
|
+
const filterRawBlocksToReportableDuplicates = (rawBlocks, tokenizedFiles, config) => {
|
|
9498
|
+
const duplicateBlocks = [];
|
|
9499
|
+
for (const rawBlock of rawBlocks) {
|
|
9500
|
+
const instances = rawBlock.instances.map((rawInstance) => buildCloneInstance(rawInstance, rawBlock.tokenLength, tokenizedFiles));
|
|
9501
|
+
let lineCount = 0;
|
|
9502
|
+
for (const instance of instances) {
|
|
9503
|
+
const instanceLineCount = instance.endLine - instance.startLine + 1;
|
|
9504
|
+
if (instanceLineCount > lineCount) lineCount = instanceLineCount;
|
|
9505
|
+
}
|
|
9506
|
+
if (lineCount < config.minLines) continue;
|
|
9507
|
+
if (instances.length < config.minOccurrences) continue;
|
|
9508
|
+
if (config.skipLocal) {
|
|
9509
|
+
if (new Set(instances.map((instance) => directoryOf(instance.path))).size < 2) continue;
|
|
9510
|
+
}
|
|
9511
|
+
const distinctFiles = new Set(instances.map((instance) => instance.path));
|
|
9512
|
+
const confidence = distinctFiles.size >= 2 ? "high" : "medium";
|
|
9513
|
+
duplicateBlocks.push({
|
|
9514
|
+
instances,
|
|
9515
|
+
tokenCount: rawBlock.tokenLength,
|
|
9516
|
+
lineCount,
|
|
9517
|
+
confidence,
|
|
9518
|
+
reason: distinctFiles.size >= 2 ? `${instances.length} occurrences spanning ${distinctFiles.size} files (≥${rawBlock.tokenLength} tokens, ${lineCount} lines)` : `${instances.length} occurrences within a single file (≥${rawBlock.tokenLength} tokens, ${lineCount} lines)`
|
|
9519
|
+
});
|
|
9520
|
+
}
|
|
9521
|
+
const maximalBlocks = dropBlocksSubsumedByLongerSibling(duplicateBlocks);
|
|
9522
|
+
maximalBlocks.sort((firstClone, secondClone) => {
|
|
9523
|
+
if (firstClone.lineCount !== secondClone.lineCount) return secondClone.lineCount - firstClone.lineCount;
|
|
9524
|
+
return secondClone.tokenCount - firstClone.tokenCount;
|
|
9525
|
+
});
|
|
9526
|
+
return maximalBlocks;
|
|
9527
|
+
};
|
|
9528
|
+
/**
|
|
9529
|
+
* The suffix-array + LCP-interval scan emits one block per LCP interval, but
|
|
9530
|
+
* nested intervals routinely yield the same set of source spans at multiple
|
|
9531
|
+
* lengths (the same maximal repeat reported at L, L-1, L-2, …). Drop any
|
|
9532
|
+
* block whose every instance is spatially contained inside some other block's
|
|
9533
|
+
* matching instance — that other block is strictly more informative.
|
|
9534
|
+
*
|
|
9535
|
+
* O(N²) worst-case, but N here is post-filter blocks (typically <1000 even on
|
|
9536
|
+
* large monorepos), and the early-exit on instance-count mismatch keeps it
|
|
9537
|
+
* tight in practice.
|
|
9538
|
+
*/
|
|
9539
|
+
const dropBlocksSubsumedByLongerSibling = (blocks) => {
|
|
9540
|
+
const sorted = [...blocks].sort((firstBlock, secondBlock) => {
|
|
9541
|
+
if (firstBlock.tokenCount !== secondBlock.tokenCount) return secondBlock.tokenCount - firstBlock.tokenCount;
|
|
9542
|
+
return secondBlock.lineCount - firstBlock.lineCount;
|
|
9543
|
+
});
|
|
9544
|
+
const survivors = [];
|
|
9545
|
+
for (const candidate of sorted) {
|
|
9546
|
+
let subsumed = false;
|
|
9547
|
+
for (const survivor of survivors) {
|
|
9548
|
+
if (survivor.instances.length !== candidate.instances.length) continue;
|
|
9549
|
+
if (allInstancesContainedIn(candidate, survivor)) {
|
|
9550
|
+
subsumed = true;
|
|
9551
|
+
break;
|
|
9552
|
+
}
|
|
9553
|
+
}
|
|
9554
|
+
if (!subsumed) survivors.push(candidate);
|
|
9555
|
+
}
|
|
9556
|
+
return survivors;
|
|
9557
|
+
};
|
|
9558
|
+
const allInstancesContainedIn = (candidate, longer) => {
|
|
9559
|
+
for (const candidateInstance of candidate.instances) {
|
|
9560
|
+
let matched = false;
|
|
9561
|
+
for (const longerInstance of longer.instances) if (candidateInstance.path === longerInstance.path && isSpanContained(candidateInstance, longerInstance)) {
|
|
9562
|
+
matched = true;
|
|
9563
|
+
break;
|
|
9564
|
+
}
|
|
9565
|
+
if (!matched) return false;
|
|
9566
|
+
}
|
|
9567
|
+
return true;
|
|
9568
|
+
};
|
|
9569
|
+
const isSpanContained = (inner, outer) => {
|
|
9570
|
+
const innerStartsAfterOuter = inner.startLine > outer.startLine || inner.startLine === outer.startLine && inner.startColumn >= outer.startColumn;
|
|
9571
|
+
const innerEndsBeforeOuter = inner.endLine < outer.endLine || inner.endLine === outer.endLine && inner.endColumn <= outer.endColumn;
|
|
9572
|
+
return innerStartsAfterOuter && innerEndsBeforeOuter;
|
|
9573
|
+
};
|
|
9574
|
+
/**
|
|
9575
|
+
* Token-based duplicate block detector.
|
|
9576
|
+
*
|
|
9577
|
+
* Pipeline:
|
|
9578
|
+
* 1. Tokenize each file with the AST visitor in `token-visitor.ts`
|
|
9579
|
+
* 2. Hash + normalize tokens with the chosen detection mode
|
|
9580
|
+
* 3. Concatenate every file's hashed tokens with unique negative sentinels
|
|
9581
|
+
* 4. Build a suffix array (prefix doubling + radix sort) and LCP array
|
|
9582
|
+
* 5. Stack-based LCP-interval scan extracts maximal duplicate blocks
|
|
9583
|
+
* 6. Filter on min-tokens / min-lines / min-occurrences / skip-local
|
|
9584
|
+
* 7. Group clones into families; collapse N two-file families with matching
|
|
9585
|
+
* basenames into a `ShadowedDirectoryPair` finding
|
|
9586
|
+
*
|
|
9587
|
+
* Returns empty arrays when `config.enabled` is false.
|
|
9588
|
+
*/
|
|
9589
|
+
const detectDuplicateBlocks = (graph, config, rootDir) => {
|
|
9590
|
+
if (!config || !config.enabled) return {
|
|
9591
|
+
duplicateBlocks: [],
|
|
9592
|
+
duplicateBlockClusters: [],
|
|
9593
|
+
shadowedDirectoryPairs: []
|
|
9594
|
+
};
|
|
9595
|
+
const tokenizedFiles = [];
|
|
9596
|
+
for (const module of graph.modules) {
|
|
9597
|
+
if (module.isDeclarationFile) continue;
|
|
9598
|
+
if (module.isConfigFile) continue;
|
|
9599
|
+
const tokenizedFile = tokenizeFile(module.fileId.path);
|
|
9600
|
+
if (!tokenizedFile) continue;
|
|
9601
|
+
tokenizedFiles.push(tokenizedFile);
|
|
9602
|
+
}
|
|
9603
|
+
if (tokenizedFiles.length === 0) return {
|
|
9604
|
+
duplicateBlocks: [],
|
|
9605
|
+
duplicateBlockClusters: [],
|
|
9606
|
+
shadowedDirectoryPairs: []
|
|
9607
|
+
};
|
|
9608
|
+
const filesHashedTokens = tokenizedFiles.map((file) => normalizeAndHashTokens(file.sourceTokens, config.mode));
|
|
9609
|
+
const filesTokenCounts = filesHashedTokens.map((fileTokens) => fileTokens.length);
|
|
9610
|
+
if (!filesTokenCounts.some((count) => count >= config.minTokens)) return {
|
|
9611
|
+
duplicateBlocks: [],
|
|
9612
|
+
duplicateBlockClusters: [],
|
|
9613
|
+
shadowedDirectoryPairs: []
|
|
9614
|
+
};
|
|
9615
|
+
const concatenation = rankReduceAndConcatenate(filesHashedTokens);
|
|
9616
|
+
if (concatenation.tokenSequence.length === 0) return {
|
|
9617
|
+
duplicateBlocks: [],
|
|
9618
|
+
duplicateBlockClusters: [],
|
|
9619
|
+
shadowedDirectoryPairs: []
|
|
9620
|
+
};
|
|
9621
|
+
const suffixArray = buildSuffixArray(concatenation.tokenSequence);
|
|
9622
|
+
const duplicateBlocks = filterRawBlocksToReportableDuplicates(extractRawDuplicateBlocks(suffixArray, buildLcpArray(concatenation.tokenSequence, suffixArray), concatenation.fileOf, concatenation.fileOffsets, filesTokenCounts, config.minTokens), tokenizedFiles, config);
|
|
9623
|
+
const duplicateBlockClusters = groupDuplicateBlocksIntoClusters(duplicateBlocks);
|
|
9624
|
+
return {
|
|
9625
|
+
duplicateBlocks,
|
|
9626
|
+
duplicateBlockClusters,
|
|
9627
|
+
shadowedDirectoryPairs: detectShadowedDirectoryPairs(duplicateBlockClusters, rootDir)
|
|
9628
|
+
};
|
|
9629
|
+
};
|
|
9630
|
+
|
|
9631
|
+
//#endregion
|
|
9632
|
+
//#region src/report/re-export-cycles.ts
|
|
9633
|
+
/**
|
|
9634
|
+
* Reports cycles in the subgraph of `isReExportEdge` edges only. These are
|
|
9635
|
+
* a strict subset of `circularDependencies` but worth separating: every
|
|
9636
|
+
* general cycle can have a legitimate bidirectional-collaboration reason,
|
|
9637
|
+
* but a re-export cycle has none — it always tanks tree-shaking and risks
|
|
9638
|
+
* the "Cannot access X before initialization" TDZ runtime error.
|
|
9639
|
+
*/
|
|
9640
|
+
const detectReExportCycles = (graph) => {
|
|
9641
|
+
const adjacency = Array.from({ length: graph.modules.length }, () => []);
|
|
9642
|
+
const reExportTargetSets = Array.from({ length: graph.modules.length }, () => /* @__PURE__ */ new Set());
|
|
9643
|
+
for (const edge of graph.edges) {
|
|
9644
|
+
if (!edge.isReExportEdge) continue;
|
|
9645
|
+
if (edge.target >= graph.modules.length) continue;
|
|
9646
|
+
if (reExportTargetSets[edge.source].has(edge.target)) continue;
|
|
9647
|
+
reExportTargetSets[edge.source].add(edge.target);
|
|
9648
|
+
adjacency[edge.source].push(edge.target);
|
|
9649
|
+
}
|
|
9650
|
+
const sccComponents = computeStronglyConnectedComponents(adjacency);
|
|
9651
|
+
const findings = [];
|
|
9652
|
+
for (const component of sccComponents) {
|
|
9653
|
+
if (component.length === 1) {
|
|
9654
|
+
const onlyNode = component[0];
|
|
9655
|
+
if (!adjacency[onlyNode].includes(onlyNode)) continue;
|
|
9656
|
+
const filePath = graph.modules[onlyNode].fileId.path;
|
|
9657
|
+
findings.push({
|
|
9658
|
+
files: [filePath],
|
|
9659
|
+
kind: "self-loop",
|
|
9660
|
+
confidence: "high",
|
|
9661
|
+
reason: `${filePath} re-exports from itself — the barrel imports its own root, which breaks bundler tree-shaking and risks TDZ runtime errors`
|
|
9662
|
+
});
|
|
9663
|
+
continue;
|
|
9664
|
+
}
|
|
9665
|
+
const sortedFiles = component.map((moduleIndex) => graph.modules[moduleIndex].fileId.path).sort();
|
|
9666
|
+
findings.push({
|
|
9667
|
+
files: sortedFiles,
|
|
9668
|
+
kind: "multi-node",
|
|
9669
|
+
confidence: "high",
|
|
9670
|
+
reason: `${sortedFiles.length} modules form a re-export cycle — refactor consumers to import from the leaf module instead of the barrel`
|
|
9671
|
+
});
|
|
9672
|
+
}
|
|
9673
|
+
findings.sort((firstFinding, secondFinding) => firstFinding.files[0].localeCompare(secondFinding.files[0]));
|
|
9674
|
+
return findings;
|
|
9675
|
+
};
|
|
9676
|
+
/**
|
|
9677
|
+
* Iterative Tarjan's SCC. Singleton components are returned too so the
|
|
9678
|
+
* caller can distinguish a real self-loop from a node with no edges.
|
|
9679
|
+
*/
|
|
9680
|
+
const computeStronglyConnectedComponents = (adjacency) => {
|
|
9681
|
+
const nodeCount = adjacency.length;
|
|
9682
|
+
if (nodeCount === 0) return [];
|
|
9683
|
+
const indices = new Array(nodeCount).fill(-1);
|
|
9684
|
+
const lowLinks = new Array(nodeCount).fill(0);
|
|
9685
|
+
const onStack = new Array(nodeCount).fill(false);
|
|
9686
|
+
const tarjanStack = [];
|
|
9687
|
+
const components = [];
|
|
9688
|
+
let nextIndex = 0;
|
|
9689
|
+
for (let startNode = 0; startNode < nodeCount; startNode++) {
|
|
9690
|
+
if (indices[startNode] !== -1) continue;
|
|
9691
|
+
const dfsStack = [{
|
|
9692
|
+
node: startNode,
|
|
9693
|
+
successorPosition: 0
|
|
9694
|
+
}];
|
|
9695
|
+
indices[startNode] = nextIndex;
|
|
9696
|
+
lowLinks[startNode] = nextIndex;
|
|
9697
|
+
nextIndex++;
|
|
9698
|
+
onStack[startNode] = true;
|
|
9699
|
+
tarjanStack.push(startNode);
|
|
9700
|
+
while (dfsStack.length > 0) {
|
|
9701
|
+
const frame = dfsStack[dfsStack.length - 1];
|
|
9702
|
+
const successors = adjacency[frame.node];
|
|
9703
|
+
if (frame.successorPosition < successors.length) {
|
|
9704
|
+
const successorNode = successors[frame.successorPosition];
|
|
9705
|
+
frame.successorPosition++;
|
|
9706
|
+
if (indices[successorNode] === -1) {
|
|
9707
|
+
indices[successorNode] = nextIndex;
|
|
9708
|
+
lowLinks[successorNode] = nextIndex;
|
|
9709
|
+
nextIndex++;
|
|
9710
|
+
onStack[successorNode] = true;
|
|
9711
|
+
tarjanStack.push(successorNode);
|
|
9712
|
+
dfsStack.push({
|
|
9713
|
+
node: successorNode,
|
|
9714
|
+
successorPosition: 0
|
|
9715
|
+
});
|
|
9716
|
+
} else if (onStack[successorNode]) {
|
|
9717
|
+
if (indices[successorNode] < lowLinks[frame.node]) lowLinks[frame.node] = indices[successorNode];
|
|
9718
|
+
}
|
|
9719
|
+
} else {
|
|
9720
|
+
if (lowLinks[frame.node] === indices[frame.node]) {
|
|
9721
|
+
const component = [];
|
|
9722
|
+
let popped;
|
|
9723
|
+
do {
|
|
9724
|
+
popped = tarjanStack.pop();
|
|
9725
|
+
onStack[popped] = false;
|
|
9726
|
+
component.push(popped);
|
|
9727
|
+
} while (popped !== frame.node);
|
|
9728
|
+
components.push(component);
|
|
9729
|
+
}
|
|
9730
|
+
dfsStack.pop();
|
|
9731
|
+
if (dfsStack.length > 0) {
|
|
9732
|
+
const parent = dfsStack[dfsStack.length - 1];
|
|
9733
|
+
if (lowLinks[frame.node] < lowLinks[parent.node]) lowLinks[parent.node] = lowLinks[frame.node];
|
|
9734
|
+
}
|
|
9735
|
+
}
|
|
9736
|
+
}
|
|
9737
|
+
}
|
|
9738
|
+
return components;
|
|
9739
|
+
};
|
|
9740
|
+
|
|
9741
|
+
//#endregion
|
|
9742
|
+
//#region src/report/feature-flags.ts
|
|
9743
|
+
const BUILTIN_SDK_PATTERNS = [
|
|
9744
|
+
{
|
|
9745
|
+
functionName: "useFlag",
|
|
9746
|
+
nameArgIndex: 0,
|
|
9747
|
+
provider: "LaunchDarkly"
|
|
9748
|
+
},
|
|
9749
|
+
{
|
|
9750
|
+
functionName: "useLDFlag",
|
|
9751
|
+
nameArgIndex: 0,
|
|
9752
|
+
provider: "LaunchDarkly"
|
|
9753
|
+
},
|
|
9754
|
+
{
|
|
9755
|
+
functionName: "useFeatureFlag",
|
|
9756
|
+
nameArgIndex: 0,
|
|
9757
|
+
provider: "LaunchDarkly"
|
|
9758
|
+
},
|
|
9759
|
+
{
|
|
9760
|
+
functionName: "variation",
|
|
9761
|
+
nameArgIndex: 0,
|
|
9762
|
+
provider: "LaunchDarkly"
|
|
9763
|
+
},
|
|
9764
|
+
{
|
|
9765
|
+
functionName: "boolVariation",
|
|
9766
|
+
nameArgIndex: 0,
|
|
9767
|
+
provider: "LaunchDarkly"
|
|
9768
|
+
},
|
|
9769
|
+
{
|
|
9770
|
+
functionName: "stringVariation",
|
|
9771
|
+
nameArgIndex: 0,
|
|
9772
|
+
provider: "LaunchDarkly"
|
|
9773
|
+
},
|
|
9774
|
+
{
|
|
9775
|
+
functionName: "numberVariation",
|
|
9776
|
+
nameArgIndex: 0,
|
|
9777
|
+
provider: "LaunchDarkly"
|
|
9778
|
+
},
|
|
9779
|
+
{
|
|
9780
|
+
functionName: "jsonVariation",
|
|
9781
|
+
nameArgIndex: 0,
|
|
9782
|
+
provider: "LaunchDarkly"
|
|
9783
|
+
},
|
|
9784
|
+
{
|
|
9785
|
+
functionName: "useGate",
|
|
9786
|
+
nameArgIndex: 0,
|
|
9787
|
+
provider: "Statsig"
|
|
9788
|
+
},
|
|
9789
|
+
{
|
|
9790
|
+
functionName: "checkGate",
|
|
9791
|
+
nameArgIndex: 0,
|
|
9792
|
+
provider: "Statsig"
|
|
9793
|
+
},
|
|
9794
|
+
{
|
|
9795
|
+
functionName: "useExperiment",
|
|
9796
|
+
nameArgIndex: 0,
|
|
9797
|
+
provider: "Statsig"
|
|
9798
|
+
},
|
|
9799
|
+
{
|
|
9800
|
+
functionName: "useConfig",
|
|
9801
|
+
nameArgIndex: 0,
|
|
9802
|
+
provider: "Statsig"
|
|
9803
|
+
},
|
|
9804
|
+
{
|
|
9805
|
+
functionName: "isEnabled",
|
|
9806
|
+
nameArgIndex: 0,
|
|
9807
|
+
provider: "Unleash"
|
|
9808
|
+
},
|
|
9809
|
+
{
|
|
9810
|
+
functionName: "getVariant",
|
|
9811
|
+
nameArgIndex: 0,
|
|
9812
|
+
provider: "Unleash"
|
|
9813
|
+
},
|
|
9814
|
+
{
|
|
9815
|
+
functionName: "isOn",
|
|
9816
|
+
nameArgIndex: 0,
|
|
9817
|
+
provider: "GrowthBook"
|
|
9818
|
+
},
|
|
9819
|
+
{
|
|
9820
|
+
functionName: "isOff",
|
|
9821
|
+
nameArgIndex: 0,
|
|
9822
|
+
provider: "GrowthBook"
|
|
9823
|
+
},
|
|
9824
|
+
{
|
|
9825
|
+
functionName: "getFeatureValue",
|
|
9826
|
+
nameArgIndex: 0,
|
|
9827
|
+
provider: "GrowthBook"
|
|
9828
|
+
},
|
|
9829
|
+
{
|
|
9830
|
+
functionName: "getTreatment",
|
|
9831
|
+
nameArgIndex: 0,
|
|
9832
|
+
provider: "Split"
|
|
9833
|
+
},
|
|
9834
|
+
{
|
|
9835
|
+
functionName: "useFeatureFlagEnabled",
|
|
9836
|
+
nameArgIndex: 0,
|
|
9837
|
+
provider: "PostHog"
|
|
9838
|
+
},
|
|
9839
|
+
{
|
|
9840
|
+
functionName: "useFeatureFlagPayload",
|
|
9841
|
+
nameArgIndex: 0,
|
|
9842
|
+
provider: "PostHog"
|
|
9843
|
+
},
|
|
9844
|
+
{
|
|
9845
|
+
functionName: "useFeatureFlagVariantKey",
|
|
9846
|
+
nameArgIndex: 0,
|
|
9847
|
+
provider: "PostHog"
|
|
9848
|
+
},
|
|
9849
|
+
{
|
|
9850
|
+
functionName: "getFeatureFlagPayload",
|
|
9851
|
+
nameArgIndex: 0,
|
|
9852
|
+
provider: "PostHog"
|
|
9853
|
+
},
|
|
9854
|
+
{
|
|
9855
|
+
functionName: "getValueAsync",
|
|
9856
|
+
nameArgIndex: 0,
|
|
9857
|
+
provider: "ConfigCat"
|
|
9858
|
+
},
|
|
9859
|
+
{
|
|
9860
|
+
functionName: "getValueDetailsAsync",
|
|
9861
|
+
nameArgIndex: 0,
|
|
9862
|
+
provider: "ConfigCat"
|
|
9863
|
+
},
|
|
9864
|
+
{
|
|
9865
|
+
functionName: "hasFeature",
|
|
9866
|
+
nameArgIndex: 0,
|
|
9867
|
+
provider: "Flagsmith"
|
|
9868
|
+
},
|
|
9869
|
+
{
|
|
9870
|
+
functionName: "useDecision",
|
|
9871
|
+
nameArgIndex: 0,
|
|
9872
|
+
provider: "Optimizely"
|
|
9873
|
+
},
|
|
9874
|
+
{
|
|
9875
|
+
functionName: "getFeatureVariable",
|
|
9876
|
+
nameArgIndex: 0,
|
|
9877
|
+
provider: "Optimizely"
|
|
9878
|
+
},
|
|
9879
|
+
{
|
|
9880
|
+
functionName: "getFeatureVariableBoolean",
|
|
9881
|
+
nameArgIndex: 0,
|
|
9882
|
+
provider: "Optimizely"
|
|
9883
|
+
},
|
|
9884
|
+
{
|
|
9885
|
+
functionName: "getFeatureVariableString",
|
|
9886
|
+
nameArgIndex: 0,
|
|
9887
|
+
provider: "Optimizely"
|
|
9888
|
+
},
|
|
9889
|
+
{
|
|
9890
|
+
functionName: "getFeatureVariableInteger",
|
|
9891
|
+
nameArgIndex: 0,
|
|
9892
|
+
provider: "Optimizely"
|
|
9893
|
+
},
|
|
9894
|
+
{
|
|
9895
|
+
functionName: "getFeatureVariableDouble",
|
|
9896
|
+
nameArgIndex: 0,
|
|
9897
|
+
provider: "Optimizely"
|
|
9898
|
+
},
|
|
9899
|
+
{
|
|
9900
|
+
functionName: "getFeatureVariableJson",
|
|
9901
|
+
nameArgIndex: 0,
|
|
9902
|
+
provider: "Optimizely"
|
|
9903
|
+
},
|
|
9904
|
+
{
|
|
9905
|
+
functionName: "getFeatureVariableJSON",
|
|
9906
|
+
nameArgIndex: 0,
|
|
9907
|
+
provider: "Optimizely"
|
|
9908
|
+
},
|
|
9909
|
+
{
|
|
9910
|
+
functionName: "getStringAssignment",
|
|
9911
|
+
nameArgIndex: 0,
|
|
9912
|
+
provider: "Eppo"
|
|
9913
|
+
},
|
|
9914
|
+
{
|
|
9915
|
+
functionName: "getBooleanAssignment",
|
|
9916
|
+
nameArgIndex: 0,
|
|
9917
|
+
provider: "Eppo"
|
|
9918
|
+
},
|
|
9919
|
+
{
|
|
9920
|
+
functionName: "getNumericAssignment",
|
|
9921
|
+
nameArgIndex: 0,
|
|
9922
|
+
provider: "Eppo"
|
|
9923
|
+
},
|
|
9924
|
+
{
|
|
9925
|
+
functionName: "getIntegerAssignment",
|
|
9926
|
+
nameArgIndex: 0,
|
|
9927
|
+
provider: "Eppo"
|
|
9928
|
+
},
|
|
9929
|
+
{
|
|
9930
|
+
functionName: "getJSONAssignment",
|
|
9931
|
+
nameArgIndex: 0,
|
|
9932
|
+
provider: "Eppo"
|
|
9933
|
+
}
|
|
9934
|
+
];
|
|
9935
|
+
const VERCEL_FLAGS_FUNCTION_NAMES = new Set(["flag", "evaluate"]);
|
|
9936
|
+
const BUILTIN_ENV_PREFIXES = [
|
|
9937
|
+
"FEATURE_",
|
|
9938
|
+
"NEXT_PUBLIC_FEATURE_",
|
|
9939
|
+
"NEXT_PUBLIC_ENABLE_",
|
|
9940
|
+
"REACT_APP_FEATURE_",
|
|
9941
|
+
"REACT_APP_ENABLE_",
|
|
9942
|
+
"VITE_FEATURE_",
|
|
9943
|
+
"VITE_ENABLE_",
|
|
9944
|
+
"NUXT_PUBLIC_FEATURE_",
|
|
9945
|
+
"ENABLE_",
|
|
9946
|
+
"FF_",
|
|
9947
|
+
"FLAG_",
|
|
9948
|
+
"TOGGLE_"
|
|
9949
|
+
];
|
|
9950
|
+
const CONFIG_OBJECT_KEYWORDS = new Set([
|
|
9951
|
+
"feature",
|
|
9952
|
+
"features",
|
|
9953
|
+
"featureflags",
|
|
9954
|
+
"featureflag",
|
|
9955
|
+
"flag",
|
|
9956
|
+
"flags",
|
|
9957
|
+
"toggle",
|
|
9958
|
+
"toggles"
|
|
9959
|
+
]);
|
|
9960
|
+
const getStaticName = (node) => {
|
|
9961
|
+
if (!isAstNode(node)) return void 0;
|
|
9962
|
+
if (node.type === "Identifier" || node.type === "PrivateIdentifier") {
|
|
9963
|
+
const identifierName = node.name;
|
|
9964
|
+
return typeof identifierName === "string" ? identifierName : void 0;
|
|
9965
|
+
}
|
|
9966
|
+
if (node.type === "Literal") {
|
|
9967
|
+
const literalValue = node.value;
|
|
9968
|
+
return typeof literalValue === "string" ? literalValue : void 0;
|
|
9969
|
+
}
|
|
9970
|
+
};
|
|
9971
|
+
const extractStringArgument = (callArguments, argumentIndex) => {
|
|
9972
|
+
if (!Array.isArray(callArguments)) return void 0;
|
|
9973
|
+
const argumentNode = callArguments[argumentIndex];
|
|
9974
|
+
if (!isAstNode(argumentNode)) return void 0;
|
|
9975
|
+
if (argumentNode.type === "Literal") {
|
|
9976
|
+
const literalValue = argumentNode.value;
|
|
9977
|
+
return typeof literalValue === "string" ? literalValue : void 0;
|
|
9978
|
+
}
|
|
9979
|
+
if (argumentNode.type === "ObjectExpression") {
|
|
9980
|
+
const properties = argumentNode.properties;
|
|
9981
|
+
if (!Array.isArray(properties)) return void 0;
|
|
9982
|
+
for (const property of properties) {
|
|
9983
|
+
if (!isAstNode(property)) continue;
|
|
9984
|
+
if (property.type !== "Property") continue;
|
|
9985
|
+
const propertyKey = getStaticName(property.key);
|
|
9986
|
+
if (propertyKey !== "key" && propertyKey !== "name") continue;
|
|
9987
|
+
const propertyValueName = getStaticName(property.value);
|
|
9988
|
+
if (propertyValueName !== void 0) return propertyValueName;
|
|
9989
|
+
}
|
|
9990
|
+
}
|
|
9991
|
+
};
|
|
9992
|
+
const extractProcessEnvName = (memberExpression) => {
|
|
9993
|
+
if (!isAstNode(memberExpression)) return void 0;
|
|
9994
|
+
if (memberExpression.type !== "MemberExpression" && memberExpression.type !== "StaticMemberExpression") return;
|
|
9995
|
+
const propertyName = getStaticName(memberExpression.property);
|
|
9996
|
+
if (propertyName === void 0) return void 0;
|
|
9997
|
+
const objectNode = memberExpression.object;
|
|
9998
|
+
if (!isAstNode(objectNode)) return void 0;
|
|
9999
|
+
if (objectNode.type !== "MemberExpression" && objectNode.type !== "StaticMemberExpression") return;
|
|
10000
|
+
const innerObjectName = getStaticName(objectNode.object);
|
|
10001
|
+
const innerPropertyName = getStaticName(objectNode.property);
|
|
10002
|
+
if (innerObjectName === "process" && innerPropertyName === "env") return propertyName;
|
|
10003
|
+
};
|
|
10004
|
+
const isFlagEnvName = (envName, extraEnvPrefixes) => {
|
|
10005
|
+
for (const prefix of BUILTIN_ENV_PREFIXES) if (envName.startsWith(prefix)) return true;
|
|
10006
|
+
for (const prefix of extraEnvPrefixes) if (envName.startsWith(prefix)) return true;
|
|
10007
|
+
return false;
|
|
10008
|
+
};
|
|
10009
|
+
const collectVercelFlagsImports = (programNode) => {
|
|
10010
|
+
const localNames = /* @__PURE__ */ new Set();
|
|
10011
|
+
if (!isAstNode(programNode)) return localNames;
|
|
10012
|
+
const body = programNode.body;
|
|
10013
|
+
if (!Array.isArray(body)) return localNames;
|
|
10014
|
+
for (const statement of body) {
|
|
10015
|
+
if (!isAstNode(statement)) continue;
|
|
10016
|
+
if (statement.type !== "ImportDeclaration") continue;
|
|
10017
|
+
const sourceLiteral = statement.source;
|
|
10018
|
+
const sourceValue = isAstNode(sourceLiteral) ? sourceLiteral.value : void 0;
|
|
10019
|
+
if (typeof sourceValue !== "string") continue;
|
|
10020
|
+
if (!(sourceValue === "flags" || sourceValue.startsWith("flags/") || sourceValue === "@vercel/flags" || sourceValue.startsWith("@vercel/flags/"))) continue;
|
|
10021
|
+
const specifiers = statement.specifiers;
|
|
10022
|
+
if (!Array.isArray(specifiers)) continue;
|
|
10023
|
+
for (const specifier of specifiers) {
|
|
10024
|
+
if (!isAstNode(specifier)) continue;
|
|
10025
|
+
if (specifier.type === "ImportSpecifier") {
|
|
10026
|
+
const imported = specifier.imported;
|
|
10027
|
+
const local = specifier.local;
|
|
10028
|
+
const importedName = getStaticName(imported);
|
|
10029
|
+
const localName = getStaticName(local);
|
|
10030
|
+
if (importedName && VERCEL_FLAGS_FUNCTION_NAMES.has(importedName) && localName) localNames.add(localName);
|
|
10031
|
+
}
|
|
10032
|
+
}
|
|
10033
|
+
}
|
|
10034
|
+
return localNames;
|
|
10035
|
+
};
|
|
10036
|
+
const visitChildrenWithGuard = (node, visitor) => {
|
|
10037
|
+
if (!isAstNode(node)) return;
|
|
10038
|
+
for (const key of Object.keys(node)) {
|
|
10039
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") continue;
|
|
10040
|
+
const value = node[key];
|
|
10041
|
+
if (Array.isArray(value)) for (const item of value) visitor(item);
|
|
10042
|
+
else if (value !== null && typeof value === "object") visitor(value);
|
|
10043
|
+
}
|
|
10044
|
+
};
|
|
10045
|
+
const recordFlag = (context, flagName, kind, byteOffset, sdkProvider) => {
|
|
10046
|
+
const { line, column } = offsetToLineColumn(byteOffset, context.lineStarts);
|
|
10047
|
+
context.results.push({
|
|
10048
|
+
path: context.filePath,
|
|
10049
|
+
name: flagName,
|
|
10050
|
+
kind,
|
|
10051
|
+
line,
|
|
10052
|
+
column,
|
|
10053
|
+
sdkProvider,
|
|
10054
|
+
guardLineStart: context.guard?.startLine,
|
|
10055
|
+
guardLineEnd: context.guard?.endLine,
|
|
10056
|
+
guardsDeadCode: false
|
|
10057
|
+
});
|
|
10058
|
+
};
|
|
10059
|
+
const visitNode$1 = (node, context) => {
|
|
10060
|
+
if (!isAstNode(node)) return;
|
|
10061
|
+
if (node.type === "IfStatement") {
|
|
10062
|
+
const start = node.start;
|
|
10063
|
+
const end = node.end;
|
|
10064
|
+
const guard = typeof start === "number" && typeof end === "number" ? {
|
|
10065
|
+
startLine: offsetToLineColumn(start, context.lineStarts).line,
|
|
10066
|
+
endLine: offsetToLineColumn(end, context.lineStarts).line
|
|
10067
|
+
} : void 0;
|
|
10068
|
+
const previousGuard = context.guard;
|
|
10069
|
+
context.guard = guard;
|
|
10070
|
+
visitNode$1(node.test, context);
|
|
10071
|
+
context.guard = previousGuard;
|
|
10072
|
+
visitNode$1(node.consequent, context);
|
|
10073
|
+
visitNode$1(node.alternate, context);
|
|
10074
|
+
return;
|
|
10075
|
+
}
|
|
10076
|
+
if (node.type === "ConditionalExpression") {
|
|
10077
|
+
const start = node.start;
|
|
10078
|
+
const end = node.end;
|
|
10079
|
+
const guard = typeof start === "number" && typeof end === "number" ? {
|
|
10080
|
+
startLine: offsetToLineColumn(start, context.lineStarts).line,
|
|
10081
|
+
endLine: offsetToLineColumn(end, context.lineStarts).line
|
|
10082
|
+
} : void 0;
|
|
10083
|
+
const previousGuard = context.guard;
|
|
10084
|
+
context.guard = guard;
|
|
10085
|
+
visitNode$1(node.test, context);
|
|
10086
|
+
context.guard = previousGuard;
|
|
10087
|
+
visitNode$1(node.consequent, context);
|
|
10088
|
+
visitNode$1(node.alternate, context);
|
|
10089
|
+
return;
|
|
10090
|
+
}
|
|
10091
|
+
visitFlagPatternsInExpression(node, context);
|
|
10092
|
+
visitChildrenWithGuard(node, (child) => visitNode$1(child, context));
|
|
10093
|
+
};
|
|
10094
|
+
const visitFlagPatternsInExpression = (node, context) => {
|
|
10095
|
+
if (!isAstNode(node)) return;
|
|
10096
|
+
if (node.type === "MemberExpression" || node.type === "StaticMemberExpression") {
|
|
10097
|
+
const envName = extractProcessEnvName(node);
|
|
10098
|
+
if (envName !== void 0 && isFlagEnvName(envName, context.envPrefixes)) {
|
|
10099
|
+
const start = node.start;
|
|
10100
|
+
if (typeof start === "number") recordFlag(context, envName, "env-var", start, void 0);
|
|
10101
|
+
} else if (context.detectConfigObjects) {
|
|
10102
|
+
const objectName = getStaticName(node.object);
|
|
10103
|
+
const propertyName = getStaticName(node.property);
|
|
10104
|
+
if (objectName && propertyName) {
|
|
10105
|
+
if (CONFIG_OBJECT_KEYWORDS.has(objectName.toLowerCase()) || CONFIG_OBJECT_KEYWORDS.has(propertyName.toLowerCase())) {
|
|
10106
|
+
const start = node.start;
|
|
10107
|
+
if (typeof start === "number") recordFlag(context, `${objectName}.${propertyName}`, "config-object", start, void 0);
|
|
10108
|
+
}
|
|
10109
|
+
}
|
|
10110
|
+
}
|
|
10111
|
+
}
|
|
10112
|
+
if (node.type === "CallExpression") {
|
|
10113
|
+
const callee = node.callee;
|
|
10114
|
+
let functionName;
|
|
10115
|
+
let calleeIsMemberExpression = false;
|
|
10116
|
+
let receiverIsItselfMemberExpression = false;
|
|
10117
|
+
if (isAstNode(callee)) {
|
|
10118
|
+
if (callee.type === "Identifier") functionName = getStaticName(callee);
|
|
10119
|
+
else if (callee.type === "MemberExpression" || callee.type === "StaticMemberExpression") {
|
|
10120
|
+
calleeIsMemberExpression = true;
|
|
10121
|
+
functionName = getStaticName(callee.property);
|
|
10122
|
+
const receiver = callee.object;
|
|
10123
|
+
if (isAstNode(receiver) && (receiver.type === "MemberExpression" || receiver.type === "StaticMemberExpression")) receiverIsItselfMemberExpression = true;
|
|
10124
|
+
}
|
|
10125
|
+
}
|
|
10126
|
+
if (functionName !== void 0) {
|
|
10127
|
+
if (!calleeIsMemberExpression && context.vercelFlagsLocalNames.has(functionName)) {
|
|
10128
|
+
const callArguments = node.arguments;
|
|
10129
|
+
const flagName = extractStringArgument(callArguments, 0);
|
|
10130
|
+
if (flagName !== void 0) {
|
|
10131
|
+
const start = node.start;
|
|
10132
|
+
if (typeof start === "number") recordFlag(context, flagName, "sdk-call", start, "Vercel Flags");
|
|
10133
|
+
}
|
|
10134
|
+
return;
|
|
10135
|
+
}
|
|
10136
|
+
if (calleeIsMemberExpression && receiverIsItselfMemberExpression) return;
|
|
10137
|
+
for (const sdkPattern of context.sdkPatterns) {
|
|
10138
|
+
if (sdkPattern.functionName !== functionName) continue;
|
|
10139
|
+
const callArguments = node.arguments;
|
|
10140
|
+
const flagName = extractStringArgument(callArguments, sdkPattern.nameArgIndex);
|
|
10141
|
+
if (flagName === void 0) continue;
|
|
10142
|
+
const start = node.start;
|
|
10143
|
+
if (typeof start === "number") recordFlag(context, flagName, "sdk-call", start, sdkPattern.provider === "" ? void 0 : sdkPattern.provider);
|
|
10144
|
+
break;
|
|
10145
|
+
}
|
|
10146
|
+
}
|
|
10147
|
+
}
|
|
10148
|
+
};
|
|
10149
|
+
const buildSdkPatterns = (extraSdkFunctionNames) => {
|
|
10150
|
+
const merged = [...BUILTIN_SDK_PATTERNS];
|
|
10151
|
+
for (const extraName of extraSdkFunctionNames) merged.push({
|
|
10152
|
+
functionName: extraName,
|
|
10153
|
+
nameArgIndex: 0,
|
|
10154
|
+
provider: ""
|
|
10155
|
+
});
|
|
10156
|
+
return merged;
|
|
10157
|
+
};
|
|
10158
|
+
const detectFeatureFlags = (graph, config) => {
|
|
10159
|
+
if (!config?.enabled) return [];
|
|
10160
|
+
const sdkPatterns = buildSdkPatterns(config.extraSdkFunctionNames);
|
|
10161
|
+
const collectedFlags = [];
|
|
10162
|
+
for (const module of graph.modules) {
|
|
10163
|
+
if (module.isDeclarationFile) continue;
|
|
10164
|
+
if (module.isConfigFile) continue;
|
|
10165
|
+
let sourceText;
|
|
10166
|
+
try {
|
|
10167
|
+
sourceText = (0, node_fs.readFileSync)(module.fileId.path, "utf-8");
|
|
10168
|
+
} catch {
|
|
10169
|
+
continue;
|
|
10170
|
+
}
|
|
10171
|
+
let parseResult;
|
|
10172
|
+
try {
|
|
10173
|
+
parseResult = (0, oxc_parser.parseSync)(module.fileId.path, sourceText);
|
|
10174
|
+
} catch {
|
|
10175
|
+
continue;
|
|
10176
|
+
}
|
|
10177
|
+
const lineStarts = computeLineStarts(sourceText);
|
|
10178
|
+
const vercelFlagsLocalNames = collectVercelFlagsImports(parseResult.program);
|
|
10179
|
+
const visitContext = {
|
|
10180
|
+
filePath: module.fileId.path,
|
|
10181
|
+
lineStarts,
|
|
10182
|
+
results: [],
|
|
10183
|
+
envPrefixes: config.extraEnvPrefixes,
|
|
10184
|
+
sdkPatterns,
|
|
10185
|
+
detectConfigObjects: config.detectConfigObjects,
|
|
10186
|
+
vercelFlagsLocalNames,
|
|
10187
|
+
guard: void 0
|
|
10188
|
+
};
|
|
10189
|
+
visitNode$1(parseResult.program, visitContext);
|
|
10190
|
+
collectedFlags.push(...visitContext.results);
|
|
10191
|
+
}
|
|
10192
|
+
collectedFlags.sort((leftFlag, rightFlag) => {
|
|
10193
|
+
if (leftFlag.path !== rightFlag.path) return leftFlag.path.localeCompare(rightFlag.path);
|
|
10194
|
+
if (leftFlag.line !== rightFlag.line) return leftFlag.line - rightFlag.line;
|
|
10195
|
+
return leftFlag.column - rightFlag.column;
|
|
10196
|
+
});
|
|
10197
|
+
return collectedFlags;
|
|
10198
|
+
};
|
|
10199
|
+
/**
|
|
10200
|
+
* Mark each flag whose guard span overlaps an unused export as
|
|
10201
|
+
* `guardsDeadCode: true`.
|
|
10202
|
+
*/
|
|
10203
|
+
const correlateFlagsWithDeadCode = (flags, scanResult) => {
|
|
10204
|
+
if (flags.length === 0 || scanResult.unusedExports.length === 0) return;
|
|
10205
|
+
const unusedByFile = /* @__PURE__ */ new Map();
|
|
10206
|
+
for (const unusedExport of scanResult.unusedExports) {
|
|
10207
|
+
const existing = unusedByFile.get(unusedExport.path);
|
|
10208
|
+
if (existing) existing.push(unusedExport.line);
|
|
10209
|
+
else unusedByFile.set(unusedExport.path, [unusedExport.line]);
|
|
10210
|
+
}
|
|
10211
|
+
for (const flag of flags) {
|
|
10212
|
+
if (flag.guardLineStart === void 0 || flag.guardLineEnd === void 0) continue;
|
|
10213
|
+
const linesInFile = unusedByFile.get(flag.path);
|
|
10214
|
+
if (!linesInFile) continue;
|
|
10215
|
+
const guardStart = flag.guardLineStart;
|
|
10216
|
+
const guardEnd = flag.guardLineEnd;
|
|
10217
|
+
for (const unusedLine of linesInFile) if (unusedLine >= guardStart && unusedLine <= guardEnd) {
|
|
10218
|
+
flag.guardsDeadCode = true;
|
|
10219
|
+
break;
|
|
10220
|
+
}
|
|
10221
|
+
}
|
|
10222
|
+
};
|
|
10223
|
+
|
|
10224
|
+
//#endregion
|
|
10225
|
+
//#region src/report/complexity.ts
|
|
10226
|
+
const incrementCyclomatic = (state) => {
|
|
10227
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10228
|
+
if (topFrame) topFrame.cyclomaticComplexity++;
|
|
10229
|
+
};
|
|
10230
|
+
const incrementCognitiveWithNesting = (state) => {
|
|
10231
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10232
|
+
if (topFrame) topFrame.cognitiveComplexity += 1 + topFrame.nestingLevel;
|
|
10233
|
+
};
|
|
10234
|
+
const incrementCognitiveFlat = (state) => {
|
|
10235
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10236
|
+
if (topFrame) topFrame.cognitiveComplexity++;
|
|
10237
|
+
};
|
|
10238
|
+
const handleLogicalOperator = (operator, state) => {
|
|
10239
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10240
|
+
if (!topFrame) return;
|
|
10241
|
+
if (topFrame.lastLogicalOperator === void 0) {
|
|
10242
|
+
topFrame.cognitiveComplexity++;
|
|
10243
|
+
topFrame.lastLogicalOperator = operator;
|
|
10244
|
+
return;
|
|
10245
|
+
}
|
|
10246
|
+
if (topFrame.lastLogicalOperator === operator) return;
|
|
10247
|
+
topFrame.cognitiveComplexity++;
|
|
10248
|
+
topFrame.lastLogicalOperator = operator;
|
|
10249
|
+
};
|
|
10250
|
+
const resetLogicalOperator = (state) => {
|
|
10251
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10252
|
+
if (topFrame) topFrame.lastLogicalOperator = void 0;
|
|
10253
|
+
};
|
|
10254
|
+
const incrementNesting = (state) => {
|
|
10255
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10256
|
+
if (topFrame) topFrame.nestingLevel++;
|
|
10257
|
+
};
|
|
10258
|
+
const decrementNesting = (state) => {
|
|
10259
|
+
const topFrame = state.frameStack[state.frameStack.length - 1];
|
|
10260
|
+
if (topFrame && topFrame.nestingLevel > 0) topFrame.nestingLevel--;
|
|
10261
|
+
};
|
|
10262
|
+
const countParameters = (parametersNode) => {
|
|
10263
|
+
if (!isAstNode(parametersNode)) return 0;
|
|
10264
|
+
const params = parametersNode;
|
|
10265
|
+
if (Array.isArray(params.params)) return params.params.length;
|
|
10266
|
+
if (Array.isArray(params.items)) return params.items.length;
|
|
10267
|
+
return 0;
|
|
10268
|
+
};
|
|
10269
|
+
const visitChildrenGeneric = (node, visitor) => {
|
|
10270
|
+
if (!isAstNode(node)) return;
|
|
10271
|
+
for (const key of Object.keys(node)) {
|
|
10272
|
+
if (key === "type" || key === "start" || key === "end" || key === "loc" || key === "range") continue;
|
|
10273
|
+
const value = node[key];
|
|
10274
|
+
if (Array.isArray(value)) for (const item of value) visitor(item);
|
|
10275
|
+
else if (value !== null && typeof value === "object") visitor(value);
|
|
10276
|
+
}
|
|
10277
|
+
};
|
|
10278
|
+
const pushFunctionFrame = (functionName, startOffset, endOffset, parameterCount, state) => {
|
|
10279
|
+
state.frameStack.push({
|
|
10280
|
+
functionName,
|
|
10281
|
+
startOffset,
|
|
10282
|
+
endOffset,
|
|
10283
|
+
cyclomaticComplexity: 1,
|
|
10284
|
+
cognitiveComplexity: 0,
|
|
10285
|
+
nestingLevel: 0,
|
|
10286
|
+
lastLogicalOperator: void 0,
|
|
10287
|
+
parameterCount
|
|
10288
|
+
});
|
|
10289
|
+
};
|
|
10290
|
+
const popFunctionFrame = (state) => {
|
|
10291
|
+
const completedFrame = state.frameStack.pop();
|
|
10292
|
+
if (!completedFrame) return;
|
|
10293
|
+
const { line, column } = offsetToLineColumn(completedFrame.startOffset, state.lineStarts);
|
|
10294
|
+
const endLine = offsetToLineColumn(completedFrame.endOffset, state.lineStarts).line;
|
|
10295
|
+
state.results.push({
|
|
10296
|
+
path: state.filePath,
|
|
10297
|
+
functionName: completedFrame.functionName,
|
|
10298
|
+
line,
|
|
10299
|
+
column,
|
|
10300
|
+
cyclomatic: completedFrame.cyclomaticComplexity,
|
|
10301
|
+
cognitive: completedFrame.cognitiveComplexity,
|
|
10302
|
+
lineCount: Math.max(1, endLine - line + 1),
|
|
10303
|
+
paramCount: completedFrame.parameterCount,
|
|
10304
|
+
confidence: "medium",
|
|
10305
|
+
reason: ""
|
|
10306
|
+
});
|
|
10307
|
+
};
|
|
10308
|
+
const visitFunctionLike = (node, kind, state) => {
|
|
10309
|
+
if (!isAstNode(node)) return;
|
|
10310
|
+
const functionName = state.pendingFunctionName ?? (() => {
|
|
10311
|
+
const idNode = node.id;
|
|
10312
|
+
const idName = isAstNode(idNode) ? idNode.name : void 0;
|
|
10313
|
+
return typeof idName === "string" ? idName : kind === "arrow" ? "<arrow>" : "<anonymous>";
|
|
10314
|
+
})();
|
|
10315
|
+
state.pendingFunctionName = void 0;
|
|
10316
|
+
const isNested = state.frameStack.length > 0;
|
|
10317
|
+
if (isNested) incrementNesting(state);
|
|
10318
|
+
const startOffset = node.start;
|
|
10319
|
+
const endOffset = node.end;
|
|
10320
|
+
const parameterCount = countParameters(node.params);
|
|
10321
|
+
pushFunctionFrame(functionName, typeof startOffset === "number" ? startOffset : 0, typeof endOffset === "number" ? endOffset : 0, parameterCount, state);
|
|
10322
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10323
|
+
popFunctionFrame(state);
|
|
10324
|
+
if (isNested) decrementNesting(state);
|
|
10325
|
+
};
|
|
10326
|
+
const visitNode = (node, state) => {
|
|
10327
|
+
if (!isAstNode(node)) return;
|
|
10328
|
+
switch (node.type) {
|
|
10329
|
+
case "FunctionDeclaration":
|
|
10330
|
+
case "FunctionExpression":
|
|
10331
|
+
case "MethodDefinition":
|
|
10332
|
+
if (node.type === "MethodDefinition") {
|
|
10333
|
+
const keyNode = node.key;
|
|
10334
|
+
const keyName = isAstNode(keyNode) ? keyNode.name ?? keyNode.value : void 0;
|
|
10335
|
+
if (typeof keyName === "string") state.pendingFunctionName = keyName;
|
|
10336
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10337
|
+
state.pendingFunctionName = void 0;
|
|
10338
|
+
return;
|
|
10339
|
+
}
|
|
10340
|
+
visitFunctionLike(node, "function", state);
|
|
10341
|
+
return;
|
|
10342
|
+
case "ArrowFunctionExpression":
|
|
10343
|
+
visitFunctionLike(node, "arrow", state);
|
|
10344
|
+
return;
|
|
10345
|
+
case "VariableDeclarator": {
|
|
10346
|
+
const declaratorId = node.id;
|
|
10347
|
+
const declaratorIdName = isAstNode(declaratorId) ? declaratorId.name : void 0;
|
|
10348
|
+
if (typeof declaratorIdName === "string") state.pendingFunctionName = declaratorIdName;
|
|
10349
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10350
|
+
state.pendingFunctionName = void 0;
|
|
10351
|
+
return;
|
|
10352
|
+
}
|
|
10353
|
+
case "PropertyDefinition": {
|
|
10354
|
+
const keyNode = node.key;
|
|
10355
|
+
const keyName = isAstNode(keyNode) ? keyNode.name : void 0;
|
|
10356
|
+
if (typeof keyName === "string") state.pendingFunctionName = keyName;
|
|
10357
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10358
|
+
state.pendingFunctionName = void 0;
|
|
10359
|
+
return;
|
|
10360
|
+
}
|
|
10361
|
+
case "IfStatement":
|
|
10362
|
+
incrementCyclomatic(state);
|
|
10363
|
+
incrementCognitiveWithNesting(state);
|
|
10364
|
+
incrementNesting(state);
|
|
10365
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10366
|
+
decrementNesting(state);
|
|
10367
|
+
resetLogicalOperator(state);
|
|
10368
|
+
return;
|
|
10369
|
+
case "ForStatement":
|
|
10370
|
+
case "ForInStatement":
|
|
10371
|
+
case "ForOfStatement":
|
|
10372
|
+
case "WhileStatement":
|
|
10373
|
+
case "DoWhileStatement":
|
|
10374
|
+
incrementCyclomatic(state);
|
|
10375
|
+
incrementCognitiveWithNesting(state);
|
|
10376
|
+
incrementNesting(state);
|
|
10377
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10378
|
+
decrementNesting(state);
|
|
10379
|
+
return;
|
|
10380
|
+
case "SwitchCase": {
|
|
10381
|
+
const testNode = node.test;
|
|
10382
|
+
if (testNode !== null && testNode !== void 0) {
|
|
10383
|
+
incrementCyclomatic(state);
|
|
10384
|
+
incrementCognitiveFlat(state);
|
|
10385
|
+
}
|
|
10386
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10387
|
+
return;
|
|
10388
|
+
}
|
|
10389
|
+
case "CatchClause":
|
|
10390
|
+
incrementCyclomatic(state);
|
|
10391
|
+
incrementCognitiveWithNesting(state);
|
|
10392
|
+
incrementNesting(state);
|
|
10393
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10394
|
+
decrementNesting(state);
|
|
10395
|
+
return;
|
|
10396
|
+
case "ConditionalExpression":
|
|
10397
|
+
incrementCyclomatic(state);
|
|
10398
|
+
incrementCognitiveWithNesting(state);
|
|
10399
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10400
|
+
return;
|
|
10401
|
+
case "LogicalExpression": {
|
|
10402
|
+
const operator = node.operator;
|
|
10403
|
+
if (operator === "&&" || operator === "||" || operator === "??") {
|
|
10404
|
+
incrementCyclomatic(state);
|
|
10405
|
+
handleLogicalOperator(operator, state);
|
|
10406
|
+
}
|
|
10407
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10408
|
+
return;
|
|
10409
|
+
}
|
|
10410
|
+
case "AssignmentExpression": {
|
|
10411
|
+
const operator = node.operator;
|
|
10412
|
+
if (operator === "&&=" || operator === "||=" || operator === "??=") incrementCyclomatic(state);
|
|
10413
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10414
|
+
return;
|
|
10415
|
+
}
|
|
10416
|
+
case "ChainExpression":
|
|
10417
|
+
incrementCyclomatic(state);
|
|
10418
|
+
visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10419
|
+
return;
|
|
10420
|
+
default: visitChildrenGeneric(node, (child) => visitNode(child, state));
|
|
10421
|
+
}
|
|
10422
|
+
};
|
|
10423
|
+
const annotateConfidence = (finding, config) => {
|
|
10424
|
+
const breaches = [];
|
|
10425
|
+
if (finding.cyclomatic >= config.cyclomaticThreshold) breaches.push(`cyclomatic ${finding.cyclomatic} ≥ ${config.cyclomaticThreshold}`);
|
|
10426
|
+
if (finding.cognitive >= config.cognitiveThreshold) breaches.push(`cognitive ${finding.cognitive} ≥ ${config.cognitiveThreshold}`);
|
|
10427
|
+
if (finding.paramCount >= config.paramCountThreshold) breaches.push(`paramCount ${finding.paramCount} ≥ ${config.paramCountThreshold}`);
|
|
10428
|
+
if (finding.lineCount >= config.functionLineThreshold) breaches.push(`lineCount ${finding.lineCount} ≥ ${config.functionLineThreshold}`);
|
|
10429
|
+
return {
|
|
10430
|
+
confidence: breaches.length >= 2 ? "high" : "medium",
|
|
10431
|
+
reason: `${finding.functionName} breaches ${breaches.length} threshold${breaches.length === 1 ? "" : "s"}: ${breaches.join(", ")}`
|
|
10432
|
+
};
|
|
10433
|
+
};
|
|
10434
|
+
/**
|
|
10435
|
+
* Per-function cyclomatic + cognitive complexity.
|
|
10436
|
+
*
|
|
10437
|
+
* Cyclomatic (McCabe): 1 + decision points. Counts if/for/while/do/case/catch,
|
|
10438
|
+
* the ?: ternary, &&, ||, ??, &&=/||=/??=, and ?. (optional chaining).
|
|
10439
|
+
*
|
|
10440
|
+
* Cognitive (SonarSource): structural increments with nesting penalty.
|
|
10441
|
+
* Operator-sequence rule: a run of the same logical operator is +1 total;
|
|
10442
|
+
* each operator change adds another +1.
|
|
10443
|
+
*
|
|
10444
|
+
* Returns only functions whose metrics breach at least one threshold from
|
|
10445
|
+
* `config`. Threshold breach count tunes the `confidence` field.
|
|
10446
|
+
*/
|
|
10447
|
+
const detectComplexHotspots = (graph, config) => {
|
|
10448
|
+
if (!config?.enabled) return [];
|
|
10449
|
+
const hotspotFindings = [];
|
|
10450
|
+
for (const module of graph.modules) {
|
|
10451
|
+
if (module.isDeclarationFile) continue;
|
|
10452
|
+
if (module.isConfigFile) continue;
|
|
10453
|
+
let sourceText;
|
|
10454
|
+
try {
|
|
10455
|
+
sourceText = (0, node_fs.readFileSync)(module.fileId.path, "utf-8");
|
|
10456
|
+
} catch {
|
|
10457
|
+
continue;
|
|
10458
|
+
}
|
|
10459
|
+
let parseResult;
|
|
10460
|
+
try {
|
|
10461
|
+
parseResult = (0, oxc_parser.parseSync)(module.fileId.path, sourceText);
|
|
10462
|
+
} catch {
|
|
10463
|
+
continue;
|
|
10464
|
+
}
|
|
10465
|
+
const visitState = {
|
|
10466
|
+
filePath: module.fileId.path,
|
|
10467
|
+
lineStarts: computeLineStarts(sourceText),
|
|
10468
|
+
results: [],
|
|
10469
|
+
frameStack: [],
|
|
10470
|
+
pendingFunctionName: void 0
|
|
10471
|
+
};
|
|
10472
|
+
visitNode(parseResult.program, visitState);
|
|
10473
|
+
for (const result of visitState.results) {
|
|
10474
|
+
if (!(result.cyclomatic >= config.cyclomaticThreshold || result.cognitive >= config.cognitiveThreshold || result.paramCount >= config.paramCountThreshold || result.lineCount >= config.functionLineThreshold)) continue;
|
|
10475
|
+
const annotated = annotateConfidence(result, config);
|
|
10476
|
+
hotspotFindings.push({
|
|
10477
|
+
...result,
|
|
10478
|
+
confidence: annotated.confidence,
|
|
10479
|
+
reason: annotated.reason
|
|
10480
|
+
});
|
|
10481
|
+
}
|
|
10482
|
+
}
|
|
10483
|
+
hotspotFindings.sort((leftFinding, rightFinding) => {
|
|
10484
|
+
const leftScore = leftFinding.cyclomatic + leftFinding.cognitive;
|
|
10485
|
+
const rightScore = rightFinding.cyclomatic + rightFinding.cognitive;
|
|
10486
|
+
if (leftScore !== rightScore) return rightScore - leftScore;
|
|
10487
|
+
if (leftFinding.path !== rightFinding.path) return leftFinding.path.localeCompare(rightFinding.path);
|
|
10488
|
+
return leftFinding.line - rightFinding.line;
|
|
10489
|
+
});
|
|
10490
|
+
return hotspotFindings;
|
|
10491
|
+
};
|
|
10492
|
+
|
|
10493
|
+
//#endregion
|
|
10494
|
+
//#region src/report/private-type-leaks.ts
|
|
10495
|
+
const extractIdentifierName = (node) => {
|
|
10496
|
+
if (!isAstNode(node)) return void 0;
|
|
10497
|
+
if (node.type === "Identifier") {
|
|
10498
|
+
const identifierName = node.name;
|
|
10499
|
+
return typeof identifierName === "string" ? identifierName : void 0;
|
|
10500
|
+
}
|
|
10501
|
+
};
|
|
10502
|
+
const collectTypeReferenceNamesFromTypeNode = (typeNode, into) => {
|
|
10503
|
+
if (!isAstNode(typeNode)) return;
|
|
10504
|
+
if (typeNode.type === "TSTypeReference") {
|
|
10505
|
+
const referencedTypeName = typeNode.typeName;
|
|
10506
|
+
if (isAstNode(referencedTypeName) && referencedTypeName.type === "Identifier") {
|
|
10507
|
+
const name = referencedTypeName.name;
|
|
10508
|
+
if (typeof name === "string") into.add(name);
|
|
10509
|
+
}
|
|
10510
|
+
}
|
|
10511
|
+
for (const key of Object.keys(typeNode)) {
|
|
10512
|
+
if (key === "type" || key === "start" || key === "end") continue;
|
|
10513
|
+
const value = typeNode[key];
|
|
10514
|
+
if (Array.isArray(value)) for (const item of value) collectTypeReferenceNamesFromTypeNode(item, into);
|
|
10515
|
+
else if (value !== null && typeof value === "object") collectTypeReferenceNamesFromTypeNode(value, into);
|
|
10516
|
+
}
|
|
10517
|
+
};
|
|
10518
|
+
const isExportedDeclaration = (statement) => {
|
|
10519
|
+
if (!isAstNode(statement)) return false;
|
|
10520
|
+
return statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration";
|
|
10521
|
+
};
|
|
10522
|
+
const declarationOf = (statement) => {
|
|
10523
|
+
if (!isAstNode(statement)) return void 0;
|
|
10524
|
+
return statement.declaration;
|
|
10525
|
+
};
|
|
10526
|
+
const exportedNameOfDeclaration = (declarationNode) => {
|
|
10527
|
+
if (!isAstNode(declarationNode)) return void 0;
|
|
10528
|
+
if (declarationNode.type === "FunctionDeclaration" || declarationNode.type === "ClassDeclaration") return extractIdentifierName(declarationNode.id);
|
|
10529
|
+
if (declarationNode.type === "VariableDeclaration") {
|
|
10530
|
+
const declarators = declarationNode.declarations;
|
|
10531
|
+
if (Array.isArray(declarators) && declarators.length > 0) {
|
|
10532
|
+
const firstDeclarator = declarators[0];
|
|
10533
|
+
if (isAstNode(firstDeclarator)) return extractIdentifierName(firstDeclarator.id);
|
|
10534
|
+
}
|
|
10535
|
+
}
|
|
10536
|
+
if (declarationNode.type === "TSInterfaceDeclaration" || declarationNode.type === "TSTypeAliasDeclaration") return extractIdentifierName(declarationNode.id);
|
|
10537
|
+
};
|
|
10538
|
+
const collectFromFunctionLikeSignature = (functionLikeNode, exportName, collected) => {
|
|
10539
|
+
if (!isAstNode(functionLikeNode)) return;
|
|
10540
|
+
const params = functionLikeNode.params;
|
|
10541
|
+
if (Array.isArray(params)) for (const param of params) collectFromParameter(param, exportName, collected);
|
|
10542
|
+
const returnTypeAnnotation = functionLikeNode.returnType;
|
|
10543
|
+
if (isAstNode(returnTypeAnnotation)) {
|
|
10544
|
+
const annotation = returnTypeAnnotation.typeAnnotation;
|
|
10545
|
+
pushTypeReferences(annotation, exportName, collected, returnTypeAnnotation);
|
|
10546
|
+
}
|
|
10547
|
+
};
|
|
10548
|
+
const collectFromParameter = (parameterNode, exportName, collected) => {
|
|
10549
|
+
if (!isAstNode(parameterNode)) return;
|
|
10550
|
+
const annotation = parameterNode.typeAnnotation;
|
|
10551
|
+
if (isAstNode(annotation)) {
|
|
10552
|
+
const innerTypeNode = annotation.typeAnnotation;
|
|
10553
|
+
pushTypeReferences(innerTypeNode, exportName, collected, annotation);
|
|
10554
|
+
}
|
|
10555
|
+
};
|
|
10556
|
+
const pushTypeReferences = (typeNode, exportName, collected, spanFallbackNode) => {
|
|
10557
|
+
if (!isAstNode(typeNode)) return;
|
|
10558
|
+
const referencedTypeNames = /* @__PURE__ */ new Set();
|
|
10559
|
+
collectTypeReferenceNamesFromTypeNode(typeNode, referencedTypeNames);
|
|
10560
|
+
for (const referencedName of referencedTypeNames) {
|
|
10561
|
+
const offset = typeNode.start;
|
|
10562
|
+
const fallbackOffset = isAstNode(spanFallbackNode) && typeof spanFallbackNode.start === "number" ? spanFallbackNode.start : 0;
|
|
10563
|
+
collected.push({
|
|
10564
|
+
exportName,
|
|
10565
|
+
typeName: referencedName,
|
|
10566
|
+
byteOffset: typeof offset === "number" ? offset : fallbackOffset
|
|
10567
|
+
});
|
|
10568
|
+
}
|
|
10569
|
+
};
|
|
10570
|
+
const collectPublicSignatureReferences = (programNode) => {
|
|
10571
|
+
const collected = [];
|
|
10572
|
+
if (!isAstNode(programNode)) return collected;
|
|
10573
|
+
const programBody = programNode.body;
|
|
10574
|
+
if (!Array.isArray(programBody)) return collected;
|
|
10575
|
+
for (const statement of programBody) {
|
|
10576
|
+
if (!isExportedDeclaration(statement)) continue;
|
|
10577
|
+
const declarationNode = declarationOf(statement);
|
|
10578
|
+
if (declarationNode === void 0 || declarationNode === null) continue;
|
|
10579
|
+
const exportedName = exportedNameOfDeclaration(declarationNode);
|
|
10580
|
+
if (!exportedName) continue;
|
|
10581
|
+
if (isAstNode(declarationNode)) {
|
|
10582
|
+
if (declarationNode.type === "FunctionDeclaration" || declarationNode.type === "ArrowFunctionExpression" || declarationNode.type === "FunctionExpression") {
|
|
10583
|
+
collectFromFunctionLikeSignature(declarationNode, exportedName, collected);
|
|
10584
|
+
continue;
|
|
10585
|
+
}
|
|
10586
|
+
if (declarationNode.type === "VariableDeclaration") {
|
|
10587
|
+
const declarators = declarationNode.declarations;
|
|
10588
|
+
if (Array.isArray(declarators)) for (const declarator of declarators) {
|
|
10589
|
+
if (!isAstNode(declarator)) continue;
|
|
10590
|
+
const id = declarator.id;
|
|
10591
|
+
if (isAstNode(id)) {
|
|
10592
|
+
const annotation = id.typeAnnotation;
|
|
10593
|
+
if (isAstNode(annotation)) {
|
|
10594
|
+
const inner = annotation.typeAnnotation;
|
|
10595
|
+
pushTypeReferences(inner, exportedName, collected, annotation);
|
|
10596
|
+
}
|
|
10597
|
+
}
|
|
10598
|
+
const init = declarator.init;
|
|
10599
|
+
if (isAstNode(init) && (init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression")) collectFromFunctionLikeSignature(init, exportedName, collected);
|
|
10600
|
+
}
|
|
10601
|
+
continue;
|
|
10602
|
+
}
|
|
10603
|
+
if (declarationNode.type === "ClassDeclaration") {
|
|
10604
|
+
const classBody = declarationNode.body;
|
|
10605
|
+
if (isAstNode(classBody)) {
|
|
10606
|
+
const members = classBody.body;
|
|
10607
|
+
if (Array.isArray(members)) for (const member of members) {
|
|
10608
|
+
if (!isAstNode(member)) continue;
|
|
10609
|
+
if (member.type === "MethodDefinition") {
|
|
10610
|
+
const value = member.value;
|
|
10611
|
+
collectFromFunctionLikeSignature(value, exportedName, collected);
|
|
10612
|
+
} else if (member.type === "PropertyDefinition") {
|
|
10613
|
+
const annotation = member.typeAnnotation;
|
|
10614
|
+
if (isAstNode(annotation)) {
|
|
10615
|
+
const inner = annotation.typeAnnotation;
|
|
10616
|
+
pushTypeReferences(inner, exportedName, collected, annotation);
|
|
10617
|
+
}
|
|
10618
|
+
}
|
|
10619
|
+
}
|
|
10620
|
+
}
|
|
10621
|
+
}
|
|
10622
|
+
}
|
|
10623
|
+
}
|
|
10624
|
+
return collected;
|
|
10625
|
+
};
|
|
10626
|
+
const collectLocalTypeNames = (programNode) => {
|
|
10627
|
+
const localTypeNames = /* @__PURE__ */ new Set();
|
|
10628
|
+
const exportedNames = /* @__PURE__ */ new Set();
|
|
10629
|
+
if (!isAstNode(programNode)) return {
|
|
10630
|
+
localTypeNames,
|
|
10631
|
+
exportedNames
|
|
10632
|
+
};
|
|
10633
|
+
const programBody = programNode.body;
|
|
10634
|
+
if (!Array.isArray(programBody)) return {
|
|
10635
|
+
localTypeNames,
|
|
10636
|
+
exportedNames
|
|
10637
|
+
};
|
|
10638
|
+
for (const statement of programBody) {
|
|
10639
|
+
if (!isAstNode(statement)) continue;
|
|
10640
|
+
if (statement.type === "TSInterfaceDeclaration" || statement.type === "TSTypeAliasDeclaration") {
|
|
10641
|
+
const name = extractIdentifierName(statement.id);
|
|
10642
|
+
if (name) localTypeNames.add(name);
|
|
10643
|
+
continue;
|
|
10644
|
+
}
|
|
10645
|
+
if (statement.type === "ExportNamedDeclaration") {
|
|
10646
|
+
const declarationNode = statement.declaration;
|
|
10647
|
+
if (isAstNode(declarationNode)) {
|
|
10648
|
+
if (declarationNode.type === "TSInterfaceDeclaration" || declarationNode.type === "TSTypeAliasDeclaration") {
|
|
10649
|
+
const name = extractIdentifierName(declarationNode.id);
|
|
10650
|
+
if (name) exportedNames.add(name);
|
|
10651
|
+
continue;
|
|
10652
|
+
}
|
|
10653
|
+
const declaredName = exportedNameOfDeclaration(declarationNode);
|
|
10654
|
+
if (declaredName) exportedNames.add(declaredName);
|
|
10655
|
+
}
|
|
10656
|
+
const specifiers = statement.specifiers;
|
|
10657
|
+
if (Array.isArray(specifiers)) for (const specifier of specifiers) {
|
|
10658
|
+
if (!isAstNode(specifier)) continue;
|
|
10659
|
+
if (specifier.type === "ExportSpecifier") {
|
|
10660
|
+
const exported = specifier.exported;
|
|
10661
|
+
const exportedNameValue = extractIdentifierName(exported);
|
|
10662
|
+
if (exportedNameValue) exportedNames.add(exportedNameValue);
|
|
10663
|
+
}
|
|
10664
|
+
}
|
|
10665
|
+
}
|
|
10666
|
+
}
|
|
10667
|
+
return {
|
|
10668
|
+
localTypeNames,
|
|
10669
|
+
exportedNames
|
|
10670
|
+
};
|
|
10671
|
+
};
|
|
10672
|
+
/**
|
|
10673
|
+
* Storybook CSF3 convention: a story file declares
|
|
10674
|
+
*
|
|
10675
|
+
* const meta = { ... } satisfies Meta<...>;
|
|
10676
|
+
* export default meta;
|
|
10677
|
+
* type Story = StoryObj<typeof meta>;
|
|
10678
|
+
* export const Primary: Story = { ... };
|
|
10679
|
+
*
|
|
10680
|
+
* `Story` is intentionally a local alias — consumers don't import it; the
|
|
10681
|
+
* Storybook runtime reads the default export. Flagging this as a leak
|
|
10682
|
+
* produces near-100% false positives on Storybook codebases, so skip story
|
|
10683
|
+
* files entirely. Storybook supports both common glob conventions — match
|
|
10684
|
+
* the `*.stories.{ext}` style as well as a bare `stories.{ext}` basename.
|
|
10685
|
+
*/
|
|
10686
|
+
const STORYBOOK_STORY_BASENAME_PATTERN = /^(?:.*\.)?stories\.(?:[cm]?ts|[cm]?js|tsx|jsx)$/i;
|
|
10687
|
+
const isStorybookStoryFile = (filePath) => {
|
|
10688
|
+
const lastSlash = filePath.lastIndexOf("/");
|
|
10689
|
+
const basename = lastSlash === -1 ? filePath : filePath.slice(lastSlash + 1);
|
|
10690
|
+
return STORYBOOK_STORY_BASENAME_PATTERN.test(basename);
|
|
10691
|
+
};
|
|
10692
|
+
/**
|
|
10693
|
+
* Detect TypeScript "private type leak": an exported declaration's signature
|
|
10694
|
+
* references a type that was declared locally in the same module but is not
|
|
10695
|
+
* itself exported. Consumers of the export need that type to satisfy the
|
|
10696
|
+
* signature, but cannot import it.
|
|
10697
|
+
*
|
|
10698
|
+
* Skips declaration files (`.d.ts`) — they are pure type modules where this
|
|
10699
|
+
* pattern is the norm. Keeps it simple: doesn't try to chase aliased re-export
|
|
10700
|
+
* paths (deslop-js's broader resolver work covers that elsewhere); a leak
|
|
10701
|
+
* that's actually re-exported gets filtered out at the `exportedNames` set.
|
|
10702
|
+
*/
|
|
10703
|
+
const detectPrivateTypeLeaks = (graph) => {
|
|
10704
|
+
const findings = [];
|
|
10705
|
+
for (const module of graph.modules) {
|
|
10706
|
+
if (module.isDeclarationFile) continue;
|
|
10707
|
+
if (module.isConfigFile) continue;
|
|
10708
|
+
if (!module.isReachable) continue;
|
|
10709
|
+
if (isStorybookStoryFile(module.fileId.path)) continue;
|
|
10710
|
+
let sourceText;
|
|
10711
|
+
try {
|
|
10712
|
+
sourceText = (0, node_fs.readFileSync)(module.fileId.path, "utf-8");
|
|
10713
|
+
} catch {
|
|
10714
|
+
continue;
|
|
10715
|
+
}
|
|
10716
|
+
let parseResult;
|
|
10717
|
+
try {
|
|
10718
|
+
parseResult = (0, oxc_parser.parseSync)(module.fileId.path, sourceText);
|
|
10719
|
+
} catch {
|
|
10720
|
+
continue;
|
|
10721
|
+
}
|
|
10722
|
+
const programNode = parseResult.program;
|
|
10723
|
+
const { localTypeNames, exportedNames } = collectLocalTypeNames(programNode);
|
|
10724
|
+
if (localTypeNames.size === 0) continue;
|
|
10725
|
+
const publicSignatureReferences = collectPublicSignatureReferences(programNode);
|
|
10726
|
+
if (publicSignatureReferences.length === 0) continue;
|
|
10727
|
+
const lineStarts = computeLineStarts(sourceText);
|
|
10728
|
+
const seenPairs = /* @__PURE__ */ new Set();
|
|
10729
|
+
for (const reference of publicSignatureReferences) {
|
|
10730
|
+
if (!localTypeNames.has(reference.typeName)) continue;
|
|
10731
|
+
if (exportedNames.has(reference.typeName)) continue;
|
|
10732
|
+
const pairKey = `${reference.exportName}::${reference.typeName}`;
|
|
10733
|
+
if (seenPairs.has(pairKey)) continue;
|
|
10734
|
+
seenPairs.add(pairKey);
|
|
10735
|
+
const { line, column } = offsetToLineColumn(reference.byteOffset, lineStarts);
|
|
10736
|
+
findings.push({
|
|
10737
|
+
path: module.fileId.path,
|
|
10738
|
+
exportName: reference.exportName,
|
|
10739
|
+
typeName: reference.typeName,
|
|
10740
|
+
line,
|
|
10741
|
+
column,
|
|
10742
|
+
confidence: "high",
|
|
10743
|
+
reason: `${reference.exportName}'s signature references ${reference.typeName}, declared locally but not exported — consumers can't satisfy the type without importing it`
|
|
10744
|
+
});
|
|
10745
|
+
}
|
|
10746
|
+
}
|
|
10747
|
+
findings.sort((leftLeak, rightLeak) => {
|
|
10748
|
+
if (leftLeak.path !== rightLeak.path) return leftLeak.path.localeCompare(rightLeak.path);
|
|
10749
|
+
return leftLeak.line - rightLeak.line;
|
|
10750
|
+
});
|
|
10751
|
+
return findings;
|
|
10752
|
+
};
|
|
10753
|
+
|
|
10754
|
+
//#endregion
|
|
10755
|
+
//#region src/report/typescript-smells.ts
|
|
10756
|
+
const parseSource = (filePath) => {
|
|
10757
|
+
let sourceText;
|
|
10758
|
+
try {
|
|
10759
|
+
sourceText = (0, node_fs.readFileSync)(filePath, "utf-8");
|
|
10760
|
+
} catch {
|
|
10761
|
+
return;
|
|
10762
|
+
}
|
|
10763
|
+
let parseResult;
|
|
10764
|
+
try {
|
|
10765
|
+
parseResult = (0, oxc_parser.parseSync)(filePath, sourceText);
|
|
10766
|
+
} catch {
|
|
10767
|
+
return;
|
|
10768
|
+
}
|
|
10769
|
+
const rawComments = parseResult.comments;
|
|
10770
|
+
const comments = Array.isArray(rawComments) ? rawComments.filter(isParsedSourceComment) : [];
|
|
10771
|
+
return {
|
|
10772
|
+
programNode: parseResult.program,
|
|
10773
|
+
sourceText,
|
|
10774
|
+
lineStarts: computeLineStarts(sourceText),
|
|
10775
|
+
comments
|
|
10776
|
+
};
|
|
10777
|
+
};
|
|
10778
|
+
const isParsedSourceComment = (candidate) => {
|
|
10779
|
+
if (typeof candidate !== "object" || candidate === null) return false;
|
|
10780
|
+
const fields = candidate;
|
|
10781
|
+
return (fields.type === "Line" || fields.type === "Block") && typeof fields.value === "string" && typeof fields.start === "number" && typeof fields.end === "number";
|
|
10782
|
+
};
|
|
10783
|
+
const sliceSnippet = (sourceText, start, end) => {
|
|
10784
|
+
const SNIPPET_BUDGET_CHARS = 80;
|
|
10785
|
+
const raw = sourceText.slice(start, Math.min(end, start + SNIPPET_BUDGET_CHARS)).replace(/\s+/g, " ").trim();
|
|
10786
|
+
return end - start > SNIPPET_BUDGET_CHARS ? `${raw}…` : raw;
|
|
10787
|
+
};
|
|
10788
|
+
const isAnyOrUnknownTypeAnnotation = (typeAnnotation) => {
|
|
10789
|
+
if (!isAstNode(typeAnnotation)) return void 0;
|
|
10790
|
+
if (typeAnnotation.type === "TSAnyKeyword") return "any";
|
|
10791
|
+
if (typeAnnotation.type === "TSUnknownKeyword") return "unknown";
|
|
10792
|
+
};
|
|
10793
|
+
const isLiteralLikeNonNull = (expression) => {
|
|
10794
|
+
if (!isAstNode(expression)) return false;
|
|
10795
|
+
if (expression.type === "Literal") return expression.value !== null;
|
|
10796
|
+
if (expression.type === "TemplateLiteral" || expression.type === "ArrayExpression" || expression.type === "ObjectExpression" || expression.type === "FunctionExpression" || expression.type === "ArrowFunctionExpression" || expression.type === "ClassExpression") return true;
|
|
10797
|
+
return false;
|
|
10798
|
+
};
|
|
10799
|
+
const collectUnnecessaryAssertionsInNode = (node, filePath, sourceText, lineStarts, results) => {
|
|
10800
|
+
if (!isAstNode(node)) return;
|
|
10801
|
+
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression") {
|
|
10802
|
+
const innerExpression = node.expression;
|
|
10803
|
+
const typeAnnotation = node.typeAnnotation;
|
|
10804
|
+
if (node.type === "TSAsExpression") {
|
|
10805
|
+
const outerKind = isAnyOrUnknownTypeAnnotation(typeAnnotation);
|
|
10806
|
+
if (outerKind === void 0 && isAstNode(innerExpression) && innerExpression.type === "TSAsExpression") {
|
|
10807
|
+
const innerTypeAnnotation = innerExpression.typeAnnotation;
|
|
10808
|
+
const innerKind = isAnyOrUnknownTypeAnnotation(innerTypeAnnotation);
|
|
10809
|
+
if (innerKind !== void 0) pushAssertion(node, "redundant-double-assertion", `\`x as ${innerKind} as T\` first widens to ${innerKind} just to assert to T — drop the intermediate ${innerKind} and assert directly`, "if you must assert, write `x as T` directly", filePath, sourceText, lineStarts, results);
|
|
10810
|
+
}
|
|
10811
|
+
if (outerKind === "any") pushAssertion(node, "assertion-to-any", "`as any` opts out of TypeScript's type system — narrow to a specific type or use `unknown`", "replace `as any` with the actual type, or use `as unknown as T` only when you genuinely need to discard the inferred type", filePath, sourceText, lineStarts, results);
|
|
10812
|
+
}
|
|
10813
|
+
}
|
|
10814
|
+
if (node.type === "TSTypeAssertion") pushAssertion(node, "angle-bracket-assertion", "`<T>x` style assertion is parsed as a JSX tag in `.tsx` and is deprecated in mixed-extension projects — prefer `x as T`", "rewrite `<T>x` as `x as T`", filePath, sourceText, lineStarts, results);
|
|
10815
|
+
if (node.type === "TSNonNullExpression") {
|
|
10816
|
+
const innerExpression = node.expression;
|
|
10817
|
+
if (isAstNode(innerExpression) && innerExpression.type === "TSNonNullExpression") pushAssertion(node, "double-non-null", "`x!!` is the non-null assertion applied twice — the second `!` is always a no-op", "drop one of the `!` operators", filePath, sourceText, lineStarts, results);
|
|
10818
|
+
else if (isLiteralLikeNonNull(innerExpression)) pushAssertion(node, "redundant-non-null-on-literal", "`!` after a literal / array / object / function expression is redundant — those values are never null", "remove the trailing `!`", filePath, sourceText, lineStarts, results);
|
|
10819
|
+
}
|
|
10820
|
+
};
|
|
10821
|
+
const pushAssertion = (node, kind, reason, suggestion, filePath, sourceText, lineStarts, results) => {
|
|
10822
|
+
if (!isAstNode(node)) return;
|
|
10823
|
+
const startOffset = node.start;
|
|
10824
|
+
const endOffset = node.end;
|
|
10825
|
+
if (typeof startOffset !== "number" || typeof endOffset !== "number") return;
|
|
10826
|
+
const { line, column } = offsetToLineColumn(startOffset, lineStarts);
|
|
10827
|
+
const isHighConfidenceKind = kind === "double-non-null" || kind === "redundant-non-null-on-literal" || kind === "redundant-double-assertion";
|
|
10828
|
+
results.push({
|
|
10829
|
+
path: filePath,
|
|
10830
|
+
kind,
|
|
10831
|
+
snippet: sliceSnippet(sourceText, startOffset, endOffset),
|
|
10832
|
+
line,
|
|
10833
|
+
column,
|
|
10834
|
+
confidence: isHighConfidenceKind ? "high" : "medium",
|
|
10835
|
+
reason,
|
|
10836
|
+
suggestion
|
|
10837
|
+
});
|
|
10838
|
+
};
|
|
10839
|
+
const visitForUnnecessaryAssertions = (node, filePath, sourceText, lineStarts, results) => {
|
|
10840
|
+
if (!isAstNode(node)) return;
|
|
10841
|
+
collectUnnecessaryAssertionsInNode(node, filePath, sourceText, lineStarts, results);
|
|
10842
|
+
for (const propertyKey of Object.keys(node)) {
|
|
10843
|
+
if (propertyKey === "type" || propertyKey === "start" || propertyKey === "end" || propertyKey === "loc" || propertyKey === "range") continue;
|
|
10844
|
+
const value = node[propertyKey];
|
|
10845
|
+
if (Array.isArray(value)) for (const item of value) visitForUnnecessaryAssertions(item, filePath, sourceText, lineStarts, results);
|
|
10846
|
+
else if (value !== null && typeof value === "object") visitForUnnecessaryAssertions(value, filePath, sourceText, lineStarts, results);
|
|
10847
|
+
}
|
|
10848
|
+
};
|
|
10849
|
+
const importExpressionSpecifier = (importExpression) => {
|
|
10850
|
+
if (!isAstNode(importExpression)) return void 0;
|
|
10851
|
+
if (importExpression.type !== "ImportExpression") return void 0;
|
|
10852
|
+
const sourceNode = importExpression.source;
|
|
10853
|
+
if (!isAstNode(sourceNode)) return void 0;
|
|
10854
|
+
if (sourceNode.type !== "Literal") return void 0;
|
|
10855
|
+
const literalValue = sourceNode.value;
|
|
10856
|
+
return typeof literalValue === "string" ? literalValue : void 0;
|
|
10857
|
+
};
|
|
10858
|
+
const findThenImportInExpressionStatement = (expressionNode) => {
|
|
10859
|
+
if (!isAstNode(expressionNode)) return void 0;
|
|
10860
|
+
if (expressionNode.type !== "CallExpression") return void 0;
|
|
10861
|
+
const callee = expressionNode.callee;
|
|
10862
|
+
if (!isAstNode(callee)) return void 0;
|
|
10863
|
+
if (callee.type !== "MemberExpression" && callee.type !== "StaticMemberExpression") return void 0;
|
|
10864
|
+
const propertyNode = callee.property;
|
|
10865
|
+
const propertyName = isAstNode(propertyNode) ? propertyNode.name : void 0;
|
|
10866
|
+
if (propertyName !== "then" && propertyName !== "catch" && propertyName !== "finally") return void 0;
|
|
10867
|
+
const objectNode = callee.object;
|
|
10868
|
+
const specifier = importExpressionSpecifier(objectNode);
|
|
10869
|
+
if (specifier === void 0) return void 0;
|
|
10870
|
+
return {
|
|
10871
|
+
importExpression: objectNode,
|
|
10872
|
+
specifier
|
|
10873
|
+
};
|
|
10874
|
+
};
|
|
10875
|
+
const findAwaitImportInExpression = (expressionNode) => {
|
|
10876
|
+
if (!isAstNode(expressionNode)) return void 0;
|
|
10877
|
+
if (expressionNode.type !== "AwaitExpression") return void 0;
|
|
10878
|
+
const argumentNode = expressionNode.argument;
|
|
10879
|
+
const specifier = importExpressionSpecifier(argumentNode);
|
|
10880
|
+
if (specifier === void 0) return void 0;
|
|
10881
|
+
return {
|
|
10882
|
+
importExpression: argumentNode,
|
|
10883
|
+
specifier
|
|
10884
|
+
};
|
|
10885
|
+
};
|
|
10886
|
+
const collectLazyImportsAtTopLevel = (programNode, filePath, lineStarts, results) => {
|
|
10887
|
+
if (!isAstNode(programNode)) return;
|
|
10888
|
+
const programBody = programNode.body;
|
|
10889
|
+
if (!Array.isArray(programBody)) return;
|
|
10890
|
+
for (const topLevelStatement of programBody) {
|
|
10891
|
+
if (!isAstNode(topLevelStatement)) continue;
|
|
10892
|
+
if (topLevelStatement.type === "VariableDeclaration") {
|
|
10893
|
+
const declarators = topLevelStatement.declarations;
|
|
10894
|
+
if (!Array.isArray(declarators)) continue;
|
|
10895
|
+
for (const declarator of declarators) {
|
|
10896
|
+
if (!isAstNode(declarator)) continue;
|
|
10897
|
+
const initializer = declarator.init;
|
|
10898
|
+
const awaitImport = findAwaitImportInExpression(initializer);
|
|
10899
|
+
if (awaitImport) recordLazyImport(awaitImport, "top-level-await-import", filePath, lineStarts, results);
|
|
10900
|
+
}
|
|
10901
|
+
continue;
|
|
10902
|
+
}
|
|
10903
|
+
if (topLevelStatement.type === "ExpressionStatement") {
|
|
10904
|
+
const innerExpression = topLevelStatement.expression;
|
|
10905
|
+
const awaitImport = findAwaitImportInExpression(innerExpression);
|
|
10906
|
+
if (awaitImport) {
|
|
10907
|
+
recordLazyImport(awaitImport, "top-level-await-import", filePath, lineStarts, results);
|
|
10908
|
+
continue;
|
|
10909
|
+
}
|
|
10910
|
+
const thenImport = findThenImportInExpressionStatement(innerExpression);
|
|
10911
|
+
if (thenImport) recordLazyImport(thenImport, "top-level-then-import", filePath, lineStarts, results);
|
|
10912
|
+
}
|
|
10913
|
+
}
|
|
10914
|
+
};
|
|
10915
|
+
const recordLazyImport = (match, kind, filePath, lineStarts, results) => {
|
|
10916
|
+
if (!isAstNode(match.importExpression)) return;
|
|
10917
|
+
const startOffset = match.importExpression.start;
|
|
10918
|
+
if (typeof startOffset !== "number") return;
|
|
10919
|
+
const { line, column } = offsetToLineColumn(startOffset, lineStarts);
|
|
10920
|
+
results.push({
|
|
10921
|
+
path: filePath,
|
|
10922
|
+
specifier: match.specifier,
|
|
10923
|
+
kind,
|
|
10924
|
+
line,
|
|
10925
|
+
column,
|
|
10926
|
+
confidence: kind === "top-level-await-import" ? "high" : "medium",
|
|
10927
|
+
reason: kind === "top-level-await-import" ? `top-level \`await import("${match.specifier}")\` runs synchronously before the module finishes loading anyway — there is no laziness benefit, prefer a static \`import\`` : `top-level \`import("${match.specifier}").then(...)\` runs at module evaluation — prefer a static \`import\` and a regular function call unless the dynamic-import contract is intentional`
|
|
10928
|
+
});
|
|
10929
|
+
};
|
|
10930
|
+
const buildPackageJsonTypeCache = () => {
|
|
10931
|
+
const directoryToType = /* @__PURE__ */ new Map();
|
|
10932
|
+
const resolveModuleType = (filePath) => {
|
|
10933
|
+
let currentDirectory = (0, node_path.dirname)((0, node_path.resolve)(filePath));
|
|
10934
|
+
const visitedDirectories = [];
|
|
10935
|
+
while (true) {
|
|
10936
|
+
visitedDirectories.push(currentDirectory);
|
|
10937
|
+
const cached = directoryToType.get(currentDirectory);
|
|
10938
|
+
if (cached !== void 0) {
|
|
10939
|
+
for (const visitedDirectory of visitedDirectories) directoryToType.set(visitedDirectory, cached);
|
|
10940
|
+
return cached;
|
|
10941
|
+
}
|
|
10942
|
+
const packageJsonPath = (0, node_path.join)(currentDirectory, "package.json");
|
|
10943
|
+
if ((0, node_fs.existsSync)(packageJsonPath)) try {
|
|
10944
|
+
const packageJson = JSON.parse((0, node_fs.readFileSync)(packageJsonPath, "utf-8"));
|
|
10945
|
+
const moduleType = packageJson.type === "module" ? "module" : packageJson.type === "commonjs" ? "commonjs" : void 0;
|
|
10946
|
+
for (const visitedDirectory of visitedDirectories) directoryToType.set(visitedDirectory, moduleType);
|
|
10947
|
+
return moduleType;
|
|
10948
|
+
} catch {
|
|
10949
|
+
for (const visitedDirectory of visitedDirectories) directoryToType.set(visitedDirectory, void 0);
|
|
10950
|
+
return;
|
|
10951
|
+
}
|
|
10952
|
+
const parentDirectory = (0, node_path.dirname)(currentDirectory);
|
|
10953
|
+
if (parentDirectory === currentDirectory) {
|
|
10954
|
+
for (const visitedDirectory of visitedDirectories) directoryToType.set(visitedDirectory, void 0);
|
|
10955
|
+
return;
|
|
10956
|
+
}
|
|
10957
|
+
currentDirectory = parentDirectory;
|
|
10958
|
+
}
|
|
10959
|
+
};
|
|
10960
|
+
return { resolveModuleType };
|
|
10961
|
+
};
|
|
10962
|
+
const isEsmFilePath = (filePath, typeCache) => {
|
|
10963
|
+
if (filePath.endsWith(".mts") || filePath.endsWith(".mjs")) return true;
|
|
10964
|
+
if (filePath.endsWith(".cts") || filePath.endsWith(".cjs")) return false;
|
|
10965
|
+
return typeCache.resolveModuleType(filePath) === "module";
|
|
10966
|
+
};
|
|
10967
|
+
const collectCommonjsInEsm = (programNode, filePath, sourceText, lineStarts, results) => {
|
|
10968
|
+
if (!isAstNode(programNode)) return;
|
|
10969
|
+
visitForCommonjs(programNode, filePath, sourceText, lineStarts, results);
|
|
10970
|
+
};
|
|
10971
|
+
const visitForCommonjs = (node, filePath, sourceText, lineStarts, results) => {
|
|
10972
|
+
if (!isAstNode(node)) return;
|
|
10973
|
+
if (node.type === "CallExpression") {
|
|
10974
|
+
const callee = node.callee;
|
|
10975
|
+
if (isAstNode(callee) && callee.type === "Identifier") {
|
|
10976
|
+
if (callee.name === "require") {
|
|
10977
|
+
const callArguments = node.arguments;
|
|
10978
|
+
if (Array.isArray(callArguments) && callArguments.length > 0) {
|
|
10979
|
+
const firstArgument = callArguments[0];
|
|
10980
|
+
if (isAstNode(firstArgument) && firstArgument.type === "Literal" && typeof firstArgument.value === "string") {
|
|
10981
|
+
const startOffset = node.start;
|
|
10982
|
+
const endOffset = node.end;
|
|
10983
|
+
if (typeof startOffset === "number" && typeof endOffset === "number") {
|
|
10984
|
+
const { line, column } = offsetToLineColumn(startOffset, lineStarts);
|
|
10985
|
+
results.push({
|
|
10986
|
+
path: filePath,
|
|
10987
|
+
kind: "require",
|
|
10988
|
+
line,
|
|
10989
|
+
column,
|
|
10990
|
+
confidence: "high",
|
|
10991
|
+
reason: "synchronous `require()` is unavailable in native ESM — use a static `import` or top-level `await import()`",
|
|
10992
|
+
snippet: sliceSnippet(sourceText, startOffset, endOffset)
|
|
10993
|
+
});
|
|
10994
|
+
}
|
|
10995
|
+
}
|
|
10996
|
+
}
|
|
10997
|
+
}
|
|
10998
|
+
}
|
|
10999
|
+
}
|
|
11000
|
+
if (node.type === "AssignmentExpression") {
|
|
11001
|
+
const leftSide = node.left;
|
|
11002
|
+
if (isAstNode(leftSide)) {
|
|
11003
|
+
if (leftSide.type === "MemberExpression" || leftSide.type === "StaticMemberExpression") {
|
|
11004
|
+
const objectNode = leftSide.object;
|
|
11005
|
+
const propertyNode = leftSide.property;
|
|
11006
|
+
const objectName = isAstNode(objectNode) ? objectNode.name : void 0;
|
|
11007
|
+
const propertyName = isAstNode(propertyNode) ? propertyNode.name : void 0;
|
|
11008
|
+
if (objectName === "module" && propertyName === "exports") {
|
|
11009
|
+
const startOffset = node.start;
|
|
11010
|
+
const endOffset = node.end;
|
|
11011
|
+
if (typeof startOffset === "number" && typeof endOffset === "number") {
|
|
11012
|
+
const { line, column } = offsetToLineColumn(startOffset, lineStarts);
|
|
11013
|
+
results.push({
|
|
11014
|
+
path: filePath,
|
|
11015
|
+
kind: "module-exports",
|
|
11016
|
+
line,
|
|
11017
|
+
column,
|
|
11018
|
+
confidence: "high",
|
|
11019
|
+
reason: "`module.exports = ...` is CommonJS — replace with `export default` or named `export` for ESM",
|
|
11020
|
+
snippet: sliceSnippet(sourceText, startOffset, endOffset)
|
|
11021
|
+
});
|
|
11022
|
+
}
|
|
11023
|
+
} else if (objectName === "exports") {
|
|
11024
|
+
const startOffset = node.start;
|
|
11025
|
+
const endOffset = node.end;
|
|
11026
|
+
if (typeof startOffset === "number" && typeof endOffset === "number") {
|
|
11027
|
+
const { line, column } = offsetToLineColumn(startOffset, lineStarts);
|
|
11028
|
+
results.push({
|
|
11029
|
+
path: filePath,
|
|
11030
|
+
kind: "exports-assignment",
|
|
11031
|
+
line,
|
|
11032
|
+
column,
|
|
11033
|
+
confidence: "high",
|
|
11034
|
+
reason: "`exports.x = ...` is CommonJS — replace with a named `export` for ESM",
|
|
11035
|
+
snippet: sliceSnippet(sourceText, startOffset, endOffset)
|
|
11036
|
+
});
|
|
11037
|
+
}
|
|
11038
|
+
}
|
|
11039
|
+
}
|
|
11040
|
+
}
|
|
11041
|
+
}
|
|
11042
|
+
for (const propertyKey of Object.keys(node)) {
|
|
11043
|
+
if (propertyKey === "type" || propertyKey === "start" || propertyKey === "end" || propertyKey === "loc" || propertyKey === "range") continue;
|
|
11044
|
+
const value = node[propertyKey];
|
|
11045
|
+
if (Array.isArray(value)) for (const item of value) visitForCommonjs(item, filePath, sourceText, lineStarts, results);
|
|
11046
|
+
else if (value !== null && typeof value === "object") visitForCommonjs(value, filePath, sourceText, lineStarts, results);
|
|
11047
|
+
}
|
|
11048
|
+
};
|
|
11049
|
+
const TS_IGNORE_LEADING = /^\s*@ts-ignore\b/;
|
|
11050
|
+
const TS_NOCHECK_LEADING = /^\s*@ts-nocheck\b/;
|
|
11051
|
+
const TS_EXPECT_ERROR_LEADING = /^\s*@ts-expect-error\b(.*)$/;
|
|
11052
|
+
const collectTypeScriptEscapeHatches = (comments, filePath, lineStarts, results) => {
|
|
11053
|
+
for (const comment of comments) {
|
|
11054
|
+
const commentBody = comment.type === "Block" ? comment.value.split("\n")[0] : comment.value;
|
|
11055
|
+
if (TS_IGNORE_LEADING.test(commentBody)) {
|
|
11056
|
+
pushEscapeHatch(comment.start, "ts-ignore", "`@ts-ignore` silently swallows the next line's type errors forever — use `@ts-expect-error` so the suppression breaks if the underlying error gets fixed", "rewrite as `@ts-expect-error <why this is okay>`", "high", filePath, lineStarts, results);
|
|
11057
|
+
continue;
|
|
11058
|
+
}
|
|
11059
|
+
if (TS_NOCHECK_LEADING.test(commentBody)) {
|
|
11060
|
+
pushEscapeHatch(comment.start, "ts-nocheck", "`@ts-nocheck` disables type checking for the entire file — fix the underlying types or scope the suppression to a specific line", "remove `@ts-nocheck` and address the underlying type errors, or use per-line `@ts-expect-error` with a justification", "medium", filePath, lineStarts, results);
|
|
11061
|
+
continue;
|
|
11062
|
+
}
|
|
11063
|
+
const expectErrorMatch = commentBody.match(TS_EXPECT_ERROR_LEADING);
|
|
11064
|
+
if (expectErrorMatch) {
|
|
11065
|
+
if ((expectErrorMatch[1] ?? "").trim().length === 0) pushEscapeHatch(comment.start, "ts-expect-error-without-explanation", "`@ts-expect-error` should be followed by a comment explaining why the next line legitimately produces a type error", "add a short justification: `// @ts-expect-error: <why this is okay>`", "low", filePath, lineStarts, results);
|
|
11066
|
+
}
|
|
11067
|
+
}
|
|
11068
|
+
};
|
|
11069
|
+
const pushEscapeHatch = (commentStartOffset, kind, reason, suggestion, confidence, filePath, lineStarts, results) => {
|
|
11070
|
+
const { line, column } = offsetToLineColumn(commentStartOffset, lineStarts);
|
|
11071
|
+
results.push({
|
|
11072
|
+
path: filePath,
|
|
11073
|
+
kind,
|
|
11074
|
+
line,
|
|
11075
|
+
column,
|
|
11076
|
+
confidence,
|
|
11077
|
+
reason,
|
|
11078
|
+
suggestion
|
|
11079
|
+
});
|
|
11080
|
+
};
|
|
11081
|
+
const isTypeScriptOrJsFile = (filePath) => filePath.endsWith(".ts") || filePath.endsWith(".tsx") || filePath.endsWith(".mts") || filePath.endsWith(".cts") || filePath.endsWith(".js") || filePath.endsWith(".jsx") || filePath.endsWith(".mjs") || filePath.endsWith(".cjs");
|
|
11082
|
+
const isTypeScriptFileExtension = (filePath) => filePath.endsWith(".ts") || filePath.endsWith(".tsx") || filePath.endsWith(".mts") || filePath.endsWith(".cts");
|
|
11083
|
+
const detectTypeScriptSmells = (graph) => {
|
|
11084
|
+
const unnecessaryAssertions = [];
|
|
11085
|
+
const lazyImportsAtTopLevel = [];
|
|
11086
|
+
const commonjsInEsm = [];
|
|
11087
|
+
const typeScriptEscapeHatches = [];
|
|
11088
|
+
const packageJsonTypeCache = buildPackageJsonTypeCache();
|
|
11089
|
+
for (const module of graph.modules) {
|
|
11090
|
+
if (module.isDeclarationFile) continue;
|
|
11091
|
+
const filePath = module.fileId.path;
|
|
11092
|
+
if (!isTypeScriptOrJsFile(filePath)) continue;
|
|
11093
|
+
const parsedSource = parseSource(filePath);
|
|
11094
|
+
if (!parsedSource) continue;
|
|
11095
|
+
if (isTypeScriptFileExtension(filePath)) {
|
|
11096
|
+
visitForUnnecessaryAssertions(parsedSource.programNode, filePath, parsedSource.sourceText, parsedSource.lineStarts, unnecessaryAssertions);
|
|
11097
|
+
collectTypeScriptEscapeHatches(parsedSource.comments, filePath, parsedSource.lineStarts, typeScriptEscapeHatches);
|
|
11098
|
+
}
|
|
11099
|
+
collectLazyImportsAtTopLevel(parsedSource.programNode, filePath, parsedSource.lineStarts, lazyImportsAtTopLevel);
|
|
11100
|
+
if (isEsmFilePath(filePath, packageJsonTypeCache)) collectCommonjsInEsm(parsedSource.programNode, filePath, parsedSource.sourceText, parsedSource.lineStarts, commonjsInEsm);
|
|
11101
|
+
}
|
|
11102
|
+
unnecessaryAssertions.sort((leftFinding, rightFinding) => {
|
|
11103
|
+
if (leftFinding.path !== rightFinding.path) return leftFinding.path.localeCompare(rightFinding.path);
|
|
11104
|
+
return leftFinding.line - rightFinding.line;
|
|
11105
|
+
});
|
|
11106
|
+
lazyImportsAtTopLevel.sort((leftFinding, rightFinding) => {
|
|
11107
|
+
if (leftFinding.path !== rightFinding.path) return leftFinding.path.localeCompare(rightFinding.path);
|
|
11108
|
+
return leftFinding.line - rightFinding.line;
|
|
11109
|
+
});
|
|
11110
|
+
commonjsInEsm.sort((leftFinding, rightFinding) => {
|
|
11111
|
+
if (leftFinding.path !== rightFinding.path) return leftFinding.path.localeCompare(rightFinding.path);
|
|
11112
|
+
return leftFinding.line - rightFinding.line;
|
|
11113
|
+
});
|
|
11114
|
+
typeScriptEscapeHatches.sort((leftFinding, rightFinding) => {
|
|
11115
|
+
if (leftFinding.path !== rightFinding.path) return leftFinding.path.localeCompare(rightFinding.path);
|
|
11116
|
+
return leftFinding.line - rightFinding.line;
|
|
11117
|
+
});
|
|
11118
|
+
return {
|
|
11119
|
+
unnecessaryAssertions,
|
|
11120
|
+
lazyImportsAtTopLevel,
|
|
11121
|
+
commonjsInEsm,
|
|
11122
|
+
typeScriptEscapeHatches
|
|
11123
|
+
};
|
|
11124
|
+
};
|
|
11125
|
+
|
|
11126
|
+
//#endregion
|
|
11127
|
+
//#region src/utils/run-safe-detector.ts
|
|
11128
|
+
const runSafeDetector = (input) => {
|
|
11129
|
+
try {
|
|
11130
|
+
return input.detector();
|
|
11131
|
+
} catch (caughtError) {
|
|
11132
|
+
input.errorSink.push(new DetectorError({
|
|
11133
|
+
module: input.module,
|
|
11134
|
+
message: `${input.detectorName} threw ${input.contextDescription}`,
|
|
11135
|
+
detail: describeUnknownError(caughtError)
|
|
11136
|
+
}));
|
|
11137
|
+
return input.fallback;
|
|
11138
|
+
}
|
|
11139
|
+
};
|
|
11140
|
+
|
|
11141
|
+
//#endregion
|
|
11142
|
+
//#region src/semantic/program.ts
|
|
11143
|
+
const failureFor = (reason, message, options = { rootDir: "" }) => {
|
|
11144
|
+
return {
|
|
11145
|
+
reason,
|
|
11146
|
+
message,
|
|
11147
|
+
error: new TypeScriptError({
|
|
11148
|
+
code: {
|
|
11149
|
+
"no-tsconfig": "tsconfig-not-found",
|
|
11150
|
+
"tsconfig-parse-error": "tsconfig-parse-failed",
|
|
11151
|
+
"program-creation-failed": "ts-program-creation-failed",
|
|
11152
|
+
"too-many-files": "ts-program-too-large",
|
|
11153
|
+
"typescript-load-failed": "ts-not-loadable"
|
|
11154
|
+
}[reason],
|
|
11155
|
+
severity: reason === "no-tsconfig" ? "info" : "warning",
|
|
11156
|
+
message,
|
|
11157
|
+
path: options.rootDir || void 0,
|
|
11158
|
+
detail: options.detail
|
|
11159
|
+
})
|
|
11160
|
+
};
|
|
11161
|
+
};
|
|
11162
|
+
const findNearestTsconfig = (rootDir, explicitPath) => {
|
|
11163
|
+
if (explicitPath) {
|
|
11164
|
+
const absoluteExplicit = (0, node_path.resolve)(rootDir, explicitPath);
|
|
11165
|
+
if ((0, node_fs.existsSync)(absoluteExplicit)) return absoluteExplicit;
|
|
11166
|
+
return;
|
|
11167
|
+
}
|
|
11168
|
+
for (const candidateName of DEFAULT_SEMANTIC_TSCONFIG_NAMES) {
|
|
11169
|
+
const candidatePath = (0, node_path.resolve)(rootDir, candidateName);
|
|
11170
|
+
if ((0, node_fs.existsSync)(candidatePath)) return candidatePath;
|
|
11171
|
+
}
|
|
11172
|
+
};
|
|
11173
|
+
const createSemanticContext = (rootDir, tsconfigPath) => {
|
|
11174
|
+
const resolvedTsconfigPath = findNearestTsconfig(rootDir, tsconfigPath);
|
|
11175
|
+
if (!resolvedTsconfigPath) return {
|
|
11176
|
+
ok: false,
|
|
11177
|
+
failure: failureFor("no-tsconfig", `No tsconfig found under ${rootDir}`, { rootDir })
|
|
11178
|
+
};
|
|
11179
|
+
let configFileContent;
|
|
11180
|
+
try {
|
|
11181
|
+
configFileContent = typescript.default.readConfigFile(resolvedTsconfigPath, typescript.default.sys.readFile);
|
|
11182
|
+
} catch (readError) {
|
|
11183
|
+
return {
|
|
11184
|
+
ok: false,
|
|
11185
|
+
failure: failureFor("tsconfig-parse-error", "ts.readConfigFile threw", {
|
|
11186
|
+
rootDir: resolvedTsconfigPath,
|
|
11187
|
+
detail: describeUnknownError(readError)
|
|
11188
|
+
})
|
|
11189
|
+
};
|
|
11190
|
+
}
|
|
11191
|
+
if (configFileContent.error) return {
|
|
11192
|
+
ok: false,
|
|
11193
|
+
failure: failureFor("tsconfig-parse-error", typescript.default.flattenDiagnosticMessageText(configFileContent.error.messageText, "\n"), { rootDir: resolvedTsconfigPath })
|
|
11194
|
+
};
|
|
11195
|
+
let parsedCommandLine;
|
|
8548
11196
|
try {
|
|
8549
11197
|
parsedCommandLine = typescript.default.parseJsonConfigFileContent(configFileContent.config, typescript.default.sys, (0, node_path.dirname)(resolvedTsconfigPath), {
|
|
8550
11198
|
noEmit: true,
|
|
@@ -8915,6 +11563,55 @@ const detectUnusedEnumMembers = (graph, config, context, referenceIndex) => {
|
|
|
8915
11563
|
return findings;
|
|
8916
11564
|
};
|
|
8917
11565
|
|
|
11566
|
+
//#endregion
|
|
11567
|
+
//#region src/utils/is-framework-lifecycle-method.ts
|
|
11568
|
+
/**
|
|
11569
|
+
* Methods invoked by-name by React / Angular runtimes. Static "no caller"
|
|
11570
|
+
* analysis can't see those call sites, so without this allowlist
|
|
11571
|
+
* `unusedClassMembers` would fire on every component.
|
|
11572
|
+
*/
|
|
11573
|
+
const FRAMEWORK_LIFECYCLE_METHODS = new Set([
|
|
11574
|
+
"render",
|
|
11575
|
+
"componentDidMount",
|
|
11576
|
+
"componentDidUpdate",
|
|
11577
|
+
"componentWillUnmount",
|
|
11578
|
+
"shouldComponentUpdate",
|
|
11579
|
+
"getSnapshotBeforeUpdate",
|
|
11580
|
+
"getDerivedStateFromProps",
|
|
11581
|
+
"getDerivedStateFromError",
|
|
11582
|
+
"componentDidCatch",
|
|
11583
|
+
"componentWillMount",
|
|
11584
|
+
"componentWillReceiveProps",
|
|
11585
|
+
"componentWillUpdate",
|
|
11586
|
+
"UNSAFE_componentWillMount",
|
|
11587
|
+
"UNSAFE_componentWillReceiveProps",
|
|
11588
|
+
"UNSAFE_componentWillUpdate",
|
|
11589
|
+
"getChildContext",
|
|
11590
|
+
"contextType",
|
|
11591
|
+
"ngOnInit",
|
|
11592
|
+
"ngOnDestroy",
|
|
11593
|
+
"ngOnChanges",
|
|
11594
|
+
"ngDoCheck",
|
|
11595
|
+
"ngAfterContentInit",
|
|
11596
|
+
"ngAfterContentChecked",
|
|
11597
|
+
"ngAfterViewInit",
|
|
11598
|
+
"ngAfterViewChecked",
|
|
11599
|
+
"ngAcceptInputType",
|
|
11600
|
+
"canActivate",
|
|
11601
|
+
"canDeactivate",
|
|
11602
|
+
"canActivateChild",
|
|
11603
|
+
"canMatch",
|
|
11604
|
+
"resolve",
|
|
11605
|
+
"intercept",
|
|
11606
|
+
"transform",
|
|
11607
|
+
"validate",
|
|
11608
|
+
"registerOnChange",
|
|
11609
|
+
"registerOnTouched",
|
|
11610
|
+
"writeValue",
|
|
11611
|
+
"setDisabledState"
|
|
11612
|
+
]);
|
|
11613
|
+
const isFrameworkLifecycleMethod = (name) => FRAMEWORK_LIFECYCLE_METHODS.has(name);
|
|
11614
|
+
|
|
8918
11615
|
//#endregion
|
|
8919
11616
|
//#region src/semantic/unused-class-members.ts
|
|
8920
11617
|
const isClassExported = (declaration) => {
|
|
@@ -9046,6 +11743,7 @@ const detectUnusedClassMembers = (graph, config, context, referenceIndex, decora
|
|
|
9046
11743
|
if (memberHasExternalReference(memberSymbol, referenceIndex)) continue;
|
|
9047
11744
|
const memberName = typescript.default.isIdentifier(member.name) ? member.name.text : member.name.getText(sourceFile);
|
|
9048
11745
|
if (overriddenMemberNames.has(memberName)) continue;
|
|
11746
|
+
if (isFrameworkLifecycleMethod(memberName)) continue;
|
|
9049
11747
|
const { line: zeroIndexedLine, character: zeroIndexedColumn } = sourceFile.getLineAndCharacterOfPosition(member.getStart(sourceFile));
|
|
9050
11748
|
const line = zeroIndexedLine + 1;
|
|
9051
11749
|
const column = zeroIndexedColumn + 1;
|
|
@@ -9446,6 +12144,22 @@ const generateReport = (graph, config) => {
|
|
|
9446
12144
|
const simplifiableFunctions = config.reportRedundancy ? safeReportDetector("detectSimplifiableFunctions", () => detectSimplifiableFunctions(graph), [], errorSink) : [];
|
|
9447
12145
|
const simplifiableExpressions = config.reportRedundancy ? safeReportDetector("detectSimplifiableExpressions", () => detectSimplifiableExpressions(graph), [], errorSink) : [];
|
|
9448
12146
|
const duplicateConstants = config.reportRedundancy ? safeReportDetector("detectDuplicateConstants", () => detectDuplicateConstants(graph), [], errorSink) : [];
|
|
12147
|
+
const crossFileDuplicateExports = config.reportRedundancy ? safeReportDetector("detectCrossFileDuplicateExports", () => detectCrossFileDuplicateExports(graph), [], errorSink) : [];
|
|
12148
|
+
const duplicateBlockResult = safeReportDetector("detectDuplicateBlocks", () => detectDuplicateBlocks(graph, config.duplicateBlocks, config.rootDir), {
|
|
12149
|
+
duplicateBlocks: [],
|
|
12150
|
+
duplicateBlockClusters: [],
|
|
12151
|
+
shadowedDirectoryPairs: []
|
|
12152
|
+
}, errorSink);
|
|
12153
|
+
const reExportCycles = safeReportDetector("detectReExportCycles", () => detectReExportCycles(graph), [], errorSink);
|
|
12154
|
+
const featureFlags = safeReportDetector("detectFeatureFlags", () => detectFeatureFlags(graph, config.featureFlags), [], errorSink);
|
|
12155
|
+
const complexFunctions = safeReportDetector("detectComplexHotspots", () => detectComplexHotspots(graph, config.complexity), [], errorSink);
|
|
12156
|
+
const privateTypeLeaks = safeReportDetector("detectPrivateTypeLeaks", () => detectPrivateTypeLeaks(graph), [], errorSink);
|
|
12157
|
+
const typeScriptSmellsResult = safeReportDetector("detectTypeScriptSmells", () => detectTypeScriptSmells(graph), {
|
|
12158
|
+
unnecessaryAssertions: [],
|
|
12159
|
+
lazyImportsAtTopLevel: [],
|
|
12160
|
+
commonjsInEsm: [],
|
|
12161
|
+
typeScriptEscapeHatches: []
|
|
12162
|
+
}, errorSink);
|
|
9449
12163
|
let semanticResult;
|
|
9450
12164
|
try {
|
|
9451
12165
|
semanticResult = runSemanticAnalysis(graph, config);
|
|
@@ -9470,6 +12184,7 @@ const generateReport = (graph, config) => {
|
|
|
9470
12184
|
errorSink.push(semanticError);
|
|
9471
12185
|
}
|
|
9472
12186
|
const redundantAliases = config.reportRedundancy ? [...syntacticRedundantAliases, ...semanticResult.redundantAliases] : [];
|
|
12187
|
+
if (featureFlags.length > 0) correlateFlagsWithDeadCode(featureFlags, { unusedExports });
|
|
9473
12188
|
const totalExports = graph.modules.reduce((exportCount, module) => exportCount + module.exports.filter((exportInfo) => !(exportInfo.name === "*" && exportInfo.isNamespaceReExport)).length, 0);
|
|
9474
12189
|
return {
|
|
9475
12190
|
unusedFiles,
|
|
@@ -9490,6 +12205,18 @@ const generateReport = (graph, config) => {
|
|
|
9490
12205
|
simplifiableFunctions,
|
|
9491
12206
|
simplifiableExpressions,
|
|
9492
12207
|
duplicateConstants,
|
|
12208
|
+
crossFileDuplicateExports,
|
|
12209
|
+
duplicateBlocks: duplicateBlockResult.duplicateBlocks,
|
|
12210
|
+
duplicateBlockClusters: duplicateBlockResult.duplicateBlockClusters,
|
|
12211
|
+
shadowedDirectoryPairs: duplicateBlockResult.shadowedDirectoryPairs,
|
|
12212
|
+
reExportCycles,
|
|
12213
|
+
featureFlags,
|
|
12214
|
+
complexFunctions,
|
|
12215
|
+
privateTypeLeaks,
|
|
12216
|
+
unnecessaryAssertions: typeScriptSmellsResult.unnecessaryAssertions,
|
|
12217
|
+
lazyImportsAtTopLevel: typeScriptSmellsResult.lazyImportsAtTopLevel,
|
|
12218
|
+
commonjsInEsm: typeScriptSmellsResult.commonjsInEsm,
|
|
12219
|
+
typeScriptEscapeHatches: typeScriptSmellsResult.typeScriptEscapeHatches,
|
|
9493
12220
|
analysisErrors: errorSink,
|
|
9494
12221
|
totalFiles: graph.modules.length,
|
|
9495
12222
|
totalExports,
|
|
@@ -9577,18 +12304,53 @@ const detectReactNative = (rootDir, workspacePackages) => {
|
|
|
9577
12304
|
*
|
|
9578
12305
|
* - `reportRedundancy: true` — on because redundancy findings are mostly
|
|
9579
12306
|
* high-signal and the detectors carry their own confidence tiers.
|
|
12307
|
+
*
|
|
12308
|
+
* - `duplicateBlocks: undefined` — token-based copy-paste detection (suffix
|
|
12309
|
+
* array + LCP) is opt-in. It re-parses every source
|
|
12310
|
+
* file to emit a token stream and adds significant runtime to the scan.
|
|
12311
|
+
* Pass `duplicateBlocks: { enabled: true }` to turn it on.
|
|
9580
12312
|
*/
|
|
9581
12313
|
const fillSemanticConfig = (semanticOverrides) => {
|
|
9582
|
-
|
|
12314
|
+
const overrides = semanticOverrides ?? {};
|
|
12315
|
+
return {
|
|
12316
|
+
enabled: overrides.enabled ?? true,
|
|
12317
|
+
reportUnusedTypes: overrides.reportUnusedTypes ?? true,
|
|
12318
|
+
reportUnusedEnumMembers: overrides.reportUnusedEnumMembers ?? true,
|
|
12319
|
+
reportUnusedClassMembers: overrides.reportUnusedClassMembers ?? false,
|
|
12320
|
+
reportRedundantVariableAliases: overrides.reportRedundantVariableAliases ?? true,
|
|
12321
|
+
reportMisclassifiedDependencies: overrides.reportMisclassifiedDependencies ?? true,
|
|
12322
|
+
reportRoundTripAliases: overrides.reportRoundTripAliases ?? true,
|
|
12323
|
+
decoratorAllowlist: overrides.decoratorAllowlist ?? DEFAULT_SEMANTIC_DECORATOR_ALLOWLIST
|
|
12324
|
+
};
|
|
12325
|
+
};
|
|
12326
|
+
const fillDuplicateBlocksConfig = (duplicateBlocksOverrides) => {
|
|
12327
|
+
const overrides = duplicateBlocksOverrides ?? {};
|
|
12328
|
+
return {
|
|
12329
|
+
enabled: overrides.enabled ?? true,
|
|
12330
|
+
mode: overrides.mode ?? "semantic",
|
|
12331
|
+
minTokens: overrides.minTokens ?? 50,
|
|
12332
|
+
minLines: overrides.minLines ?? 5,
|
|
12333
|
+
minOccurrences: overrides.minOccurrences ?? 2,
|
|
12334
|
+
skipLocal: overrides.skipLocal ?? false
|
|
12335
|
+
};
|
|
12336
|
+
};
|
|
12337
|
+
const fillFeatureFlagsConfig = (flagsOverrides) => {
|
|
12338
|
+
const overrides = flagsOverrides ?? {};
|
|
12339
|
+
return {
|
|
12340
|
+
enabled: overrides.enabled ?? true,
|
|
12341
|
+
extraEnvPrefixes: overrides.extraEnvPrefixes ?? [],
|
|
12342
|
+
extraSdkFunctionNames: overrides.extraSdkFunctionNames ?? [],
|
|
12343
|
+
detectConfigObjects: overrides.detectConfigObjects ?? false
|
|
12344
|
+
};
|
|
12345
|
+
};
|
|
12346
|
+
const fillComplexityConfig = (complexityOverrides) => {
|
|
12347
|
+
const overrides = complexityOverrides ?? {};
|
|
9583
12348
|
return {
|
|
9584
|
-
enabled:
|
|
9585
|
-
|
|
9586
|
-
|
|
9587
|
-
|
|
9588
|
-
|
|
9589
|
-
reportMisclassifiedDependencies: semanticOverrides.reportMisclassifiedDependencies ?? true,
|
|
9590
|
-
reportRoundTripAliases: semanticOverrides.reportRoundTripAliases ?? true,
|
|
9591
|
-
decoratorAllowlist: semanticOverrides.decoratorAllowlist ?? DEFAULT_SEMANTIC_DECORATOR_ALLOWLIST
|
|
12349
|
+
enabled: overrides.enabled ?? true,
|
|
12350
|
+
cyclomaticThreshold: overrides.cyclomaticThreshold ?? 10,
|
|
12351
|
+
cognitiveThreshold: overrides.cognitiveThreshold ?? 15,
|
|
12352
|
+
paramCountThreshold: overrides.paramCountThreshold ?? 5,
|
|
12353
|
+
functionLineThreshold: overrides.functionLineThreshold ?? 80
|
|
9592
12354
|
};
|
|
9593
12355
|
};
|
|
9594
12356
|
const defineConfig = (options) => ({
|
|
@@ -9600,7 +12362,10 @@ const defineConfig = (options) => ({
|
|
|
9600
12362
|
reportTypes: options.reportTypes ?? false,
|
|
9601
12363
|
includeEntryExports: options.includeEntryExports ?? false,
|
|
9602
12364
|
reportRedundancy: options.reportRedundancy ?? true,
|
|
9603
|
-
semantic: fillSemanticConfig(options.semantic)
|
|
12365
|
+
semantic: fillSemanticConfig(options.semantic),
|
|
12366
|
+
duplicateBlocks: fillDuplicateBlocksConfig(options.duplicateBlocks),
|
|
12367
|
+
featureFlags: fillFeatureFlagsConfig(options.featureFlags),
|
|
12368
|
+
complexity: fillComplexityConfig(options.complexity)
|
|
9604
12369
|
});
|
|
9605
12370
|
const buildEmptyScanResult = (errors, elapsedMs) => ({
|
|
9606
12371
|
unusedFiles: [],
|
|
@@ -9621,6 +12386,18 @@ const buildEmptyScanResult = (errors, elapsedMs) => ({
|
|
|
9621
12386
|
simplifiableFunctions: [],
|
|
9622
12387
|
simplifiableExpressions: [],
|
|
9623
12388
|
duplicateConstants: [],
|
|
12389
|
+
crossFileDuplicateExports: [],
|
|
12390
|
+
duplicateBlocks: [],
|
|
12391
|
+
duplicateBlockClusters: [],
|
|
12392
|
+
shadowedDirectoryPairs: [],
|
|
12393
|
+
reExportCycles: [],
|
|
12394
|
+
featureFlags: [],
|
|
12395
|
+
complexFunctions: [],
|
|
12396
|
+
privateTypeLeaks: [],
|
|
12397
|
+
unnecessaryAssertions: [],
|
|
12398
|
+
lazyImportsAtTopLevel: [],
|
|
12399
|
+
commonjsInEsm: [],
|
|
12400
|
+
typeScriptEscapeHatches: [],
|
|
9624
12401
|
analysisErrors: errors,
|
|
9625
12402
|
totalFiles: 0,
|
|
9626
12403
|
totalExports: 0,
|