knip 3.5.0 → 3.5.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.
Files changed (38) hide show
  1. package/dist/ProjectPrincipal.js +2 -2
  2. package/dist/WorkspaceWorker.js +2 -2
  3. package/dist/constants.js +1 -0
  4. package/dist/plugins/_template/index.js +2 -4
  5. package/dist/plugins/astro/index.js +1 -1
  6. package/dist/plugins/ava/index.js +4 -6
  7. package/dist/plugins/babel/index.js +2 -3
  8. package/dist/plugins/commitizen/index.js +2 -3
  9. package/dist/plugins/commitlint/index.js +2 -3
  10. package/dist/plugins/eslint/helpers.js +2 -2
  11. package/dist/plugins/eslint/index.js +2 -1
  12. package/dist/plugins/github-actions/index.js +2 -3
  13. package/dist/plugins/graphql-codegen/index.js +2 -4
  14. package/dist/plugins/husky/index.js +2 -3
  15. package/dist/plugins/jest/index.js +4 -4
  16. package/dist/plugins/lefthook/index.js +5 -5
  17. package/dist/plugins/lint-staged/index.js +4 -6
  18. package/dist/plugins/mocha/index.js +2 -3
  19. package/dist/plugins/node-test-runner/index.js +1 -5
  20. package/dist/plugins/npm-package-json-lint/index.js +2 -3
  21. package/dist/plugins/nx/index.js +2 -3
  22. package/dist/plugins/postcss/index.js +2 -3
  23. package/dist/plugins/prettier/index.js +2 -3
  24. package/dist/plugins/release-it/index.js +4 -6
  25. package/dist/plugins/remark/index.js +2 -3
  26. package/dist/plugins/semantic-release/index.js +2 -3
  27. package/dist/plugins/stylelint/index.js +2 -4
  28. package/dist/plugins/typedoc/index.js +3 -2
  29. package/dist/typescript/getImportsAndExports.js +3 -2
  30. package/dist/util/modules.d.ts +1 -1
  31. package/dist/util/modules.js +2 -2
  32. package/dist/util/path.d.ts +1 -0
  33. package/dist/util/path.js +1 -0
  34. package/dist/util/plugin.d.ts +1 -0
  35. package/dist/util/plugin.js +1 -0
  36. package/dist/version.d.ts +1 -1
  37. package/dist/version.js +1 -1
  38. package/package.json +1 -1
@@ -7,7 +7,7 @@ import { getImportsAndExports } from './typescript/getImportsAndExports.js';
7
7
  import { createCustomModuleResolver } from './typescript/resolveModuleNames.js';
8
8
  import { SourceFileManager } from './typescript/SourceFileManager.js';
9
9
  import { compact } from './util/array.js';
10
- import { isMaybePackageName, sanitizeSpecifier } from './util/modules.js';
10
+ import { isStartsLikePackageName, sanitizeSpecifier } from './util/modules.js';
11
11
  import { dirname, extname, isInNodeModules, join } from './util/path.js';
12
12
  import { timerify } from './util/Performance.js';
