knip 2.41.6 → 2.43.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.
@@ -4,6 +4,7 @@ import { SourceFileManager } from './typescript/SourceFileManager.js';
4
4
  import type { PrincipalOptions } from './PrincipalFactory.js';
5
5
  import type { SyncCompilers, AsyncCompilers } from './types/compilers.js';
6
6
  import type { ExportItem, ExportItemMember } from './types/exports.js';
7
+ import type { ProgramMaybe53 } from './typescript/SourceFile.js';
7
8
  import type { GlobbyFilterFunction } from 'globby';
8
9
  export declare class ProjectPrincipal {
9
10
  entryPaths: Set<string>;
@@ -20,7 +21,7 @@ export declare class ProjectPrincipal {
20
21
  compilerHost: ts.CompilerHost;
21
22
  resolveModuleNames: ReturnType<typeof createCustomModuleResolver>;
22
23
  lsFindReferences: ts.LanguageService['findReferences'];
23
- program?: ts.Program;
24
+ program?: ProgramMaybe53;
24
25
  };
25
26
  constructor({ compilerOptions, cwd, compilers, isGitIgnored }: PrincipalOptions);
26
27
  private createProgram;
@@ -111,7 +111,13 @@ export class ProjectPrincipal {
111
111
  if (!sourceFile)
112
112
  throw new Error(`Unable to find ${filePath}`);
113
113
  const skipExports = this.skipExportsAnalysis.has(filePath);
114
- const { imports, exports, scripts } = getImportsAndExports(sourceFile, { skipTypeOnly, skipExports });
114
+ const getResolvedModule = specifier => this.backend.program?.getResolvedModule
115
+ ? this.backend.program.getResolvedModule(sourceFile, specifier, undefined)
116
+ : sourceFile.resolvedModules?.get(specifier, undefined);
117
+ const { imports, exports, scripts } = getImportsAndExports(sourceFile, getResolvedModule, {
118
+ skipTypeOnly,
119
+ skipExports,
120
+ });
115
121
  const { internal, unresolved, external } = imports;
116
122
  const unresolvedImports = new Set();
117
123
  unresolved.forEach(specifier => {
@@ -1,5 +1,4 @@
1
- export declare const resolvePluginName: (pluginName: string) => string;
2
- export declare const resolvePresetName: (pluginName: string) => string;
1
+ export declare const resolveName: (identifier: string, namespace: 'preset' | 'plugin') => string;
3
2
  export declare const api: {
4
3
  assertVersion: () => boolean;
5
4
  cache: {
@@ -1,24 +1,32 @@
1
- export const resolvePluginName = (pluginName) => {
2
- if (!/babel/.test(pluginName))
3
- return `babel-plugin-${pluginName}`;
4
- if (pluginName.startsWith('@babel/')) {
5
- if (pluginName.startsWith('@babel/plugin'))
6
- return pluginName;
7
- const [, name] = pluginName.split('/');
8
- return `@babel/plugin-${name}`;
1
+ import { isAbsolute, isInternal } from '../../util/path.js';
2
+ export const resolveName = (identifier, namespace) => {
3
+ if (isAbsolute(identifier) || isInternal(identifier))
4
+ return identifier;
5
+ if (identifier.startsWith('module:'))
6
+ return identifier.replace(/^module:/, '');
7
+ if (identifier.startsWith('@')) {
8
+ const [scope, name, ...rest] = identifier.split('/');
9
+ if (rest.length > 0)
10
+ return identifier;
11
+ if (scope) {
12
+ if (!name)
13
+ return [scope, `babel-${namespace}`].join('/');
14
+ if (scope === '@babel') {
15
+ if (name.startsWith(namespace))
16
+ return identifier;
17
+ return `@babel/${namespace}-${name}`;
18
+ }
19
+ if (name.includes(`babel-${namespace}`))
20
+ return identifier;
21
+ return [scope, `babel-${namespace}-${name}`].join('/');
22
+ }
9
23
  }
10
- return pluginName;
11
- };
12
- export const resolvePresetName = (pluginName) => {
13
- if (!/babel/.test(pluginName))
14
- return `babel-preset-${pluginName}`;
15
- if (pluginName.startsWith('@babel/')) {
16
- if (pluginName.startsWith('@babel/preset'))
17
- return pluginName;
18
- const [, name] = pluginName.split('/');
19
- return `@babel/preset-${name}`;
20
- }
21
- return pluginName;
24
+ const [name, ...rest] = identifier.split('/');
25
+ if (rest.length > 0)
26
+ return identifier;
27
+ if (name.startsWith(`babel-${namespace}`))
28
+ return identifier;
29
+ return `babel-${namespace}-${name}`;
22
30
  };
23
31
  const cacheFn = () => void 0;
24
32
  cacheFn.forever = () => cacheFn;
@@ -1,22 +1,20 @@
1
1
  import { compact } from '../../util/array.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
3
  import { hasDependency, load } from '../../util/plugin.js';
4
- import { resolvePresetName, resolvePluginName, api } from './helpers.js';
4
+ import { resolveName, api } from './helpers.js';
5
5
  export const NAME = 'Babel';
6
6
  export const ENABLERS = [/^@babel\//];
7
7
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
8
8
  export const CONFIG_FILE_PATTERNS = [
9
- 'babel.config.json',
10
- 'babel.config.js',
11
- '.babelrc.json',
12
- '.babelrc.js',
9
+ 'babel.config.{json,js,cjs,mjs,cts}',
10
+ '.babelrc.{json,js,cjs,mjs,cts}',
13
11
  '.babelrc',
14
12
  'package.json',
15
13
  ];
16
- const getItemName = (value) => typeof value === 'string' ? [value] : Array.isArray(value) ? [value[0]] : [];
14
+ const getName = (value) => typeof value === 'string' ? [value] : Array.isArray(value) ? [value[0]] : [];
17
15
  export const getDependenciesFromConfig = (config) => {
18
- const presets = config.presets?.flatMap(getItemName).map(resolvePresetName) ?? [];
19
- const plugins = config.plugins?.flatMap(getItemName).map(resolvePluginName) ?? [];
16
+ const presets = config.presets?.flatMap(getName).map(name => resolveName(name, 'preset')) ?? [];
17
+ const plugins = config.plugins?.flatMap(getName).map(name => resolveName(name, 'plugin')) ?? [];
20
18
  const nested = config.env ? Object.values(config.env).flatMap(getDependenciesFromConfig) : [];
21
19
  return compact([...presets, ...plugins, ...nested]);
22
20
  };
@@ -7,4 +7,4 @@ export var SymbolType;
7
7
  SymbolType["CLASS"] = "class";
8
8
  SymbolType["MEMBER"] = "member";
9
9
  SymbolType["UNKNOWN"] = "unknown";
10
- })(SymbolType || (SymbolType = {}));
10
+ })(SymbolType = SymbolType || (SymbolType = {}));
@@ -15,4 +15,8 @@ export interface BoundSourceFile extends ts.SourceFile {
15
15
  scriptKind?: ts.ScriptKind;
16
16
  pragmas?: Map<string, PragmaMap | PragmaMap[]>;
17
17
  }
18
+ export interface ProgramMaybe53 extends ts.Program {
19
+ getResolvedModule?: (f: ts.SourceFile, moduleName: string, mode: ts.ResolutionMode) => ts.ResolvedModuleWithFailedLookupLocations | undefined;
20
+ }
21
+ export type GetResolvedModule = (name: string) => ts.ResolvedModuleWithFailedLookupLocations | undefined;
18
22
  export {};
@@ -1,5 +1,5 @@
1
1
  import ts from 'typescript';
2
- import type { BoundSourceFile } from './SourceFile.js';
2
+ import type { BoundSourceFile, GetResolvedModule } from './SourceFile.js';
3
3
  import type { ExportItems as Exports, ExportItem } from '../types/exports.js';
4
4
  import type { Imports } from '../types/imports.js';
5
5
  export type GetImportsAndExportsOptions = {
@@ -15,7 +15,7 @@ export type AddImportOptions = {
15
15
  export type AddExportOptions = ExportItem & {
16
16
  identifier: string;
17
17
  };
18
- export declare const getImportsAndExports: (sourceFile: BoundSourceFile, options: GetImportsAndExportsOptions) => {
18
+ export declare const getImportsAndExports: (sourceFile: BoundSourceFile, getResolvedModule: GetResolvedModule, options: GetImportsAndExportsOptions) => {
19
19
  imports: {
20
20
  internal: Imports;
21
21
  external: Set<string>;
@@ -13,7 +13,7 @@ const getVisitors = (sourceFile) => ({
13
13
  import: getImportVisitors(sourceFile),
14
14
  script: getScriptVisitors(sourceFile),
15
15
  });
16
- export const getImportsAndExports = (sourceFile, options) => {
16
+ export const getImportsAndExports = (sourceFile, getResolvedModule, options) => {
17
17
  const internalImports = new Map();
18
18
  const externalImports = new Set();
19
19
  const unresolvedImports = new Set();
@@ -50,7 +50,7 @@ export const getImportsAndExports = (sourceFile, options) => {
50
50
  const { specifier, symbol, identifier = '__anonymous', isReExport = false } = options;
51
51
  if (isBuiltin(specifier))
52
52
  return;
53
- const module = sourceFile.resolvedModules?.get(specifier, undefined);
53
+ const module = getResolvedModule(specifier);
54
54
  if (module?.resolvedModule) {
55
55
  const filePath = module.resolvedModule.resolvedFileName;
56
56
  if (filePath) {
@@ -6,7 +6,12 @@ export default visit(() => true, node => {
6
6
  if (node.exportClause && ts.isNamedExports(node.exportClause)) {
7
7
  const type = node.isTypeOnly ? SymbolType.TYPE : SymbolType.UNKNOWN;
8
8
  return node.exportClause.elements.map(element => {
9
- return { node: element, identifier: element.name.getText(), type, pos: element.name.pos };
9
+ return {
10
+ node: element,
11
+ identifier: element.name.getText(),
12
+ type,
13
+ pos: element.name.flowNode?.node?.pos ?? element.name.pos,
14
+ };
10
15
  });
11
16
  }
12
17
  }
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.41.6";
1
+ export declare const version = "2.43.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.41.6';
1
+ export const version = '2.43.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.41.6",
3
+ "version": "2.43.0",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://github.com/webpro/knip",
6
6
  "repository": "github:webpro/knip",