knip 5.36.7 → 5.37.1

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.
@@ -10,6 +10,7 @@ const commands = [
10
10
  'dedupe',
11
11
  'dlx',
12
12
  'explain',
13
+ 'global',
13
14
  'info',
14
15
  'init',
15
16
  'install',
@@ -1,4 +1,5 @@
1
1
  import type { SyncCompilerFn } from './types.js';
2
- export declare const importMatcher: RegExp;
3
2
  export declare const fencedCodeBlockMatcher: RegExp;
3
+ export declare const importMatcher: RegExp;
4
4
  export declare const importsWithinScripts: SyncCompilerFn;
5
+ export declare const tsScriptBodies: SyncCompilerFn;
@@ -1,6 +1,6 @@
1
+ export const fencedCodeBlockMatcher = /```[\s\S]*?```/g;
1
2
  const scriptExtractor = /<script\b[^>]*>([\s\S]*?)<\/script>/gm;
2
3
  export const importMatcher = /import[^'"]+['"]([^'"]+)['"]/g;
3
- export const fencedCodeBlockMatcher = /```[\s\S]*?```/g;
4
4
  export const importsWithinScripts = (text) => {
5
5
  const scripts = [];
6
6
  let scriptMatch;
@@ -11,3 +11,13 @@ export const importsWithinScripts = (text) => {
11
11
  }
12
12
  return scripts.join(';\n');
13
13
  };
14
+ const tsScriptExtractor = /<script\b[^>]*lang="ts"[^>]*>(?<body>[\s\S]*?)<\/script>/gm;
15
+ export const tsScriptBodies = (text) => {
16
+ const scripts = [];
17
+ let scriptMatch;
18
+ while ((scriptMatch = tsScriptExtractor.exec(text))) {
19
+ if (scriptMatch.groups?.body)
20
+ scripts.push(scriptMatch.groups.body);
21
+ }
22
+ return scripts.join(';\n');
23
+ };
@@ -1,4 +1,4 @@
1
- import { importsWithinScripts } from './compilers.js';
1
+ import { tsScriptBodies } from './compilers.js';
2
2
  const condition = (hasDependency) => hasDependency('vue') || hasDependency('nuxt');
3
- const compiler = importsWithinScripts;
3
+ const compiler = tsScriptBodies;
4
4
  export default { condition, compiler };
@@ -0,0 +1,4 @@
1
+ import type { PluginOptions } from '../../types/config.js';
2
+ import type { JestInitialOptions } from './types.js';
3
+ export declare const resolveExtensibleConfig: (configFilePath: string) => Promise<any>;
4
+ export declare const getReportersDependencies: (config: JestInitialOptions, options: PluginOptions) => string[];
@@ -0,0 +1,39 @@
1
+ import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
2
+ import { load } from '../../util/plugin.js';
3
+ export const resolveExtensibleConfig = async (configFilePath) => {
4
+ let config = await load(configFilePath);
5
+ if (config?.preset) {
6
+ const { preset } = config;
7
+ if (isInternal(preset)) {
8
+ const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
9
+ const presetConfig = await resolveExtensibleConfig(presetConfigPath);
10
+ config = Object.assign({}, presetConfig, config);
11
+ }
12
+ }
13
+ return config;
14
+ };
15
+ const getStringPropOrFallback = (prop, fallback) => {
16
+ return typeof prop === 'string' ? prop : fallback;
17
+ };
18
+ export const getReportersDependencies = (config, options) => {
19
+ const jUnitReporterDeps = [];
20
+ for (const reporter of config.reporters ?? []) {
21
+ if (typeof reporter !== 'string' && reporter[0] === 'jest-junit') {
22
+ const { testCasePropertiesFile, testCasePropertiesDirectory, testSuitePropertiesFile, testSuitePropertiesDirectory, } = reporter[1];
23
+ const testCaseFileName = getStringPropOrFallback(testCasePropertiesFile, 'junitProperties.js');
24
+ const testCaseDirectory = getStringPropOrFallback(testCasePropertiesDirectory, options.rootCwd);
25
+ const testCaseFilePath = join(testCaseDirectory, testCaseFileName);
26
+ const testSuiteFileName = getStringPropOrFallback(testSuitePropertiesFile, 'junitTestCaseProperties.js');
27
+ const testSuiteDirectory = getStringPropOrFallback(testSuitePropertiesDirectory, options.rootCwd);
28
+ const testSuiteFilePath = join(testSuiteDirectory, testSuiteFileName);
29
+ jUnitReporterDeps.push(testCaseFilePath);
30
+ jUnitReporterDeps.push(testSuiteFilePath);
31
+ }
32
+ }
33
+ const reporters = config.reporters
34
+ ? config.reporters
35
+ .map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
36
+ .filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
37
+ : [];
38
+ return [...reporters, ...jUnitReporterDeps];
39
+ };
@@ -1,23 +1,12 @@
1
1
  import { toDeferResolve, toEntry } from '../../util/input.js';
2
- import { dirname, isInternal, join, toAbsolute } from '../../util/path.js';
3
- import { hasDependency, load } from '../../util/plugin.js';
2
+ import { isInternal, join, toAbsolute } from '../../util/path.js';
3
+ import { hasDependency } from '../../util/plugin.js';
4
+ import { getReportersDependencies, resolveExtensibleConfig } from './helpers.js';
4
5
  const title = 'Jest';
5
6
  const enablers = ['jest'];
6
7
  const isEnabled = ({ dependencies, manifest }) => hasDependency(dependencies, enablers) || Boolean(manifest.name?.startsWith('jest-presets'));
7
8
  const config = ['jest.config.{js,ts,mjs,cjs,json}', 'package.json'];
8
9
  const entry = ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'];
9
- const resolveExtensibleConfig = async (configFilePath) => {
10
- let config = await load(configFilePath);
11
- if (config?.preset) {
12
- const { preset } = config;
13
- if (isInternal(preset)) {
14
- const presetConfigPath = toAbsolute(preset, dirname(configFilePath));
15
- const presetConfig = await resolveExtensibleConfig(presetConfigPath);
16
- config = Object.assign({}, presetConfig, config);
17
- }
18
- }
19
- return config;
20
- };
21
10
  const resolveDependencies = async (config, options) => {
22
11
  const { configFileDir } = options;
23
12
  if (config?.preset) {
@@ -42,13 +31,13 @@ const resolveDependencies = async (config, options) => {
42
31
  }
43
32
  const runner = config.runner ? [config.runner] : [];
44
33
  const runtime = config.runtime && config.runtime !== 'jest-circus' ? [config.runtime] : [];
45
- const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
34
+ const environments = config.testEnvironment === 'jsdom'
35
+ ? ['jest-environment-jsdom']
36
+ : config.testEnvironment
37
+ ? [config.testEnvironment]
38
+ : [];
46
39
  const resolvers = config.resolver ? [config.resolver] : [];
47
- const reporters = config.reporters
48
- ? config.reporters
49
- .map(reporter => (typeof reporter === 'string' ? reporter : reporter[0]))
50
- .filter(reporter => !['default', 'github-actions', 'summary'].includes(reporter))
51
- : [];
40
+ const reporters = getReportersDependencies(config, options);
52
41
  const watchPlugins = config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
53
42
  const transform = config.transform
54
43
  ? Object.values(config.transform).map(transform => (typeof transform === 'string' ? transform : transform[0]))
@@ -89,7 +78,7 @@ const resolveEntryPaths = async (localConfig, options) => {
89
78
  const { configFileDir } = options;
90
79
  if (typeof localConfig === 'function')
91
80
  localConfig = await localConfig();
92
- const rootDir = localConfig.rootDir ? join(configFileDir, localConfig.rootDir) : configFileDir;
81
+ const rootDir = localConfig.rootDir ?? configFileDir;
93
82
  const replaceRootDir = (name) => name.replace(/<rootDir>/, rootDir);
94
83
  return (localConfig.testMatch ?? []).map(replaceRootDir).map(toEntry);
95
84
  };
@@ -97,7 +86,7 @@ const resolveConfig = async (localConfig, options) => {
97
86
  const { configFileDir } = options;
98
87
  if (typeof localConfig === 'function')
99
88
  localConfig = await localConfig();
100
- const rootDir = localConfig.rootDir ? join(configFileDir, localConfig.rootDir) : configFileDir;
89
+ const rootDir = localConfig.rootDir ?? configFileDir;
101
90
  const replaceRootDir = (name) => name.replace(/<rootDir>/, rootDir);
102
91
  const inputs = await resolveDependencies(localConfig, options);
103
92
  const result = inputs.map(dependency => {
@@ -1,10 +1,10 @@
1
1
  import { isBuiltin } from 'node:module';
2
2
  import ts from 'typescript';
3
- import { ALIAS_TAG, ANONYMOUS, DEFAULT_EXTENSIONS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
3
+ import { ALIAS_TAG, ANONYMOUS, IMPORT_STAR, PROTOCOL_VIRTUAL } from '../constants.js';
4
4
  import { timerify } from '../util/Performance.js';
5
5
  import { addNsValue, addValue, createImports } from '../util/dependency-graph.js';
6
6
  import { getPackageNameFromFilePath, isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
7
- import { extname, isInNodeModules } from '../util/path.js';
7
+ import { isInNodeModules } from '../util/path.js';
8
8
  import { shouldIgnore } from '../util/tag.js';
9
9
  import { getAccessMembers, getDestructuredIds, getJSDocTags, getLineAndCharacterOfPosition, getTypeName, isAccessExpression, isConsiderReferencedNS, isDestructuring, isImportSpecifier, isObjectEnumerationCallExpressionArgument, isReferencedInExport, } from './ast-helpers.js';
10
10
  import { findInternalReferences, isType } from './find-internal-references.js';
@@ -108,9 +108,8 @@ const getImportsAndExports = (sourceFile, resolveModule, typeChecker, options) =
108
108
  else if (identifier !== ANONYMOUS && identifier !== IMPORT_STAR) {
109
109
  addValue(imports.imported, identifier, sourceFile.fileName);
110
110
  }
111
- if (symbol && DEFAULT_EXTENSIONS.includes(extname(sourceFile.fileName))) {
111
+ if (symbol)
112
112
  importedInternalSymbols.set(symbol, filePath);
113
- }
114
113
  }
115
114
  };
116
115
  const addImport = (options, node) => {
@@ -3,8 +3,9 @@ import picocolors from 'picocolors';
3
3
  import parsedArgValues from './cli-arguments.js';
4
4
  const { debug } = parsedArgValues;
5
5
  const IS_DEBUG_ENABLED = debug ?? false;
6
+ const IS_COLORS = !process.env.NO_COLOR;
6
7
  const noop = () => { };
7
- const inspectOptions = { maxArrayLength: null, depth: null, colors: true };
8
+ const inspectOptions = { maxArrayLength: null, depth: null, colors: IS_COLORS };
8
9
  export const inspect = (obj) => console.log(util.inspect(obj, inspectOptions));
9
10
  const ctx = (text) => typeof text === 'string'
10
11
  ? picocolors.yellow(`[${text}]`)
@@ -56,7 +56,8 @@ export const getWatchHandler = async ({ analyzedFiles, analyzeSourceFile, chief,
56
56
  else {
57
57
  graph.delete(filePath);
58
58
  analyzedFiles.delete(filePath);
59
- cachedUnusedFiles.add(filePath);
59
+ if (filePath.startsWith(cwd))
60
+ cachedUnusedFiles.add(filePath);
60
61
  }
61
62
  }
62
63
  for (const filePath of filePaths)
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "5.36.7";
1
+ export declare const version = "5.37.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '5.36.7';
1
+ export const version = '5.37.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "5.36.7",
3
+ "version": "5.37.1",
4
4
  "description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
5
5
  "homepage": "https://knip.dev",
6
6
  "repository": {