13
13
  const baseCompilerOptions = {
@@ -145,7 +145,7 @@ export class ProjectPrincipal {
145
145
  }
146
146
  else {
147
147
  const sanitizedSpecifier = sanitizeSpecifier(specifier);
148
- if (isMaybePackageName(sanitizedSpecifier)) {
148
+ if (isStartsLikePackageName(sanitizedSpecifier)) {
149
149
  external.add(sanitizedSpecifier);
150
150
  }
151
151
  else {
@@ -4,7 +4,7 @@ import { debugLogArray, debugLogObject } from './util/debug.js';
4
4
  import { _pureGlob, negate, hasProductionSuffix, hasNoProductionSuffix, prependDirToPattern } from './util/glob.js';
5
5
  import { FAKE_PATH } from './util/loader.js';
6
6
  import { get, getKeysByValue } from './util/object.js';
7
- import { join, toPosix } from './util/path.js';
7
+ import { basename, join, toPosix } from './util/path.js';
8
8
  import { fromEntryPattern, fromProductionEntryPattern, isEntryPattern, isProductionEntryPattern, } from './util/protocols.js';
9
9
  const nullConfig = { config: null, entry: null, project: null };
10
10
  export class WorkspaceWorker {
@@ -183,7 +183,7 @@ export class WorkspaceWorker {
183
183
  continue;
184
184
  const patterns = this.getConfigurationFilePatterns(pluginName);
185
185
  const allConfigFilePaths = await _pureGlob({ patterns, cwd, ignore, gitignore: false });
186
- const configFilePaths = allConfigFilePaths.filter(filePath => !filePath.endsWith('package.json') ||
186
+ const configFilePaths = allConfigFilePaths.filter(filePath => basename(filePath) !== 'package.json' ||
187
187
  get(this.manifest, 'PACKAGE_JSON_PATH' in plugin ? plugin.PACKAGE_JSON_PATH : pluginName));
188
188
  debugLogArray([name, plugin.NAME], 'config file paths', configFilePaths);
189
189
  if (configFilePaths.length === 0)
package/dist/constants.js CHANGED
@@ -33,6 +33,7 @@ export const IGNORED_GLOBAL_BINARIES = [
33
33
  'test',
34
34
  'true',
35
35
  'yarn',
36
+ 'xargs',
36
37
  ];
37
38
  export const IGNORED_DEPENDENCIES = ['knip', 'typescript'];
38
39
  export const DUMMY_VIRTUAL_FILE_EXTENSIONS = new Set(['.html', '.jpeg', '.jpg', '.png', '.svg', '.webp']);
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  import { toEntryPattern, toProductionEntryPattern } from '../../util/protocols.js';
@@ -10,10 +11,7 @@ export const PRODUCTION_ENTRY_FILE_PATTERNS = [];
10
11
  export const PROJECT_FILE_PATTERNS = [];
11
12
  const findPluginDependencies = async (configFilePath, options) => {
12
13
  const { manifest, config, isProduction } = options;
13
- const localConfig = configFilePath.endsWith('package.json')
14
- ?
15
- manifest.plugin
16
- : await load(configFilePath);
14
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.plugin : await load(configFilePath);
17
15
  if (!localConfig)
18
16
  return [];
19
17
  const entryPatterns = (config?.entry ?? localConfig.entryPathsOrPatterns ?? ENTRY_FILE_PATTERNS).map(toEntryPattern);
@@ -12,7 +12,7 @@ export const findDependencies = async (configFilePath, options) => {
12
12
  : [...ENTRY_FILE_PATTERNS.map(toEntryPattern), ...PRODUCTION_ENTRY_FILE_PATTERNS.map(toProductionEntryPattern)];
13
13
  if (!isProduction &&
14
14
  manifest.scripts &&
15
- Object.values(manifest.scripts).some(script => /astro (--.+ )?check/.test(script))) {
15
+ Object.values(manifest.scripts).some(script => /(?<=^|\s)astro(\s|\s.+\s)check(?=\s|$)/.test(script))) {
16
16
  dependencies.push('@astrojs/check');
17
17
  }
18
18
  return dependencies;
@@ -1,6 +1,6 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
1
+ import { basename } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
- import { hasDependency, load } from '../../util/plugin.js';
3
+ import { getDependenciesFromScripts, hasDependency, load } from '../../util/plugin.js';
4
4
  import { toEntryPattern } from '../../util/protocols.js';
5
5
  export const NAME = 'Ava';
6
6
  export const ENABLERS = ['ava'];
@@ -20,9 +20,7 @@ export const ENTRY_FILE_PATTERNS = [
20
20
  ];
21
21
  const findAvaDependencies = async (configFilePath, options) => {
22
22
  const { cwd, manifest, isProduction, config } = options;
23
- let localConfig = configFilePath.endsWith('package.json')
24
- ? manifest.ava
25
- : await load(configFilePath);
23
+ let localConfig = basename(configFilePath) === 'package.json' ? manifest.ava : await load(configFilePath);
26
24
  if (typeof localConfig === 'function')
27
25
  localConfig = localConfig();
28
26
  const entryPatterns = (config.entry ?? localConfig?.files ?? ENTRY_FILE_PATTERNS).map(toEntryPattern);
@@ -31,7 +29,7 @@ const findAvaDependencies = async (configFilePath, options) => {
31
29
  const nodeArgs = localConfig.nodeArguments ?? [];
32
30
  const requireArgs = (localConfig.require ?? []).map(require => `--require ${require}`);
33
31
  const fakeCommand = `node ${nodeArgs.join(' ')} ${requireArgs.join(' ')}`;
34
- const dependencies = _getDependenciesFromScripts([fakeCommand], {
32
+ const dependencies = getDependenciesFromScripts([fakeCommand], {
35
33
  cwd,
36
34
  manifest,
37
35
  knownGlobalsOnly: true,
@@ -1,4 +1,5 @@
1
1
  import { compact } from '../../util/array.js';
2
+ import { basename } from '../../util/path.js';
2
3
  import { timerify } from '../../util/Performance.js';
3
4
  import { hasDependency, load } from '../../util/plugin.js';
4
5
  import { resolveName, api } from './helpers.js';
@@ -21,9 +22,7 @@ export const getDependenciesFromConfig = (config) => {
21
22
  const findBabelDependencies = async (configFilePath, { manifest, isProduction }) => {
22
23
  if (isProduction)
23
24
  return [];
24
- let localConfig = configFilePath.endsWith('package.json')
25
- ? manifest.babel
26
- : await load(configFilePath);
25
+ let localConfig = basename(configFilePath) === 'package.json' ? manifest.babel : await load(configFilePath);
27
26
  if (typeof localConfig === 'function')
28
27
  localConfig = localConfig(api);
29
28
  if (!localConfig)
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'Commitizen';
@@ -8,9 +9,7 @@ export const CONFIG_FILE_PATTERNS = ['.czrc', '.cz.json', 'package.json'];
8
9
  const findPluginDependencies = async (configFilePath, { manifest, isProduction }) => {
9
10
  if (isProduction)
10
11
  return [];
11
- const localConfig = configFilePath.endsWith('package.json')
12
- ? manifest.config?.commitizen
13
- : await load(configFilePath);
12
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.config?.commitizen : await load(configFilePath);
14
13
  if (!localConfig)
15
14
  return [];
16
15
  return localConfig.path ? [localConfig.path] : [];
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'commitlint';
@@ -12,9 +13,7 @@ export const CONFIG_FILE_PATTERNS = [
12
13
  const findCommitLintDependencies = async (configFilePath, { manifest, isProduction }) => {
13
14
  if (isProduction)
14
15
  return [];
15
- const localConfig = configFilePath.endsWith('package.json')
16
- ? manifest.commitlint
17
- : await load(configFilePath);
16
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.commitlint : await load(configFilePath);
18
17
  if (!localConfig)
19
18
  return [];
20
19
  return localConfig.extends ? [localConfig.extends].flat() : [];
@@ -1,6 +1,6 @@
1
1
  import { compact } from '../../util/array.js';
2
2
  import { getPackageNameFromModuleSpecifier } from '../../util/modules.js';
3
- import { isInternal, dirname, toAbsolute } from '../../util/path.js';
3
+ import { basename, isInternal, dirname, toAbsolute } from '../../util/path.js';
4
4
  import { load } from '../../util/plugin.js';
5
5
  import { _resolve } from '../../util/require.js';
6
6
  import { getDependenciesFromConfig } from '../babel/index.js';
@@ -21,7 +21,7 @@ const getDependencies = (config) => {
21
21
  };
22
22
  export const getDependenciesDeep = async (configFilePath, options, dependencies = new Set()) => {
23
23
  const addAll = (deps) => deps.forEach(dependency => dependencies.add(dependency));
24
- const localConfig = configFilePath.endsWith('package.json')
24
+ const localConfig = basename(configFilePath) === 'package.json'
25
25
  ? options.manifest[PACKAGE_JSON_PATH]
26
26
  : /(\.(jsonc?|ya?ml)|rc)$/.test(configFilePath)
27
27
  ? await load(configFilePath)
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency } from '../../util/plugin.js';
3
4
  import { getDependenciesDeep } from './helpers.js';
@@ -17,7 +18,7 @@ export const CONFIG_FILE_PATTERNS = [
17
18
  const findESLintDependencies = async (configFilePath, { cwd, manifest, isProduction }) => {
18
19
  if (isProduction)
19
20
  return [];
20
- if (configFilePath.endsWith('eslint.config.js'))
21
+ if (basename(configFilePath) === 'eslint.config.js')
21
22
  return [];
22
23
  const dependencies = await getDependenciesDeep(configFilePath, { cwd, manifest });
23
24
  return Array.from(dependencies);
@@ -1,8 +1,7 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
2
1
  import { _firstGlob } from '../../util/glob.js';
3
2
  import { getValuesByKeyDeep } from '../../util/object.js';
4
3
  import { timerify } from '../../util/Performance.js';
5
- import { load } from '../../util/plugin.js';
4
+ import { getDependenciesFromScripts, load } from '../../util/plugin.js';
6
5
  export const NAME = 'GitHub Actions';
7
6
  export const ENABLERS = 'This plugin is enabled when a `.yml` or `.yaml` file is found in the `.github/workflows` folder.';
8
7
  export const isEnabled = async ({ cwd }) => Boolean(await _firstGlob({ cwd, patterns: ['.github/workflows/*.{yml,yaml}'] }));
@@ -15,7 +14,7 @@ const findGithubActionsDependencies = async (configFilePath, options) => {
15
14
  if (!config)
16
15
  return [];
17
16
  const scripts = getValuesByKeyDeep(config, 'run').filter((value) => typeof value === 'string');
18
- return _getDependenciesFromScripts(scripts, {
17
+ return getDependenciesFromScripts(scripts, {
19
18
  cwd,
20
19
  manifest,
21
20
  knownGlobalsOnly: true,
@@ -1,4 +1,4 @@
1
- import { isInternal } from '../../util/path.js';
1
+ import { basename, isInternal } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
3
  import { hasDependency, load } from '../../util/plugin.js';
4
4
  import { toEntryPattern } from '../../util/protocols.js';
@@ -17,9 +17,7 @@ const findPluginDependencies = async (configFilePath, options) => {
17
17
  const { manifest, isProduction } = options;
18
18
  if (isProduction)
19
19
  return [];
20
- const localConfig = configFilePath.endsWith('package.json')
21
- ? manifest[PACKAGE_JSON_PATH]
22
- : await load(configFilePath);
20
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest[PACKAGE_JSON_PATH] : await load(configFilePath);
23
21
  if (!localConfig)
24
22
  return [];
25
23
  const generateSet = Object.values(localConfig.generates);
@@ -1,8 +1,7 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
2
1
  import { getGitHookPaths } from '../../util/git.js';
3
2
  import { FAKE_PATH } from '../../util/loader.js';
4
3
  import { timerify } from '../../util/Performance.js';
5
- import { hasDependency, loadFile } from '../../util/plugin.js';
4
+ import { getDependenciesFromScripts, hasDependency, loadFile } from '../../util/plugin.js';
6
5
  export const NAME = 'husky';
7
6
  export const ENABLERS = ['husky'];
8
7
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
@@ -15,7 +14,7 @@ const findHuskyDependencies = async (configFilePath, options) => {
15
14
  const script = await loadFile(configFilePath);
16
15
  if (!script)
17
16
  return [];
18
- return _getDependenciesFromScripts(String(script), {
17
+ return getDependenciesFromScripts(String(script), {
19
18
  cwd,
20
19
  manifest,
21
20
  knownGlobalsOnly: true,
@@ -1,4 +1,4 @@
1
- import { join, isInternal, toAbsolute, dirname } from '../../util/path.js';
1
+ import { basename, join, isInternal, toAbsolute, dirname } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
3
  import { hasDependency, load } from '../../util/plugin.js';
4
4
  import { toEntryPattern } from '../../util/protocols.js';
@@ -46,6 +46,7 @@ const resolveDependencies = (config, options) => {
46
46
  ? Object.values(config.moduleNameMapper).map(mapper => (typeof mapper === 'string' ? mapper : mapper[0]))
47
47
  : []).filter(value => !/\$[0-9]/.test(value));
48
48
  const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];
49
+ const snapshotResolver = config.snapshotResolver ? [config.snapshotResolver] : [];
49
50
  return [
50
51
  ...entryPatterns,
51
52
  ...presets,
@@ -60,13 +61,12 @@ const resolveDependencies = (config, options) => {
60
61
  ...transform,
61
62
  ...moduleNameMapper,
62
63
  ...testResultsProcessor,
64
+ ...snapshotResolver,
63
65
  ];
64
66
  };
65
67
  const findJestDependencies = async (configFilePath, options) => {
66
68
  const { manifest, cwd } = options;
67
- let localConfig = configFilePath.endsWith('package.json')
68
- ? manifest.jest
69
- : await resolveExtensibleConfig(configFilePath);
69
+ let localConfig = basename(configFilePath) === 'package.json' ? manifest.jest : await resolveExtensibleConfig(configFilePath);
70
70
  if (typeof localConfig === 'function')
71
71
  localConfig = await localConfig();
72
72
  if (!localConfig)
@@ -1,8 +1,8 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
2
1
  import { getGitHookPaths } from '../../util/git.js';
3
2
  import { getValuesByKeyDeep } from '../../util/object.js';
3
+ import { extname } from '../../util/path.js';
4
4
  import { timerify } from '../../util/Performance.js';
5
- import { hasDependency, load, loadFile } from '../../util/plugin.js';
5
+ import { getDependenciesFromScripts, hasDependency, load, loadFile } from '../../util/plugin.js';
6
6
  import { fromBinary } from '../../util/protocols.js';
7
7
  export const NAME = 'Lefthook';
8
8
  export const ENABLERS = ['lefthook', '@arkweid/lefthook', '@evilmartians/lefthook'];
@@ -14,18 +14,18 @@ const findLefthookDependencies = async (configFilePath, options) => {
14
14
  if (isProduction)
15
15
  return [];
16
16
  const dependencies = manifest.devDependencies ? Object.keys(manifest.devDependencies) : [];
17
- if (configFilePath.endsWith('.yml')) {
17
+ if (extname(configFilePath) === '.yml') {
18
18
  const localConfig = await load(configFilePath);
19
19
  if (!localConfig)
20
20
  return [];
21
21
  const scripts = getValuesByKeyDeep(localConfig, 'run').filter((run) => typeof run === 'string');
22
22
  const lefthook = process.env.CI ? ENABLERS.filter(dependency => dependencies.includes(dependency)) : [];
23
- return [...lefthook, ..._getDependenciesFromScripts(scripts, { cwd, manifest, knownGlobalsOnly: true })];
23
+ return [...lefthook, ...getDependenciesFromScripts(scripts, { cwd, manifest, knownGlobalsOnly: true })];
24
24
  }
25
25
  const script = await loadFile(configFilePath);
26
26
  if (!script)
27
27
  return [];
28
- const scriptDependencies = _getDependenciesFromScripts([script], { cwd, manifest, knownGlobalsOnly: false });
28
+ const scriptDependencies = getDependenciesFromScripts([script], { cwd, manifest, knownGlobalsOnly: false });
29
29
  const matches = scriptDependencies.find(dep => dependencies.includes(fromBinary(dep)));
30
30
  return matches ? [matches] : [];
31
31
  };
@@ -1,6 +1,6 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
1
+ import { basename } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
- import { hasDependency, load } from '../../util/plugin.js';
3
+ import { getDependenciesFromScripts, hasDependency, load } from '../../util/plugin.js';
4
4
  export const NAME = 'lint-staged';
5
5
  export const ENABLERS = ['lint-staged'];
6
6
  export const PACKAGE_JSON_PATH = 'lint-staged';
@@ -17,9 +17,7 @@ const findLintStagedDependencies = async (configFilePath, options) => {
17
17
  const { cwd, manifest, isProduction } = options;
18
18
  if (isProduction)
19
19
  return [];
20
- let localConfig = configFilePath.endsWith('package.json')
21
- ? manifest['lint-staged']
22
- : await load(configFilePath);
20
+ let localConfig = basename(configFilePath) === 'package.json' ? manifest['lint-staged'] : await load(configFilePath);
23
21
  if (typeof localConfig === 'function')
24
22
  localConfig = localConfig();
25
23
  if (!localConfig)
@@ -28,7 +26,7 @@ const findLintStagedDependencies = async (configFilePath, options) => {
28
26
  for (const entry of Object.values(localConfig).flat()) {
29
27
  const scripts = [typeof entry === 'function' ? await entry([]) : entry].flat();
30
28
  const options = { cwd, manifest };
31
- _getDependenciesFromScripts(scripts, options).forEach(identifier => dependencies.add(identifier));
29
+ getDependenciesFromScripts(scripts, options).forEach(identifier => dependencies.add(identifier));
32
30
  }
33
31
  return Array.from(dependencies);
34
32
  };
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  import { toEntryPattern } from '../../util/protocols.js';
@@ -8,9 +9,7 @@ export const CONFIG_FILE_PATTERNS = ['.mocharc.{js,cjs,json,jsonc,yml,yaml}', 'p
8
9
  export const ENTRY_FILE_PATTERNS = ['**/test/*.{js,cjs,mjs}'];
9
10
  const findMochaDependencies = async (configFilePath, options) => {
10
11
  const { config, manifest, isProduction } = options;
11
- const localConfig = configFilePath.endsWith('package.json')
12
- ? manifest.mocha
13
- : await load(configFilePath);
12
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.mocha : await load(configFilePath);
14
13
  const entryPatterns = (config.entry ?? (localConfig?.spec ? [localConfig.spec].flat() : ENTRY_FILE_PATTERNS)).map(toEntryPattern);
15
14
  if (isProduction || !localConfig)
16
15
  return entryPatterns;
@@ -2,11 +2,7 @@ import { timerify } from '../../util/Performance.js';
2
2
  import { toEntryPattern } from '../../util/protocols.js';
3
3
  export const NAME = 'Node.js Test Runner';
4
4
  export const ENABLERS = 'This plugin is enabled when any script in `package.json` includes `node --test`';
5
- export const isEnabled = ({ manifest }) => {
6
- return Object.keys(manifest.scripts ?? {})
7
- .filter(s => /test/.test(s))
8
- .some(s => manifest.scripts && /node (.*)--test/.test(manifest.scripts[s]));
9
- };
5
+ export const isEnabled = ({ manifest }) => Object.keys(manifest.scripts ?? {}).some(script => manifest.scripts && /(?<=^|\s)node (.*)--test/.test(manifest.scripts[script]));
10
6
  export const ENTRY_FILE_PATTERNS = [
11
7
  '**/*{.,-,_}test.?(c|m)js',
12
8
  '**/test-*.?(c|m)js',
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'npm-package-json-lint';
@@ -9,9 +10,7 @@ const findNpmPkgJsonLintConfigDependencies = async (configFilePath, options) =>
9
10
  const { manifest, isProduction } = options;
10
11
  if (isProduction)
11
12
  return [];
12
- const localConfig = configFilePath.endsWith('package.json')
13
- ? manifest[PACKAGE_JSON_PATH]
14
- : await load(configFilePath);
13
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest[PACKAGE_JSON_PATH] : await load(configFilePath);
15
14
  return localConfig?.extends ? [localConfig.extends] : [];
16
15
  };
17
16
  export const findDependencies = timerify(findNpmPkgJsonLintConfigDependencies);
@@ -1,7 +1,6 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
2
1
  import { compact } from '../../util/array.js';
3
2
  import { timerify } from '../../util/Performance.js';
4
- import { hasDependency, load } from '../../util/plugin.js';
3
+ import { getDependenciesFromScripts, hasDependency, load } from '../../util/plugin.js';
5
4
  export const NAME = 'Nx';
6
5
  export const ENABLERS = ['nx', /^@nrwl\//, /^@nx\//];
7
6
  export const isEnabled = ({ dependencies }) => hasDependency(dependencies, ENABLERS);
@@ -21,7 +20,7 @@ const findNxDependencies = async (configFilePath, options) => {
21
20
  const scripts = targets
22
21
  .filter(target => target.executor === 'nx:run-commands')
23
22
  .flatMap(target => target.options?.commands ?? (target.options?.command ? [target.options.command] : []));
24
- const dependencies = _getDependenciesFromScripts(scripts, { cwd, manifest });
23
+ const dependencies = getDependenciesFromScripts(scripts, { cwd, manifest });
25
24
  return compact([...executors, ...dependencies]);
26
25
  };
27
26
  export const findDependencies = timerify(findNxDependencies);
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'PostCSS';
@@ -8,9 +9,7 @@ const findPostCSSDependencies = async (configFilePath, options) => {
8
9
  const { manifest, isProduction } = options;
9
10
  if (isProduction)
10
11
  return [];
11
- const localConfig = configFilePath.endsWith('package.json')
12
- ? manifest?.postcss
13
- : await load(configFilePath);
12
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest?.postcss : await load(configFilePath);
14
13
  if (!localConfig)
15
14
  return [];
16
15
  return localConfig.plugins
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'Prettier';
@@ -12,9 +13,7 @@ export const CONFIG_FILE_PATTERNS = [
12
13
  const findPrettierDependencies = async (configFilePath, { manifest, isProduction }) => {
13
14
  if (isProduction)
14
15
  return [];
15
- const localConfig = configFilePath.endsWith('package.json')
16
- ? manifest.prettier
17
- : await load(configFilePath);
16
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.prettier : await load(configFilePath);
18
17
  if (typeof localConfig === 'string') {
19
18
  return [localConfig];
20
19
  }
@@ -1,6 +1,6 @@
1
- import { _getDependenciesFromScripts } from '../../binaries/index.js';
1
+ import { basename } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
- import { hasDependency, load } from '../../util/plugin.js';
3
+ import { getDependenciesFromScripts, hasDependency, load } from '../../util/plugin.js';
4
4
  export const NAME = 'Release It';
5
5
  export const ENABLERS = ['release-it'];
6
6
  export const PACKAGE_JSON_PATH = 'release-it';
@@ -15,9 +15,7 @@ const findReleaseItDependencies = async (configFilePath, options) => {
15
15
  const { cwd, manifest, isProduction } = options;
16
16
  if (isProduction)
17
17
  return [];
18
- const localConfig = configFilePath.endsWith('package.json')
19
- ? manifest[PACKAGE_JSON_PATH]
20
- : await load(configFilePath);
18
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest[PACKAGE_JSON_PATH] : await load(configFilePath);
21
19
  if (!localConfig)
22
20
  return [];
23
21
  const plugins = localConfig.plugins ? Object.keys(localConfig.plugins) : [];
@@ -28,7 +26,7 @@ const findReleaseItDependencies = async (configFilePath, options) => {
28
26
  if (typeof localConfig.gitlab?.releaseNotes === 'string') {
29
27
  scripts.push(localConfig.gitlab.releaseNotes);
30
28
  }
31
- const dependencies = _getDependenciesFromScripts(scripts, { cwd, manifest });
29
+ const dependencies = getDependenciesFromScripts(scripts, { cwd, manifest });
32
30
  return [...plugins, ...dependencies];
33
31
  };
34
32
  export const findDependencies = timerify(findReleaseItDependencies);
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'Remark';
@@ -15,9 +16,7 @@ const findRemarkDependencies = async (configFilePath, options) => {
15
16
  const { manifest, isProduction } = options;
16
17
  if (isProduction)
17
18
  return [];
18
- const localConfig = configFilePath.endsWith('package.json')
19
- ? manifest[PACKAGE_JSON_PATH]
20
- : await load(configFilePath);
19
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest[PACKAGE_JSON_PATH] : await load(configFilePath);
21
20
  if (!localConfig)
22
21
  return [];
23
22
  const plugins = localConfig.plugins
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'Semantic Release';
@@ -14,9 +15,7 @@ const findSemanticReleaseDependencies = async (configFilePath, options) => {
14
15
  const { manifest, isProduction } = options;
15
16
  if (isProduction)
16
17
  return [];
17
- const localConfig = configFilePath.endsWith('package.json')
18
- ? manifest[PACKAGE_JSON_PATH]
19
- : await load(configFilePath);
18
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest[PACKAGE_JSON_PATH] : await load(configFilePath);
20
19
  const plugins = (localConfig?.plugins ?? []).map(plugin => (Array.isArray(plugin) ? plugin[0] : plugin));
21
20
  return plugins;
22
21
  };
@@ -1,4 +1,4 @@
1
- import { isInternal } from '../../util/path.js';
1
+ import { basename, isInternal } from '../../util/path.js';
2
2
  import { timerify } from '../../util/Performance.js';
3
3
  import { hasDependency, load } from '../../util/plugin.js';
4
4
  export const NAME = 'Stylelint';
@@ -13,9 +13,7 @@ const findPluginDependencies = async (configFilePath, options) => {
13
13
  const { manifest, isProduction } = options;
14
14
  if (isProduction)
15
15
  return [];
16
- const localConfig = configFilePath.endsWith('package.json')
17
- ? manifest.stylelint
18
- : await load(configFilePath);
16
+ const localConfig = basename(configFilePath) === 'package.json' ? manifest.stylelint : await load(configFilePath);
19
17
  if (!localConfig)
20
18
  return [];
21
19
  const extend = localConfig.extends ? [localConfig.extends].flat().filter(extend => !isInternal(extend)) : [];
@@ -1,3 +1,4 @@
1
+ import { basename } from '../../util/path.js';
1
2
  import { timerify } from '../../util/Performance.js';
2
3
  import { hasDependency, load } from '../../util/plugin.js';
3
4
  export const NAME = 'TypeDoc';
@@ -16,9 +17,9 @@ const findTypeDocDependencies = async (configFilePath, options) => {
16
17
  const { manifest, isProduction } = options;
17
18
  if (isProduction)
18
19
  return [];
19
- const localConfig = configFilePath.endsWith('package.json')
20
+ const localConfig = basename(configFilePath) === 'package.json'
20
21
  ? manifest[PACKAGE_JSON_PATH]
21
- : configFilePath.endsWith('tsconfig.json')
22
+ : basename(configFilePath) === 'tsconfig.json'
22
23
  ? (await load(configFilePath)).typedocOptions
23
24
  : await load(configFilePath);
24
25
  return localConfig?.plugin ?? [];
@@ -1,7 +1,7 @@
1
1
  import { isBuiltin } from 'node:module';
2
2
  import ts from 'typescript';
3
3
  import { getOrSet } from '../util/map.js';
4
- import { isMaybePackageName, sanitizeSpecifier } from '../util/modules.js';
4
+ import { isStartsLikePackageName, sanitizeSpecifier } from '../util/modules.js';
5
5
  import { isInNodeModules } from '../util/path.js';
6
6
  import { isDeclarationFileExtension, isAccessExpression, getAccessExpressionName, getJSDocTags, getLineAndCharacterOfPosition, } from './ast-helpers.js';
7
7
  import getExportVisitors from './visitors/exports/index.js';
@@ -59,8 +59,9 @@ export const getImportsAndExports = (sourceFile, getResolvedModule, options) =>
59
59
  addInternalImport({ identifier, specifier, symbol, filePath, isReExport });
60
60
  }
61
61
  const sanitizedSpecifier = sanitizeSpecifier(specifier);
62
- if (!isMaybePackageName(sanitizedSpecifier))
62
+ if (!isStartsLikePackageName(sanitizedSpecifier)) {
63
63
  return;
64
+ }
64
65
  if (isDeclarationFileExtension(module.resolvedModule.extension)) {
65
66
  externalImports.add(sanitizedSpecifier);
66
67
  }
@@ -2,7 +2,7 @@ import type { PackageJson } from '@npmcli/package-json';
2
2
  export declare const getPackageNameFromModuleSpecifier: (moduleSpecifier: string) => string | undefined;
3
3
  export declare const getPackageNameFromFilePath: (value: string) => string;
4
4
  export declare const normalizeSpecifierFromFilePath: (value: string) => string;
5
- export declare const isMaybePackageName: (specifier: string) => boolean;
5
+ export declare const isStartsLikePackageName: (specifier: string) => boolean;
6
6
  export declare const isDefinitelyTyped: (packageName: string) => boolean;
7
7
  export declare const getDefinitelyTypedFor: (packageName: string) => string;
8
8
  export declare const getPackageFromDefinitelyTyped: (typedDependency: string) => string;
@@ -3,7 +3,7 @@ import { _glob } from './glob.js';
3
3
  import { getStringValues } from './object.js';
4
4
  import { isAbsolute, toPosix } from './path.js';
5
5
  export const getPackageNameFromModuleSpecifier = (moduleSpecifier) => {
6
- if (!isMaybePackageName(moduleSpecifier))
6
+ if (!isStartsLikePackageName(moduleSpecifier))
7
7
  return;
8
8
  const parts = moduleSpecifier.split('/').slice(0, 2);
9
9
  return moduleSpecifier.startsWith('@') ? parts.join('/') : parts[0];
@@ -20,7 +20,7 @@ export const normalizeSpecifierFromFilePath = (value) => {
20
20
  return match[match.length - 1];
21
21
  return value;
22
22
  };
23
- export const isMaybePackageName = (specifier) => /^@?[a-z0-9]/.test(specifier);
23
+ export const isStartsLikePackageName = (specifier) => /^@?[a-z0-9]/.test(specifier);
24
24
  export const isDefinitelyTyped = (packageName) => packageName.startsWith('@types/');
25
25
  export const getDefinitelyTypedFor = (packageName) => {
26
26
  if (isDefinitelyTyped(packageName))
@@ -1,6 +1,7 @@
1
1
  export declare const isAbsolute: (path: string) => boolean;
2
2
  export declare const dirname: (path: string) => string;
3
3
  export declare const extname: (path: string) => string;
4
+ export declare const basename: (path: string, suffix?: string | undefined) => string;
4
5
  export declare const join: (...paths: string[]) => string;
5
6
  export declare const toPosix: (value: string) => string;
6
7
  export declare const cwd: string;
package/dist/util/path.js CHANGED
@@ -4,6 +4,7 @@ const { directory } = parsedArgValues;
4
4
  export const isAbsolute = path.isAbsolute;
5
5
  export const dirname = path.posix.dirname;
6
6
  export const extname = path.posix.extname;
7
+ export const basename = path.posix.basename;
7
8
  export const join = path.posix.join;
8
9
  export const toPosix = (value) => value.split(path.sep).join(path.posix.sep);
9
10
  export const cwd = directory ? path.posix.resolve(directory) : toPosix(process.cwd());
@@ -1,5 +1,6 @@
1
1
  export { _load as load, _loadFile as loadFile } from './loader.js';
2
2
  export { _tryResolve as tryResolve } from './require.js';
3
+ export { _getDependenciesFromScripts as getDependenciesFromScripts } from '../binaries/index.js';
3
4
  import type { RawPluginConfiguration } from '../types/config.js';
4
5
  export declare const toCamelCase: (name: string) => string;
5
6
  export declare const hasDependency: (dependencies: Set<string>, values: (string | RegExp)[]) => boolean;
@@ -1,5 +1,6 @@
1
1
  export { _load as load, _loadFile as loadFile } from './loader.js';
2
2
  export { _tryResolve as tryResolve } from './require.js';
3
+ export { _getDependenciesFromScripts as getDependenciesFromScripts } from '../binaries/index.js';
3
4
  import { arrayify } from './array.js';
4
5
  export const toCamelCase = (name) => name.toLowerCase().replace(/(-[a-z])/g, group => group.toUpperCase().replace('-', ''));
5
6
  export const hasDependency = (dependencies, values) => values.some(value => {
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "3.5.0";
1
+ export declare const version = "3.5.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '3.5.0';
1
+ export const version = '3.5.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "3.5.0",
3
+ "version": "3.5.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": {