knip 2.41.0 → 2.41.2

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,4 @@ type GetDependenciesDeep = (configFilePath: string, options: {
4
4
  manifest: PackageJsonWithPlugins;
5
5
  }, dependencies?: Set<string>) => Promise<Set<string>>;
6
6
  export declare const getDependenciesDeep: GetDependenciesDeep;
7
- export declare const resolvePluginSpecifier: (specifier: string) => string;
8
- export declare const resolveExtendSpecifier: (specifier: string) => string | undefined;
9
7
  export {};
@@ -1,4 +1,5 @@
1
1
  import { compact } from '../../util/array.js';
2
+ import { getPackageNameFromModuleSpecifier } from '../../util/modules.js';
2
3
  import { isInternal, dirname, toAbsolute } from '../../util/path.js';
3
4
  import { load } from '../../util/plugin.js';
4
5
  import { _resolve } from '../../util/require.js';
@@ -54,15 +55,17 @@ const resolveSpecifier = (namespace, rawSpecifier) => {
54
55
  const specifier = rawSpecifier.replace(/(^plugin:|:.+$)/, '');
55
56
  if (isQualifiedSpecifier(specifier))
56
57
  return specifier;
57
- if (!specifier.startsWith('@'))
58
- return `${namespace}-${specifier}`;
58
+ if (!specifier.startsWith('@')) {
59
+ const id = rawSpecifier.startsWith('plugin:') ? getPackageNameFromModuleSpecifier(specifier) : specifier;
60
+ return `${namespace}-${id}`;
61
+ }
59
62
  const [scope, name, ...rest] = specifier.split('/');
60
63
  if (rawSpecifier.startsWith('plugin:') && rest.length === 0)
61
- return [scope, namespace, name].join('/');
64
+ return [scope, namespace].join('/');
62
65
  return [scope, name ? `${namespace}-${name}` : namespace, ...rest].join('/');
63
66
  };
