Comparing version 5.18.2 to 5.19.0
import parse from '@ericcornelissen/bash-parser'; | ||
import { debugLogObject } from '../util/debug.js'; | ||
import { toBinary } from '../util/protocols.js'; | ||
import * as FallbackResolver from './resolvers/fallback.js'; | ||
import KnownResolvers from './resolvers/index.js'; | ||
import { stripBinaryPath } from './util.js'; | ||
import { parseNodeArgs } from './resolvers/node.js'; | ||
import { trimBinary } from './util.js'; | ||
const spawningBinaries = ['cross-env', 'retry-cli']; | ||
const isExpansion = (node) => 'expansion' in node; | ||
const isAssignment = (node) => 'type' in node && node.type === 'AssignmentWord'; | ||
export const getBinariesFromScript = (script, options) => { | ||
@@ -13,4 +18,7 @@ if (!script) | ||
case 'Command': { | ||
const binary = node.name?.text ? stripBinaryPath(node.name.text) : node.name?.text; | ||
const commandExpansions = node.prefix?.flatMap(prefix => prefix.expansion?.filter(expansion => expansion.type === 'CommandExpansion') ?? []) ?? []; | ||
const binary = node.name?.text ? trimBinary(node.name.text) : node.name?.text; | ||
const commandExpansions = node.prefix | ||
?.filter(isExpansion) | ||
.map(prefix => prefix.expansion) | ||
.flatMap(expansion => expansion.filter(expansion => expansion.type === 'CommandExpansion') ?? []) ?? []; | ||
if (commandExpansions.length > 0) { | ||
@@ -28,2 +36,9 @@ return commandExpansions.flatMap(expansion => getBinariesFromNodes(expansion.commandAST.commands)) ?? []; | ||
return fromArgs(args); | ||
const fromNodeOptions = node.prefix | ||
?.filter(isAssignment) | ||
.filter(node => node.text.startsWith('NODE_OPTIONS=')) | ||
.flatMap(node => node.text.split('=')[1]) | ||
.map(arg => parseNodeArgs(arg.split(' '))) | ||
.filter(args => args.require) | ||
.map(arg => arg.require) ?? []; | ||
if (binary in KnownResolvers) { | ||
@@ -33,5 +48,9 @@ const resolver = KnownResolvers[binary]; | ||
} | ||
if (spawningBinaries.includes(binary)) { | ||
const command = script.replace(new RegExp(`.*${node.name?.text ?? binary}(\\s--\\s)?`), ''); | ||
return [toBinary(binary), ...getBinariesFromScript(command, options)]; | ||
} | ||
if (options.knownGlobalsOnly) | ||
return []; | ||
return FallbackResolver.resolve(binary, args, { ...options, fromArgs }); | ||
return [...FallbackResolver.resolve(binary, args, { ...options, fromArgs }), ...fromNodeOptions]; | ||
} | ||
@@ -38,0 +57,0 @@ case 'LogicalExpression': |
@@ -0,2 +1,4 @@ | ||
import parseArgs from 'minimist'; | ||
import type { Resolver } from '../types.js'; | ||
export declare const parseNodeArgs: (args: string[]) => parseArgs.ParsedArgs; | ||
export declare const resolve: Resolver; |
import parseArgs from 'minimist'; | ||
import { compact } from '../../util/array.js'; | ||
import { tryResolveFilePath, tryResolveSpecifiers } from '../util.js'; | ||
export const parseNodeArgs = (args) => parseArgs(args, { | ||
string: ['r'], | ||
alias: { require: ['r', 'loader', 'experimental-loader', 'test-reporter', 'watch', 'import'] }, | ||
}); | ||
export const resolve = (_binary, args, { cwd }) => { | ||
const parsed = parseArgs(args, { | ||
string: ['r'], | ||
alias: { require: ['r', 'loader', 'experimental-loader', 'test-reporter', 'watch', 'import'] }, | ||
}); | ||
const parsed = parseNodeArgs(args); | ||
return compact([tryResolveFilePath(cwd, parsed._[0]), ...tryResolveSpecifiers(cwd, [parsed.require].flat())]); | ||
}; |
export declare const tryResolveFilePath: (cwd: string, specifier: string, acceptModuleSpecifier?: boolean) => string | undefined; | ||
export declare const tryResolveSpecifiers: (cwd: string, specifiers: string[]) => (string | undefined)[]; | ||
export declare const stripVersionFromSpecifier: (specifier: string) => string; | ||
export declare const stripBinaryPath: (command: string) => string; | ||
export declare const trimBinary: (command: string) => string; | ||
export declare const argsFrom: (args: string[], from: string) => string[]; |
@@ -18,3 +18,3 @@ import { getPackageNameFromFilePath, getPackageNameFromModuleSpecifier } from '../util/modules.js'; | ||
else if (specifier.includes('node_modules/.bin')) { | ||
return toBinary(stripBinaryPath(specifier)); | ||
return toBinary(trimBinary(specifier)); | ||
} | ||
@@ -29,5 +29,5 @@ else { | ||
const stripNodeModulesFromPath = (command) => command.replace(/^(\.\/)?node_modules\//, ''); | ||
export const stripBinaryPath = (command) => stripVersionFromSpecifier(stripNodeModulesFromPath(command) | ||
export const trimBinary = (command) => stripVersionFromSpecifier(stripNodeModulesFromPath(command) | ||
.replace(/^(\.bin\/)/, '') | ||
.replace(/\$\(npm bin\)\/(\w+)/, '$1')); | ||
export const argsFrom = (args, from) => args.slice(args.indexOf(from)); |
@@ -85,3 +85,3 @@ import picocolors from 'picocolors'; | ||
if (isConfigurationError(knownError)) | ||
console.log(`\n${helpText}`); | ||
console.log('\nRun `knip --help` or visit https://knip.dev for help'); | ||
process.exit(2); | ||
@@ -88,0 +88,0 @@ } |
@@ -1,2 +0,1 @@ | ||
import { existsSync } from 'node:fs'; | ||
import picomatch from 'picomatch'; | ||
@@ -17,3 +16,3 @@ import { ConfigurationValidator } from './ConfigurationValidator.js'; | ||
import { getKeysByValue } from './util/object.js'; | ||
import { join, relative, toPosix } from './util/path.js'; | ||
import { join, relative, resolve, toPosix } from './util/path.js'; | ||
import { createPkgGraph } from './util/pkgs-graph.js'; | ||
@@ -228,4 +227,8 @@ import { normalizePluginConfig, toCamelCase } from './util/plugin.js'; | ||
determineIncludedWorkspaces() { | ||
if (workspaceArg && !existsSync(workspaceArg)) { | ||
throw new ConfigurationError(`Directory does not exist: ${workspaceArg}`); | ||
if (workspaceArg) { | ||
const dir = resolve(workspaceArg); | ||
if (!isDirectory(dir)) | ||
throw new ConfigurationError('Workspace is not a directory'); | ||
if (!isFile(join(dir, 'package.json'))) | ||
throw new ConfigurationError('Unable to find package.json in workspace'); | ||
} | ||
@@ -232,0 +235,0 @@ const getAncestors = (name) => (ancestors, ancestorName) => { |
@@ -64,2 +64,3 @@ import { dirname, isInternal, join, toAbsolute } from '#p/util/path.js'; | ||
const globalSetup = config.globalSetup ? [config.globalSetup] : []; | ||
const globalTeardown = config.globalTeardown ? [config.globalTeardown] : []; | ||
return [ | ||
@@ -81,2 +82,3 @@ ...presets, | ||
...globalSetup, | ||
...globalTeardown, | ||
]; | ||
@@ -83,0 +85,0 @@ }; |
@@ -5,2 +5,3 @@ import { existsSync } from 'node:fs'; | ||
import { DEFAULT_EXTENSIONS, FOREIGN_FILE_EXTENSIONS } from '../constants.js'; | ||
import { timerify } from '../util/Performance.js'; | ||
import { sanitizeSpecifier } from '../util/modules.js'; | ||
@@ -48,3 +49,2 @@ import { dirname, extname, isAbsolute, isInNodeModules, join } from '../util/path.js'; | ||
const ext = extname(resolvedFileName); | ||
const extension = virtualFileExtensions.includes(ext) ? ts.Extension.Js : ext; | ||
if (!virtualFileExtensions.includes(ext) && !FOREIGN_FILE_EXTENSIONS.has(ext)) { | ||
@@ -63,3 +63,3 @@ const srcFilePath = toSourceFilePath(resolvedFileName); | ||
resolvedFileName, | ||
extension, | ||
extension: virtualFileExtensions.includes(ext) ? ts.Extension.Js : ext, | ||
isExternalLibraryImport: isInNodeModules(resolvedFileName), | ||
@@ -100,3 +100,3 @@ resolvedUsingTsExtension: false, | ||
} | ||
return resolveModuleNames; | ||
return timerify(resolveModuleNames); | ||
} |
@@ -5,3 +5,3 @@ import ts from 'typescript'; | ||
export default visit(() => true, node => { | ||
if (isPropertyAccessCall(node, 'require.resolve')) { | ||
if (isPropertyAccessCall(node, 'require.resolve') || isPropertyAccessCall(node, 'import.meta.resolve')) { | ||
if (node.arguments[0] && ts.isStringLiteralLike(node.arguments[0])) { | ||
@@ -8,0 +8,0 @@ const specifier = node.arguments[0].text; |
@@ -1,1 +0,1 @@ | ||
export declare const version = "5.18.2"; | ||
export declare const version = "5.19.0"; |
@@ -1,1 +0,1 @@ | ||
export const version = '5.18.2'; | ||
export const version = '5.19.0'; |
{ | ||
"name": "knip", | ||
"version": "5.18.2", | ||
"version": "5.19.0", | ||
"description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects", | ||
@@ -5,0 +5,0 @@ "homepage": "https://knip.dev", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
758137
17160