knip 6.44.2 → 6.45.0

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.
Files changed (218) hide show
  1. package/dist/ConfigurationChief.d.ts +23 -1033
  2. package/dist/ConfigurationChief.js +71 -15
  3. package/dist/DependencyDeputy.d.ts +10 -6
  4. package/dist/DependencyDeputy.js +51 -27
  5. package/dist/IssueCollector.d.ts +14 -11
  6. package/dist/IssueCollector.js +25 -20
  7. package/dist/IssueFixer.js +1 -1
  8. package/dist/PackagePeeker.d.ts +6 -0
  9. package/dist/PackagePeeker.js +43 -14
  10. package/dist/ProjectPrincipal.d.ts +6 -3
  11. package/dist/ProjectPrincipal.js +83 -32
  12. package/dist/WorkspaceWorker.d.ts +5 -1
  13. package/dist/WorkspaceWorker.js +39 -22
  14. package/dist/binaries/bash-parser.js +1 -7
  15. package/dist/binaries/command.d.ts +3 -3
  16. package/dist/binaries/command.js +6 -2
  17. package/dist/binaries/fallback.js +1 -1
  18. package/dist/binaries/plugins.d.ts +4 -1
  19. package/dist/binaries/plugins.js +66 -18
  20. package/dist/binaries/resolvers/npm.js +7 -5
  21. package/dist/binaries/resolvers/npx.js +8 -40
  22. package/dist/binaries/util.d.ts +13 -1
  23. package/dist/binaries/util.js +35 -1
  24. package/dist/cli.js +10 -84
  25. package/dist/configuration-hints.d.ts +16 -0
  26. package/dist/configuration-hints.js +64 -0
  27. package/dist/constants.d.ts +2 -1
  28. package/dist/constants.js +2 -0
  29. package/dist/graph/SubpathReferences.d.ts +33 -0
  30. package/dist/graph/SubpathReferences.js +114 -0
  31. package/dist/graph/analyze.d.ts +4 -1
  32. package/dist/graph/analyze.js +16 -5
  33. package/dist/graph/build.d.ts +3 -1
  34. package/dist/graph/build.js +47 -23
  35. package/dist/graph-explorer/operations/resolve-export-origins.js +1 -1
  36. package/dist/plugins/angular/index.js +10 -17
  37. package/dist/plugins/aws-cdk/index.d.ts +3 -0
  38. package/dist/plugins/aws-cdk/index.js +32 -0
  39. package/dist/plugins/aws-cdk/types.d.ts +5 -0
  40. package/dist/plugins/aws-cdk/types.js +1 -0
  41. package/dist/plugins/babel/index.d.ts +1 -1
  42. package/dist/plugins/babel/index.js +19 -5
  43. package/dist/plugins/bun/index.js +8 -7
  44. package/dist/plugins/c8/index.js +17 -4
  45. package/dist/plugins/changesets/index.js +3 -2
  46. package/dist/plugins/commitizen/index.js +6 -3
  47. package/dist/plugins/commitlint/index.js +10 -11
  48. package/dist/plugins/cypress/index.js +4 -2
  49. package/dist/plugins/danger/index.js +1 -1
  50. package/dist/plugins/docusaurus/helpers.js +6 -6
  51. package/dist/plugins/docusaurus/index.js +2 -2
  52. package/dist/plugins/electron-forge/index.js +2 -2
  53. package/dist/plugins/electron-vite/index.js +2 -2
  54. package/dist/plugins/eleventy/index.js +3 -2
  55. package/dist/plugins/eslint/helpers.js +4 -3
  56. package/dist/plugins/eslint/resolveFromAST.js +2 -2
  57. package/dist/plugins/evalite/index.d.ts +3 -0
  58. package/dist/plugins/evalite/index.js +26 -0
  59. package/dist/plugins/flue/index.js +2 -2
  60. package/dist/plugins/github-action/index.js +2 -2
  61. package/dist/plugins/github-actions/index.js +2 -2
  62. package/dist/plugins/graphql-codegen/index.js +13 -9
  63. package/dist/plugins/index.d.ts +5 -0
  64. package/dist/plugins/index.js +10 -0
  65. package/dist/plugins/jest/index.js +5 -5
  66. package/dist/plugins/knex/index.js +2 -2
  67. package/dist/plugins/laravel-vite-plugin/resolveFromAST.js +2 -2
  68. package/dist/plugins/lunaria/index.js +5 -2
  69. package/dist/plugins/markdownlint/index.js +8 -2
  70. package/dist/plugins/marko/index.js +2 -2
  71. package/dist/plugins/marko/taglibs.d.ts +1 -1
  72. package/dist/plugins/marko/taglibs.js +4 -28
  73. package/dist/plugins/mdx/index.d.ts +2 -1
  74. package/dist/plugins/mdx/index.js +7 -4
  75. package/dist/plugins/mdxlint/index.js +18 -15
  76. package/dist/plugins/mikro-orm/index.d.ts +3 -0
  77. package/dist/plugins/mikro-orm/index.js +77 -0
  78. package/dist/plugins/mikro-orm/types.d.ts +21 -0
  79. package/dist/plugins/mikro-orm/types.js +1 -0
  80. package/dist/plugins/msw/index.js +6 -3
  81. package/dist/plugins/msw/types.d.ts +1 -1
  82. package/dist/plugins/netlify/index.js +7 -3
  83. package/dist/plugins/next/index.js +63 -9
  84. package/dist/plugins/next/types.d.ts +5 -0
  85. package/dist/plugins/next-mdx/resolveFromAST.js +15 -10
  86. package/dist/plugins/nitro/index.js +5 -6
  87. package/dist/plugins/node/index.js +4 -4
  88. package/dist/plugins/npm-package-json-lint/index.js +10 -3
  89. package/dist/plugins/nuxt/index.js +18 -10
  90. package/dist/plugins/nuxt/types.d.ts +4 -0
  91. package/dist/plugins/nx/index.js +6 -3
  92. package/dist/plugins/openclaw/index.js +11 -11
  93. package/dist/plugins/oxlint/index.js +2 -2
  94. package/dist/plugins/payload/index.js +1 -1
  95. package/dist/plugins/playwright/index.js +3 -3
  96. package/dist/plugins/pm2/index.js +7 -7
  97. package/dist/plugins/prettier/index.js +2 -1
  98. package/dist/plugins/prisma/index.js +2 -2
  99. package/dist/plugins/quasar/resolveFromAST.js +14 -9
  100. package/dist/plugins/react-router/index.js +2 -2
  101. package/dist/plugins/release-it/index.js +6 -2
  102. package/dist/plugins/remix/index.js +3 -3
  103. package/dist/plugins/retry-cli/index.d.ts +3 -0
  104. package/dist/plugins/retry-cli/index.js +14 -0
  105. package/dist/plugins/rollup/index.js +1 -4
  106. package/dist/plugins/rstest/index.js +3 -3
  107. package/dist/plugins/semantic-release/index.js +3 -3
  108. package/dist/plugins/serverless-framework/index.js +10 -8
  109. package/dist/plugins/solidjs-vite-plugin/resolveFromAST.js +2 -2
  110. package/dist/plugins/stencil/resolveFromAST.js +5 -5
  111. package/dist/plugins/stryker/index.js +2 -2
  112. package/dist/plugins/swc/index.js +4 -3
  113. package/dist/plugins/tailwind/index.js +2 -2
  114. package/dist/plugins/tanstack-router/index.js +2 -2
  115. package/dist/plugins/ts-node/index.js +6 -1
  116. package/dist/plugins/turbo/index.js +3 -3
  117. package/dist/plugins/typedoc/index.js +2 -2
  118. package/dist/plugins/typescript/content-mappers.d.ts +1 -1
  119. package/dist/plugins/typescript/index.js +6 -9
  120. package/dist/plugins/varlock/scan.js +1 -1
  121. package/dist/plugins/vite/helpers.d.ts +1 -0
  122. package/dist/plugins/vite/helpers.js +13 -3
  123. package/dist/plugins/vite/index.js +5 -2
  124. package/dist/plugins/vite-plugin-pwa/resolveFromAST.js +2 -2
  125. package/dist/plugins/vitest/index.js +14 -16
  126. package/dist/plugins/vue/index.js +1 -1
  127. package/dist/plugins/web-test-runner/index.d.ts +3 -0
  128. package/dist/plugins/web-test-runner/index.js +86 -0
  129. package/dist/plugins/wrangler/index.js +3 -3
  130. package/dist/plugins/yarn/index.js +5 -5
  131. package/dist/plugins.js +2 -5
  132. package/dist/reporters/github-actions.js +3 -7
  133. package/dist/reporters/json.d.ts +1 -0
  134. package/dist/reporters/json.js +1 -0
  135. package/dist/reporters/util/configuration-hints.d.ts +3 -1
  136. package/dist/reporters/util/configuration-hints.js +40 -21
  137. package/dist/reporters/util/util.d.ts +1 -1
  138. package/dist/run-cli.d.ts +2 -0
  139. package/dist/run-cli.js +82 -0
  140. package/dist/run.js +12 -0
  141. package/dist/session/incremental.d.ts +1 -0
  142. package/dist/session/incremental.js +1 -0
  143. package/dist/session/watch.js +2 -1
  144. package/dist/types/PluginNames.d.ts +2 -2
  145. package/dist/types/PluginNames.js +5 -0
  146. package/dist/types/args.d.ts +3 -1
  147. package/dist/types/config.d.ts +17 -10
  148. package/dist/types/configuration.d.ts +45 -0
  149. package/dist/types/configuration.js +1 -0
  150. package/dist/types/issues.d.ts +4 -0
  151. package/dist/types/module-graph.d.ts +1 -0
  152. package/dist/types/options.d.ts +1 -0
  153. package/dist/types/package-json.d.ts +3 -8
  154. package/dist/types/workspace.d.ts +1 -0
  155. package/dist/typescript/get-imports-and-exports.js +18 -6
  156. package/dist/typescript/resolve-module-names.js +7 -45
  157. package/dist/typescript/visitors/calls.d.ts +1 -1
  158. package/dist/typescript/visitors/calls.js +14 -10
  159. package/dist/typescript/visitors/exports.js +16 -26
  160. package/dist/typescript/visitors/imports.d.ts +2 -1
  161. package/dist/typescript/visitors/imports.js +41 -24
  162. package/dist/typescript/visitors/members.d.ts +1 -1
  163. package/dist/typescript/visitors/members.js +38 -24
  164. package/dist/typescript/visitors/shadow.d.ts +2 -0
  165. package/dist/typescript/visitors/shadow.js +25 -0
  166. package/dist/typescript/visitors/walk.d.ts +20 -12
  167. package/dist/typescript/visitors/walk.js +78 -48
  168. package/dist/util/Performance.d.ts +25 -3
  169. package/dist/util/Performance.js +51 -27
  170. package/dist/util/cli-arguments.d.ts +2 -1
  171. package/dist/util/cli-arguments.js +4 -2
  172. package/dist/util/create-input-handler.js +138 -119
  173. package/dist/util/create-options.d.ts +4 -2071
  174. package/dist/util/create-options.js +6 -4
  175. package/dist/util/create-workspace-graph.js +2 -1
  176. package/dist/util/errors.d.ts +5 -2
  177. package/dist/util/errors.js +11 -18
  178. package/dist/util/fs.js +4 -2
  179. package/dist/util/input.d.ts +2 -2
  180. package/dist/util/input.js +2 -2
  181. package/dist/util/is-plugin-name.d.ts +2 -0
  182. package/dist/util/is-plugin-name.js +3 -0
  183. package/dist/util/jiti.d.ts +2 -1
  184. package/dist/util/jiti.js +10 -3
  185. package/dist/util/load-config.d.ts +1 -1
  186. package/dist/util/load-config.js +3 -2
  187. package/dist/util/loader.js +3 -2
  188. package/dist/util/map-workspaces.js +23 -1
  189. package/dist/util/module-graph.js +1 -0
  190. package/dist/util/modules.d.ts +2 -0
  191. package/dist/util/modules.js +78 -0
  192. package/dist/util/package-json.d.ts +12 -5
  193. package/dist/util/package-json.js +54 -26
  194. package/dist/util/parse-and-convert-gitignores.d.ts +2 -2
  195. package/dist/util/parse-and-convert-gitignores.js +5 -5
  196. package/dist/util/parse-configuration.d.ts +2 -0
  197. package/dist/util/parse-configuration.js +66 -0
  198. package/dist/util/plugin.js +8 -12
  199. package/dist/util/resolve.d.ts +1 -0
  200. package/dist/util/resolve.js +3 -2
  201. package/dist/util/scripts.d.ts +0 -5
  202. package/dist/util/scripts.js +1 -32
  203. package/dist/util/to-source-path.d.ts +4 -1
  204. package/dist/util/to-source-path.js +2 -2
  205. package/dist/util/unhandled-rejection.d.ts +8 -0
  206. package/dist/util/unhandled-rejection.js +29 -0
  207. package/dist/util/validation.d.ts +11 -0
  208. package/dist/util/validation.js +68 -0
  209. package/dist/util/watch.d.ts +3 -1
  210. package/dist/util/watch.js +5 -2
  211. package/dist/version.d.ts +1 -1
  212. package/dist/version.js +1 -1
  213. package/package.json +6 -7
  214. package/schema.json +34 -0
  215. package/dist/schema/configuration.d.ts +0 -3103
  216. package/dist/schema/configuration.js +0 -94
  217. package/dist/schema/plugins.d.ts +0 -1019
  218. package/dist/schema/plugins.js +0 -215