64
- export const resolvePluginSpecifier = (specifier) => resolveSpecifier('eslint-plugin', specifier);
65
- export const resolveExtendSpecifier = (specifier) => {
67
+ const resolvePluginSpecifier = (specifier) => resolveSpecifier('eslint-plugin', specifier);
68
+ const resolveExtendSpecifier = (specifier) => {
66
69
  if (isInternal(specifier))
67
70
  return;
68
71
  if (/^next(\/.+)?$/.test(specifier))
@@ -4,7 +4,7 @@ import { findVitestDependencies } from '../vitest/index.js';
4
4
  export const NAME = 'Vite';
5
5
  export const ENABLERS = ['vite'];
6
6
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
7
- export const CONFIG_FILE_PATTERNS = ['vite.config.{js,ts}'];
7
+ export const CONFIG_FILE_PATTERNS = ['vite.config.{js,mjs,ts,cjs,mts,cts}'];
8
8
  const findViteDependencies = async (configFilePath, options) => {
9
9
  const localConfig = await load(configFilePath);
10
10
  if (!localConfig)
@@ -1,5 +1,5 @@
1
1
  import { compact } from '../../util/array.js';
2
- import { dirname, join } from '../../util/path.js';
2
+ import { dirname, join, relative } from '../../util/path.js';
3
3
  import { timerify } from '../../util/Performance.js';
4
4
  import { hasDependency, load, tryResolve } from '../../util/plugin.js';
5
5
  import { toEntryPattern } from '../../util/protocols.js';
@@ -9,6 +9,13 @@ export const ENABLERS = ['vitest'];
9
9
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
10
10
  export const CONFIG_FILE_PATTERNS = ['vitest.config.ts', 'vitest.{workspace,projects}.{ts,js,json}'];
11
11
  export const ENTRY_FILE_PATTERNS = ['**/*.{test,spec}.?(c|m)[jt]s?(x)'];
12
+ const resolveEntry = (containingFilePath, specifier) => {
13
+ const dir = dirname(containingFilePath);
14
+ const resolvedPath = tryResolve(join(dir, specifier), containingFilePath);
15
+ if (resolvedPath)
16
+ return toEntryPattern(relative(dir, resolvedPath));
17
+ return specifier;
18
+ };
12
19
  const findConfigDependencies = (configFilePath, localConfig, options) => {
13
20
  const { isProduction, config } = options;
14
21
  const testConfig = localConfig.test;
@@ -18,9 +25,8 @@ const findConfigDependencies = (configFilePath, localConfig, options) => {
18
25
  const environments = testConfig.environment ? [getEnvPackageName(testConfig.environment)] : [];
19
26
  const reporters = getExternalReporters(testConfig.reporters);
20
27
  const coverage = testConfig.coverage ? [`@vitest/coverage-${testConfig.coverage.provider ?? 'v8'}`] : [];
21
- const toPath = (v) => tryResolve(join(dirname(configFilePath), v), configFilePath) ?? v;
22
- const setupFiles = (testConfig.setupFiles ? [testConfig.setupFiles].flat() : []).map(toPath);
23
- const globalSetup = (testConfig.globalSetup ? [testConfig.globalSetup].flat() : []).map(toPath);
28
+ const setupFiles = [testConfig.setupFiles ?? []].flat().map(v => resolveEntry(configFilePath, v));
29
+ const globalSetup = [testConfig.globalSetup ?? []].flat().map(v => resolveEntry(configFilePath, v));
24
30
  return [...entryPatterns, ...environments, ...reporters, ...coverage, ...setupFiles, ...globalSetup];
25
31
  };
26
32
  export const findVitestDependencies = async (configFilePath, localConfig, options) => {
package/dist/util/fs.d.ts CHANGED
@@ -5,3 +5,4 @@ export declare const loadJSON: (filePath: string) => Promise<any>;
5
5
  export declare const loadYAML: (filePath: string) => Promise<unknown>;
6
6
  export declare const parseJSON: (filePath: string, contents: string) => Promise<any>;
7
7
  export declare const parseYAML: (contents: string) => Promise<unknown>;
8
+ export declare function isTypeModule(path: string): boolean;
package/dist/util/fs.js CHANGED
@@ -1,9 +1,9 @@
1
- import { statSync } from 'node:fs';
1
+ import { readFileSync, statSync } from 'node:fs';
2
2
  import { readFile } from 'node:fs/promises';
3
3
  import yaml from 'js-yaml';
4
4
  import stripJsonComments from 'strip-json-comments';
5
5
  import { LoaderError } from './errors.js';
6
- import { join } from './path.js';
6
+ import { dirname, join } from './path.js';
7
7
  export const isFile = (filePath) => {
8
8
  const stat = statSync(filePath, { throwIfNoEntry: false });
9
9
  return stat !== undefined && stat.isFile();
@@ -40,3 +40,18 @@ export const parseJSON = async (filePath, contents) => {
40
40
  export const parseYAML = async (contents) => {
41
41
  return yaml.load(contents);
42
42
  };
43
+ export function isTypeModule(path) {
44
+ while (path && path !== '.' && path !== '/') {
45
+ path = dirname(path);
46
+ try {
47
+ const pkg = readFileSync(join(path, 'package.json'), 'utf-8');
48
+ try {
49
+ return JSON.parse(pkg).type === 'module';
50
+ }
51
+ catch { }
52
+ break;
53
+ }
54
+ catch { }
55
+ }
56
+ return false;
57
+ }
@@ -1,9 +1,10 @@
1
1
  import { pathToFileURL } from 'node:url';
2
2
  import { LoaderError } from './errors.js';
3
3
  import { loadJSON, loadYAML, loadFile, parseJSON, parseYAML } from './fs.js';
4
+ import { isTypeModule } from './fs.js';
4
5
  import { extname } from './path.js';
5
6
  import { timerify } from './Performance.js';
6
- import { jiti } from './register.js';
7
+ import { jitiCJS, jitiESM } from './register.js';
7
8
  export const FAKE_PATH = '__FAKE__';
8
9
  const load = async (filePath) => {
9
10
  if (filePath === FAKE_PATH)
@@ -20,12 +21,17 @@ const load = async (filePath) => {
20
21
  if (ext === '.yaml' || ext === '.yml') {
21
22
  return loadYAML(filePath);
22
23
  }
23
- if (ext === '.mjs') {
24
+ if (ext === '.mjs' || (ext === '.js' && isTypeModule(filePath))) {
24
25
  const fileUrl = pathToFileURL(filePath);
25
26
  const imported = await import(fileUrl.href);
26
27
  return imported.default ?? imported;
27
28
  }
28
- return await jiti(filePath);
29
+ if (isTypeModule(filePath)) {
30
+ return await jitiESM(filePath);
31
+ }
32
+ else {
33
+ return await jitiCJS(filePath);
34
+ }
29
35
  }
30
36
  catch (error) {
31
37
  throw new LoaderError(`Error loading ${filePath}`, { cause: error });
@@ -1 +1,2 @@
1
- export declare const jiti: any;
1
+ export declare const jitiCJS: any;
2
+ export declare const jitiESM: any;
@@ -1,14 +1,10 @@
1
- import module from 'node:module';
2
- import createJITI from 'jiti';
3
- import { IGNORED_FILE_EXTENSIONS, DEFAULT_EXTENSIONS } from '../constants.js';
4
- const _extensions = module.Module._extensions;
5
- export const jiti = createJITI(process.cwd(), { interopDefault: true, extensions: DEFAULT_EXTENSIONS });
6
- if (!('.ts' in _extensions)) {
7
- jiti.register();
8
- }
9
- const exportFilePath = (module, filePath) => {
10
- module.exports = filePath;
1
+ import createJITI, {} from 'jiti';
2
+ import { DEFAULT_EXTENSIONS } from '../constants.js';
3
+ const options = {
4
+ interopDefault: true,
5
+ extensions: DEFAULT_EXTENSIONS,
6
+ esmResolve: false,
11
7
  };
12
- IGNORED_FILE_EXTENSIONS.forEach(ext => {
13
- _extensions[ext] = exportFilePath;
14
- });
8
+ const createLoader = (options) => createJITI(process.cwd(), options);
9
+ export const jitiCJS = createLoader(options);
10
+ export const jitiESM = createLoader({ ...options, esmResolve: true });
@@ -5,11 +5,11 @@ import { debugLog } from './debug.js';
5
5
  import { getPackageNameFromModuleSpecifier } from './modules.js';
6
6
  import { cwd, toPosix, join } from './path.js';
7
7
  import { timerify } from './Performance.js';
8
- import { jiti } from './register.js';
8
+ import { jitiCJS } from './register.js';
9
9
  const createRequire = (path) => nodeCreateRequire(pathToFileURL(path ?? cwd));
10
10
  const require = createRequire();
11
11
  export const _require = timerify(require);
12
- const resolve = (specifier) => toPosix(jiti.resolve(specifier));
12
+ const resolve = (specifier) => toPosix(jitiCJS.resolve(specifier));
13
13
  const tryResolve = (specifier, from) => {
14
14
  try {
15
15
  return resolve(specifier);
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "2.41.0";
1
+ export declare const version = "2.41.2";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '2.41.0';
1
+ export const version = '2.41.2';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "2.41.0",
3
+ "version": "2.41.2",
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",