@@ -1,6 +1,7 @@
1
1
  import type { ArrayExpression, CallExpression, Expression, ImportExpression, VariableDeclarator } from 'oxc-parser';
2
2
  import type { WalkState } from './walk.ts';
3
+ export declare function markDynamicNamespaceOpaque(name: string, pos: number, s: WalkState): void;
3
4
  export declare function getPromiseAllElements(expression: Expression): ArrayExpression['elements'] | undefined;
4
- export declare function handleVariableDeclarator(node: VariableDeclarator, s: WalkState): void;
5
+ export declare function handleVariableDeclarator(node: VariableDeclarator, isVar: boolean, s: WalkState): void;
5
6
  export declare function handleImportThen(node: CallExpression, s: WalkState): void;
6
7
  export declare function handleImportExpression(node: ImportExpression, s: WalkState): void;
@@ -2,6 +2,26 @@ import { IMPORT_FLAGS, IMPORT_STAR, OPAQUE } from '../../constants.js';
2
2
  import { addValue } from '../../util/module-graph.js';
3
3
  import { isInNodeModules } from '../../util/path.js';
4
4
  import { getStringValue, isStringLiteral } from '../ast-nodes.js';
5
+ import { isShadowedIn } from './shadow.js';
6
+ export function markDynamicNamespaceOpaque(name, pos, s) {
7
+ const _import = s.localImportMap.get(name);
8
+ if (!_import?.isDynamicImport || !_import.isNamespace)
9
+ return;
10
+ if (isShadowedIn(s, name, pos))
11
+ return;
12
+ const internalImport = s.internal.get(_import.filePath);
13
+ if (internalImport)
14
+ addValue(internalImport.import, OPAQUE, s.filePath);
15
+ }
16
+ const getBlockScope = (s) => {
17
+ const i = s.scopeDepth - 1;
18
+ return i < 0 ? undefined : [s.scopeStarts[i], s.scopeEnds[i]];
19
+ };
20
+ const setDynamicNamespace = (name, filePath, scope, isVar, s) => {
21
+ if (scope && !isVar)
22
+ s.scopedImports.push({ name, end: scope[1], prev: s.localImportMap.get(name) });
23
+ s.localImportMap.set(name, { importedName: IMPORT_STAR, filePath, isNamespace: true, isDynamicImport: true, scope });
24
+ };
5
25
  export function getPromiseAllElements(expression) {
6
26
  if (expression.type === 'CallExpression' &&
7
27
  expression.callee.type === 'MemberExpression' &&
@@ -13,7 +33,7 @@ export function getPromiseAllElements(expression) {
13
33
  return expression.arguments[0].elements;
14
34
  }
15
35
  }
16
- export function handleVariableDeclarator(node, s) {
36
+ export function handleVariableDeclarator(node, isVar, s) {
17
37
  const init = node.init;
18
38
  if (!init)
19
39
  return;
@@ -26,7 +46,7 @@ export function handleVariableDeclarator(node, s) {
26
46
  }
27
47
  if (importExpr && isStringLiteral(importExpr.source)) {
28
48
  const caughtFlag = init.type === 'AwaitExpression' && s.caughtTryBlockEnds.length > 0 ? IMPORT_FLAGS.CAUGHT : IMPORT_FLAGS.NONE;
29
- handleDynamicImportBinding(node.id, importExpr, init.type === 'AwaitExpression', caughtFlag, s);
49
+ handleDynamicImportBinding(node.id, importExpr, init.type === 'AwaitExpression', caughtFlag, getBlockScope(s), isVar, s);
30
50
  return;
31
51
  }
32
52
  if (init.type === 'CallExpression' &&
@@ -89,12 +109,7 @@ export function handleVariableDeclarator(node, s) {
89
109
  s.addImport(specifier, 'default', binding.name, undefined, imp.source.start, IMPORT_FLAGS.DYNAMIC | caughtFlag);
90
110
  const resolved = s.resolveModule(specifier, s.filePath);
91
111
  if (resolved && !resolved.isExternalLibraryImport && !isInNodeModules(resolved.resolvedFileName)) {
92
- s.localImportMap.set(binding.name, {
93
- importedName: IMPORT_STAR,
94
- filePath: resolved.resolvedFileName,
95
- isNamespace: true,
96
- isDynamicImport: true,
97
- });
112
+ setDynamicNamespace(binding.name, resolved.resolvedFileName, getBlockScope(s), isVar, s);
98
113
  }
99
114
  }
100
115
  else {
@@ -108,12 +123,18 @@ export function handleVariableDeclarator(node, s) {
108
123
  const registerAlias = (expr) => {
109
124
  if (expr?.type === 'Identifier') {
110
125
  const _import = s.localImportMap.get(expr.name);
126
+ if (_import?.isDynamicImport && isShadowedIn(s, expr.name, expr.start))
127
+ return;
111
128
  if (_import) {
112
- s.addImportAlias(aliasName, expr.name, _import.filePath);
129
+ s.addImportAlias(aliasName, expr.name, _import);
113
130
  if (_import.isNamespace) {
114
131
  const internalImport = s.internal.get(_import.filePath);
115
- if (internalImport)
116
- internalImport.refs.add(expr.name);
132
+ if (internalImport) {
133
+ if (_import.isDynamicImport)
134
+ addValue(internalImport.import, OPAQUE, s.filePath);
135
+ else
136
+ internalImport.refs.add(expr.name);
137
+ }
117
138
  }
118
139
  }
119
140
  }
@@ -129,8 +150,8 @@ export function handleVariableDeclarator(node, s) {
129
150
  for (const prop of init.properties) {
130
151
  if (prop.type === 'SpreadElement' && prop.argument?.type === 'Identifier') {
131
152
  const _import = s.localImportMap.get(prop.argument.name);
132
- if (_import) {
133
- s.addImportAlias(aliasName, prop.argument.name, _import.filePath);
153
+ if (_import && !(_import.isDynamicImport && isShadowedIn(s, prop.argument.name, prop.argument.start))) {
154
+ s.addImportAlias(aliasName, prop.argument.name, _import);
134
155
  if (_import.isNamespace) {
135
156
  const internalImport = s.internal.get(_import.filePath);
136
157
  if (internalImport)
@@ -144,7 +165,7 @@ export function handleVariableDeclarator(node, s) {
144
165
  prop.key?.type === 'Identifier') {
145
166
  const nsName = prop.value.name;
146
167
  const _import = s.localImportMap.get(nsName);
147
- if (_import?.isNamespace) {
168
+ if (_import?.isNamespace && !(_import.isDynamicImport && isShadowedIn(s, nsName, prop.value.start))) {
148
169
  const internalImport = s.internal.get(_import.filePath);
149
170
  if (internalImport)
150
171
  internalImport.refs.add(nsName);
@@ -153,7 +174,7 @@ export function handleVariableDeclarator(node, s) {
153
174
  map = new Map();
154
175
  s.nsContainers.set(aliasName, map);
155
176
  }
156
- map.set(prop.key.name, nsName);
177
+ map.set(prop.key.name, { id: nsName, import: _import });
157
178
  }
158
179
  }
159
180
  }
@@ -222,7 +243,7 @@ export function handleVariableDeclarator(node, s) {
222
243
  internalImport.refs.add(defaultValue.name);
223
244
  }
224
245
  if (prop.value.left?.type === 'Identifier') {
225
- s.addImportAlias(prop.value.left.name, defaultValue.name, _import.filePath);
246
+ s.addImportAlias(prop.value.left.name, defaultValue.name, _import);
226
247
  }
227
248
  }
228
249
  }
@@ -230,7 +251,7 @@ export function handleVariableDeclarator(node, s) {
230
251
  }
231
252
  }
232
253
  }
233
- function handleDynamicImportBinding(id, importExpr, isAwaited, caughtFlag, s) {
254
+ function handleDynamicImportBinding(id, importExpr, isAwaited, caughtFlag, scope, isVar, s) {
234
255
  s.handledImportExpressions.add(importExpr.start);
235
256
  const specifier = getStringValue(importExpr.source);
236
257
  if (id.type === 'ObjectPattern') {
@@ -255,12 +276,7 @@ function handleDynamicImportBinding(id, importExpr, isAwaited, caughtFlag, s) {
255
276
  s.addImport(specifier, 'default', id.name, undefined, importExpr.source.start, IMPORT_FLAGS.DYNAMIC | caughtFlag);
256
277
  const resolved = s.resolveModule(specifier, s.filePath);
257
278
  if (resolved && !resolved.isExternalLibraryImport && !isInNodeModules(resolved.resolvedFileName)) {
258
- s.localImportMap.set(id.name, {
259
- importedName: IMPORT_STAR,
260
- filePath: resolved.resolvedFileName,
261
- isNamespace: true,
262
- isDynamicImport: true,
263
- });
279
+ setDynamicNamespace(id.name, resolved.resolvedFileName, scope, isVar, s);
264
280
  }
265
281
  }
266
282
  else {
@@ -284,7 +300,8 @@ export function handleImportThen(node, s) {
284
300
  if (param?.type !== 'Identifier' && param?.type !== 'ObjectPattern')
285
301
  return;
286
302
  const caughtFlag = s.caughtImportExpressions.has(callee.object.start) ? IMPORT_FLAGS.CAUGHT : IMPORT_FLAGS.NONE;
287
- handleDynamicImportBinding(param, callee.object, true, caughtFlag, s);
303
+ const body = fn.body ?? fn;
304
+ handleDynamicImportBinding(param, callee.object, true, caughtFlag, [body.start, body.end], false, s);
288
305
  }
289
306
  export function handleImportExpression(node, s) {
290
307
  if (s.handledImportExpressions.has(node.start))
@@ -1,4 +1,4 @@
1
1
  import type { MemberExpression, JSXMemberExpression } from 'oxc-parser';
2
- import { type WalkState } from './walk.ts';
2
+ import type { WalkState } from './walk.ts';
3
3
  export declare function handleMemberExpression(node: MemberExpression, s: WalkState): void;
4
4
  export declare function handleJSXMemberExpression(node: JSXMemberExpression, s: WalkState): void;
@@ -1,7 +1,7 @@
1
- import { OPAQUE } from '../../constants.js';
1
+ import { IMPORT_FLAGS, OPAQUE } from '../../constants.js';
2
2
  import { addValue } from '../../util/module-graph.js';
3
3
  import { getStringValue, isStringLiteral } from '../ast-nodes.js';
4
- import { isShadowed } from './walk.js';
4
+ import { isShadowedIn } from './shadow.js';
5
5
  const chainNames = [];
6
6
  function markWholeObjectRead(internalImport, id) {
7
7
  (internalImport.enumerated ??= new Set()).add(id);
@@ -10,6 +10,24 @@ function isNumericKey(value) {
10
10
  return value != null && Number.isFinite(Number(value)) && String(Number(value)) === value;
11
11
  }
12
12
  export function handleMemberExpression(node, s) {
13
+ if (node.object.type === 'CallExpression' &&
14
+ node.object.callee.type === 'Identifier' &&
15
+ node.object.callee.name === 'require' &&
16
+ node.object.arguments.length === 1 &&
17
+ isStringLiteral(node.object.arguments[0]) &&
18
+ !s.handledImportExpressions.has(node.object.start)) {
19
+ let memberName;
20
+ if (!node.computed && node.property.type === 'Identifier')
21
+ memberName = node.property.name;
22
+ else if (node.computed && isStringLiteral(node.property))
23
+ memberName = getStringValue(node.property);
24
+ if (memberName) {
25
+ const specifier = getStringValue(node.object.arguments[0]);
26
+ const reqTags = s.currentVarDeclStart >= 0 ? s.getJSDocTags(s.currentVarDeclStart) : undefined;
27
+ s.addImport(specifier, memberName, undefined, undefined, node.object.arguments[0].start, IMPORT_FLAGS.NONE, undefined, reqTags);
28
+ }
29
+ return;
30
+ }
13
31
  if (node.object.type === 'MemberExpression' &&
14
32
  node.object.object.type === 'MemberExpression' &&
15
33
  !node.computed &&
@@ -18,7 +36,7 @@ export function handleMemberExpression(node, s) {
18
36
  }
19
37
  if (node.object.type === 'Identifier') {
20
38
  const localName = node.object.name;
21
- const shadowed = isShadowed(localName, node.object.start);
39
+ const shadowed = isShadowedIn(s, localName, node.object.start);
22
40
  const _import = !shadowed ? s.localImportMap.get(localName) : undefined;
23
41
  if (_import) {
24
42
  const internalImport = s.internal.get(_import.filePath);
@@ -78,12 +96,11 @@ export function handleMemberExpression(node, s) {
78
96
  isWholeRead = true;
79
97
  }
80
98
  for (const alias of aliases) {
81
- const internalImport = s.internal.get(alias.filePath);
99
+ const internalImport = s.internal.get(alias.import.filePath);
82
100
  if (!internalImport)
83
101
  continue;
84
102
  if (isWholeRead) {
85
- const origin = s.localImportMap.get(alias.id);
86
- markWholeObjectRead(internalImport, origin?.importedName ?? alias.id);
103
+ markWholeObjectRead(internalImport, alias.import.importedName);
87
104
  }
88
105
  else if (memberName) {
89
106
  s.addNsMemberRefs(internalImport, alias.id, memberName);
@@ -97,7 +114,7 @@ export function handleMemberExpression(node, s) {
97
114
  node.object.object.type === 'Identifier' &&
98
115
  node.object.property.type === 'Identifier') {
99
116
  const rootName = node.object.object.name;
100
- if (!isShadowed(rootName, node.object.object.start)) {
117
+ if (!isShadowedIn(s, rootName, node.object.object.start)) {
101
118
  const _import = s.localImportMap.get(rootName);
102
119
  if (_import?.isNamespace) {
103
120
  const internalImport = s.internal.get(_import.filePath);
@@ -119,7 +136,7 @@ export function handleMemberExpression(node, s) {
119
136
  !node.computed &&
120
137
  node.property.type === 'Identifier') {
121
138
  const rootName = node.object.object.name;
122
- if (!isShadowed(rootName, node.object.object.start)) {
139
+ if (!isShadowedIn(s, rootName, node.object.object.start)) {
123
140
  const _import = s.localImportMap.get(rootName);
124
141
  if (_import) {
125
142
  const internalImport = s.internal.get(_import.filePath);
@@ -155,7 +172,7 @@ export function handleMemberExpression(node, s) {
155
172
  chainNames.push(root.property.name);
156
173
  root = root.object;
157
174
  }
158
- if (root.type === 'Identifier' && !isShadowed(root.name, root.start)) {
175
+ if (root.type === 'Identifier' && !isShadowedIn(s, root.name, root.start)) {
159
176
  const _import = s.localImportMap.get(root.name);
160
177
  const internalImport = _import && s.internal.get(_import.filePath);
161
178
  if (internalImport || !_import) {
@@ -176,21 +193,18 @@ export function handleMemberExpression(node, s) {
176
193
  node.object.property.type === 'Identifier') {
177
194
  const containerName = node.object.object.name;
178
195
  const propKey = node.object.property.name;
179
- const nsName = s.nsContainers.get(containerName)?.get(propKey);
180
- if (nsName) {
181
- const _import = s.localImportMap.get(nsName);
182
- if (_import) {
183
- const internalImport = s.internal.get(_import.filePath);
184
- if (internalImport) {
185
- let memberName;
186
- if (!node.computed && node.property.type === 'Identifier')
187
- memberName = node.property.name;
188
- else if (node.computed && isStringLiteral(node.property))
189
- memberName = getStringValue(node.property);
190
- if (memberName) {
191
- s.addNsMemberRefs(internalImport, nsName, memberName);
192
- s.accessedNsContainers.add(`${containerName}.${propKey}`);
193
- }
196
+ const ref = s.nsContainers.get(containerName)?.get(propKey);
197
+ if (ref) {
198
+ const internalImport = s.internal.get(ref.import.filePath);
199
+ if (internalImport) {
200
+ let memberName;
201
+ if (!node.computed && node.property.type === 'Identifier')
202
+ memberName = node.property.name;
203
+ else if (node.computed && isStringLiteral(node.property))
204
+ memberName = getStringValue(node.property);
205
+ if (memberName) {
206
+ s.addNsMemberRefs(internalImport, ref.id, memberName);
207
+ s.accessedNsContainers.add(`${containerName}.${propKey}`);
194
208
  }
195
209
  }
196
210
  }
@@ -0,0 +1,2 @@
1
+ import type { WalkState } from './walk.ts';
2
+ export declare const isShadowedIn: (s: WalkState, name: string, pos: number) => boolean;
@@ -0,0 +1,25 @@
1
+ export const isShadowedIn = (s, name, pos) => {
2
+ if (s.shadowScopes.size === 0)
3
+ return false;
4
+ const ranges = s.shadowScopes.get(name);
5
+ if (!ranges)
6
+ return false;
7
+ const scope = s.localImportMap.get(name)?.scope;
8
+ if (scope) {
9
+ for (let i = ranges.length - 1; i >= 0; i--) {
10
+ const range = ranges[i];
11
+ if (range[0] < scope[0])
12
+ break;
13
+ if (range[0] === scope[0] && range[1] >= scope[1])
14
+ continue;
15
+ if (pos >= range[0] && pos < range[1])
16
+ return true;
17
+ }
18
+ return false;
19
+ }
20
+ for (const range of ranges) {
21
+ if (pos >= range[0] && pos < range[1])
22
+ return true;
23
+ }
24
+ return false;
25
+ };
@@ -5,6 +5,17 @@ import type { Fix } from '../../types/exports.ts';
5
5
  import type { IssueSymbol, SymbolType } from '../../types/issues.ts';
6
6
  import type { Export, ExportMember, ImportMap, ImportMaps } from '../../types/module-graph.ts';
7
7
  import { type ResolveModule } from '../ast-nodes.ts';
8
+ export type LocalImport = {
9
+ importedName: string;
10
+ filePath: string;
11
+ isNamespace: boolean;
12
+ isDynamicImport?: boolean;
13
+ scope?: [number, number];
14
+ };
15
+ export type LocalImportRef = {
16
+ id: string;
17
+ import: LocalImport;
18
+ };
8
19
  interface WalkContext {
9
20
  lineStarts: number[];
10
21
  skipExports: boolean;
@@ -15,19 +26,11 @@ interface WalkContext {
15
26
  scripts: Set<string>;
16
27
  addImport: (specifier: string, identifier: string | undefined, alias: string | undefined, namespace: string | undefined, pos: number, modifiers: number, specifierPos?: number, jsDocTags?: Set<string>) => void;
17
28
  addNsMemberRefs: (internalImport: ImportMaps, namespace: string, member: string | string[]) => void;
18
- addImportAlias: (aliasName: string, id: string, filePath: string) => void;
29
+ addImportAlias: (aliasName: string, id: string, _import: LocalImport) => void;
19
30
  internal: ImportMap;
20
- localImportMap: Map<string, {
21
- importedName: string;
22
- filePath: string;
23
- isNamespace: boolean;
24
- isDynamicImport?: boolean;
25
- }>;
31
+ localImportMap: Map<string, LocalImport>;
26
32
  localDeclarationTypes: Map<string, SymbolType>;
27
- importAliases: Map<string, Set<{
28
- id: string;
29
- filePath: string;
30
- }>>;
33
+ importAliases: Map<string, Set<LocalImportRef>>;
31
34
  referencedInExport: Map<string, Set<string>>;
32
35
  skipBareExprRefs: boolean;
33
36
  localRefs: Set<string> | undefined;
@@ -58,7 +61,7 @@ export interface WalkState extends WalkContext {
58
61
  functionCaughtTryBlockEnds: number[][];
59
62
  bareExprRefs: Set<string>;
60
63
  accessedAliases: Set<string>;
61
- nsContainers: Map<string, Map<string, string>>;
64
+ nsContainers: Map<string, Map<string, LocalImportRef>>;
62
65
  accessedNsContainers: Set<string>;
63
66
  chainedMemberExprs: WeakSet<object>;
64
67
  currentVarDeclStart: number;
@@ -69,6 +72,11 @@ export interface WalkState extends WalkContext {
69
72
  scopeStarts: number[];
70
73
  scopeEnds: number[];
71
74
  shadowScopes: Map<string, [number, number][]>;
75
+ scopedImports: {
76
+ name: string;
77
+ end: number;
78
+ prev: LocalImport | undefined;
79
+ }[];
72
80
  localDeclarations: Map<string, FunctionNode | Class | VariableDeclarator>;
73
81
  localInterfaces: Map<string, TSInterfaceDeclaration | null>;
74
82
  pendingCallRefs: Array<{
@@ -7,8 +7,9 @@ import { collectAugmentationRefs, getLineAndCol, getStringValue, isStringLiteral
7
7
  import { EMPTY_TAGS } from './jsdoc.js';
8
8
  import { handleCallExpression, handleNewExpression, trackCustomElementRegistry } from './calls.js';
9
9
  import { handleExportAssignment, handleExportDefault, handleExportNamed, handleExpressionStatement, } from './exports.js';
10
- import { getPromiseAllElements, handleImportExpression, handleImportThen, handleVariableDeclarator, } from './imports.js';
10
+ import { getPromiseAllElements, handleImportExpression, handleImportThen, handleVariableDeclarator, markDynamicNamespaceOpaque, } from './imports.js';
11
11
  import { handleJSXMemberExpression, handleMemberExpression } from './members.js';
12
+ import { isShadowedIn } from './shadow.js';
12
13
  let state;
13
14
  const _getFix = (start, end, flags) => state.options.isFixExports ? [start, end, flags ?? FIX_FLAGS.NONE] : undefined;
14
15
  const _getTypeFix = (start, end) => state.options.isFixTypes ? [start, end, FIX_FLAGS.NONE] : undefined;
@@ -197,20 +198,7 @@ const _addRefInExport = (name, exportName) => {
197
198
  state.referencedInExport.set(name, new Set([exportName]));
198
199
  };
199
200
  const _isInNamespace = (node) => state.nsRanges.length > 0 && state.nsRanges.some(([start, end]) => node.start >= start && node.end <= end);
200
- export const isShadowed = (name, pos) => {
201
- if (state.shadowScopes.size === 0)
202
- return false;
203
- const ranges = state.shadowScopes.get(name);
204
- if (!ranges)
205
- return false;
206
- if (state.localImportMap.get(name)?.isDynamicImport)
207
- return false;
208
- for (const range of ranges) {
209
- if (pos >= range[0] && pos <= range[1])
210
- return true;
211
- }
212
- return false;
213
- };
201
+ export const isShadowed = (name, pos) => isShadowedIn(state, name, pos);
214
202
  const _addLocalRef = (name, pos) => {
215
203
  if (isShadowed(name, pos))
216
204
  return;
@@ -261,17 +249,46 @@ const _addParamShadows = (params, body) => {
261
249
  for (const param of items)
262
250
  _collectBindingNames(param, range);
263
251
  };
252
+ const _restoreScopedImports = (end) => {
253
+ const stack = state.scopedImports;
254
+ while (stack.length > 0 && stack[stack.length - 1].end <= end) {
255
+ const { name, prev } = stack.pop();
256
+ if (prev)
257
+ state.localImportMap.set(name, prev);
258
+ else
259
+ state.localImportMap.delete(name);
260
+ }
261
+ };
262
+ const _enterScope = (node) => {
263
+ state.scopeStarts[state.scopeDepth] = node.start;
264
+ state.scopeEnds[state.scopeDepth] = node.end;
265
+ state.scopeDepth++;
266
+ };
267
+ const _exitScope = (node) => {
268
+ state.scopeDepth--;
269
+ _restoreScopedImports(node.end);
270
+ };
264
271
  const coreVisitorObject = {
265
272
  BlockStatement(node) {
266
- state.scopeStarts[state.scopeDepth] = node.start;
267
- state.scopeEnds[state.scopeDepth] = node.end;
268
- state.scopeDepth++;
273
+ _enterScope(node);
269
274
  },
270
275
  'BlockStatement:exit'(node) {
271
- state.scopeDepth--;
276
+ _exitScope(node);
272
277
  if (state.caughtTryBlockEnds[state.caughtTryBlockEnds.length - 1] === node.end)
273
278
  state.caughtTryBlockEnds.pop();
274
279
  },
280
+ ForStatement(node) {
281
+ _enterScope(node);
282
+ },
283
+ 'ForStatement:exit'(node) {
284
+ _exitScope(node);
285
+ },
286
+ SwitchStatement(node) {
287
+ _enterScope(node);
288
+ },
289
+ 'SwitchStatement:exit'(node) {
290
+ _exitScope(node);
291
+ },
275
292
  TSModuleDeclaration(node) {
276
293
  state.nsRanges.push([node.start, node.end]);
277
294
  if (node.kind === 'global' || !isStringLiteral(node.id))
@@ -303,6 +320,8 @@ const coreVisitorObject = {
303
320
  if (node.id?.name) {
304
321
  state.localDeclarationTypes.set(node.id.name, SYMBOL_TYPE.CLASS);
305
322
  state.localDeclarations.set(node.id.name, node);
323
+ if (state.scopeDepth > 0)
324
+ _addShadow(node.id.name);
306
325
  }
307
326
  },
308
327
  'ClassDeclaration:exit'() {
@@ -314,11 +333,13 @@ const coreVisitorObject = {
314
333
  'ClassExpression:exit'() {
315
334
  state.classNameStack.pop();
316
335
  },
317
- StaticBlock() {
336
+ StaticBlock(node) {
318
337
  state.staticBlockDepth++;
338
+ _enterScope(node);
319
339
  },
320
- 'StaticBlock:exit'() {
340
+ 'StaticBlock:exit'(node) {
321
341
  state.staticBlockDepth--;
342
+ _exitScope(node);
322
343
  },
323
344
  FunctionDeclaration(node) {
324
345
  state.functionCaughtTryBlockEnds.push(state.caughtTryBlockEnds);
@@ -347,8 +368,9 @@ const coreVisitorObject = {
347
368
  state.caughtTryBlockEnds = [];
348
369
  _addParamShadows(node.params, node.body);
349
370
  },
350
- 'ArrowFunctionExpression:exit'() {
371
+ 'ArrowFunctionExpression:exit'(node) {
351
372
  state.caughtTryBlockEnds = state.functionCaughtTryBlockEnds.pop();
373
+ _restoreScopedImports(node.end);
352
374
  },
353
375
  CatchClause(node) {
354
376
  if (node.param?.type === 'Identifier' && node.body) {
@@ -357,12 +379,14 @@ const coreVisitorObject = {
357
379
  },
358
380
  VariableDeclaration(node) {
359
381
  state.currentVarDeclStart = node.start;
382
+ for (const decl of node.declarations)
383
+ handleVariableDeclarator(decl, node.kind === 'var', state);
360
384
  if (state.scopeDepth > 0) {
385
+ const i = state.scopeDepth - 1;
361
386
  for (const decl of node.declarations) {
362
- if (decl.id.type === 'Identifier') {
387
+ if (decl.id.type === 'Identifier')
363
388
  state.localDeclarationTypes.set(decl.id.name, SYMBOL_TYPE.VARIABLE);
364
- _addShadow(decl.id.name);
365
- }
389
+ _collectBindingNames(decl.id, [state.scopeStarts[i], state.scopeEnds[i]]);
366
390
  }
367
391
  }
368
392
  else {
@@ -391,7 +415,6 @@ const coreVisitorObject = {
391
415
  handleExpressionStatement(node, state);
392
416
  },
393
417
  VariableDeclarator(node) {
394
- handleVariableDeclarator(node, state);
395
418
  trackCustomElementRegistry(node, state);
396
419
  },
397
420
  ImportExpression(node) {
@@ -419,8 +442,8 @@ const coreVisitorObject = {
419
442
  }
420
443
  },
421
444
  CallExpression(node) {
422
- handleImportThen(node, state);
423
445
  handleCallExpression(node, state);
446
+ handleImportThen(node, state);
424
447
  },
425
448
  NewExpression(node) {
426
449
  handleNewExpression(node, state);
@@ -432,10 +455,7 @@ const coreVisitorObject = {
432
455
  handleJSXMemberExpression(node, state);
433
456
  },
434
457
  ForInStatement(node) {
435
- if (node.left.type === 'VariableDeclaration' && node.body) {
436
- for (const decl of node.left.declarations)
437
- _collectBindingNames(decl.id, [node.body.start, node.body.end]);
438
- }
458
+ _enterScope(node);
439
459
  if (node.right.type === 'Identifier' && !isShadowed(node.right.name, node.right.start)) {
440
460
  const _import = state.localImportMap.get(node.right.name);
441
461
  if (_import?.isNamespace) {
@@ -445,11 +465,11 @@ const coreVisitorObject = {
445
465
  }
446
466
  }
447
467
  },
468
+ 'ForInStatement:exit'(node) {
469
+ _exitScope(node);
470
+ },
448
471
  ForOfStatement(node) {
449
- if (node.left.type === 'VariableDeclaration' && node.body) {
450
- for (const decl of node.left.declarations)
451
- _collectBindingNames(decl.id, [node.body.start, node.body.end]);
452
- }
472
+ _enterScope(node);
453
473
  if (node.right.type === 'Identifier' && !isShadowed(node.right.name, node.right.start)) {
454
474
  const _import = state.localImportMap.get(node.right.name);
455
475
  if (_import?.isNamespace) {
@@ -459,6 +479,9 @@ const coreVisitorObject = {
459
479
  }
460
480
  }
461
481
  },
482
+ 'ForOfStatement:exit'(node) {
483
+ _exitScope(node);
484
+ },
462
485
  ArrayExpression(node) {
463
486
  for (const element of node.elements ?? []) {
464
487
  if (element?.type !== 'Identifier')
@@ -471,6 +494,18 @@ const coreVisitorObject = {
471
494
  }
472
495
  }
473
496
  },
497
+ ReturnStatement(node) {
498
+ if (node.argument?.type === 'Identifier')
499
+ markDynamicNamespaceOpaque(node.argument.name, node.argument.start, state);
500
+ },
501
+ AssignmentExpression(node) {
502
+ if (node.right?.type === 'Identifier')
503
+ markDynamicNamespaceOpaque(node.right.name, node.right.start, state);
504
+ },
505
+ SpreadElement(node) {
506
+ if (node.argument?.type === 'Identifier')
507
+ markDynamicNamespaceOpaque(node.argument.name, node.argument.start, state);
508
+ },
474
509
  TSQualifiedName(node) {
475
510
  let left = node;
476
511
  const parts = [];
@@ -865,6 +900,7 @@ function walkAST(program, sourceText, filePath, hasModuleSyntax, ctx) {
865
900
  scopeStarts: [],
866
901
  scopeEnds: [],
867
902
  shadowScopes: new Map(),
903
+ scopedImports: [],
868
904
  localDeclarations: new Map(),
869
905
  localInterfaces: new Map(),
870
906
  pendingCallRefs: [],
@@ -929,26 +965,20 @@ function walkAST(program, sourceText, filePath, hasModuleSyntax, ctx) {
929
965
  for (const [aliasName, aliasSet] of state.importAliases) {
930
966
  if (!state.accessedAliases.has(aliasName)) {
931
967
  for (const alias of aliasSet) {
932
- const _import = state.localImportMap.get(alias.id);
933
- if (_import?.isNamespace) {
934
- const internalImport = state.internal.get(_import.filePath);
935
- if (internalImport) {
968
+ if (alias.import.isNamespace) {
969
+ const internalImport = state.internal.get(alias.import.filePath);
970
+ if (internalImport)
936
971
  addValue(internalImport.import, OPAQUE, filePath);
937
- }
938
972
  }
939
973
  }
940
974
  }
941
975
  }
942
976
  for (const [containerName, propMap] of state.nsContainers) {
943
- for (const [propKey, nsName] of propMap) {
977
+ for (const [propKey, ref] of propMap) {
944
978
  if (!state.accessedNsContainers.has(`${containerName}.${propKey}`)) {
945
- const _import = state.localImportMap.get(nsName);
946
- if (_import) {
947
- const internalImport = state.internal.get(_import.filePath);
948
- if (internalImport) {
949
- addValue(internalImport.import, OPAQUE, filePath);
950
- }
951
- }
979
+ const internalImport = state.internal.get(ref.import.filePath);
980
+ if (internalImport)
981
+ addValue(internalImport.import, OPAQUE, filePath);
952
982
  }
953
983
  }
954
984
  }