Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: webpro-nl/knip
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 5.24.3
Choose a base ref
...
head repository: webpro-nl/knip
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5.24.4
Choose a head ref
  • 2 commits
  • 10 files changed
  • 1 contributor

Commits on Jul 8, 2024

  1. Copy the full SHA
    65d8098 View commit details
  2. Release 5.24.4

    webpro committed Jul 8, 2024
    Copy the full SHA
    1f1c8bb View commit details
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module.exports = {
testResultsProcessor: 'jest-phabricator',
setupFiles: ['@workspaces-plugin-config/shared/jest-setup.ts'],
};
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
"test": "vitest"
},
"devDependencies": {
"@workspaces-plugin-config/shared": "*",
"eslint-plugin-no-secrets": "*",
"eslint-plugin-prettier": "*",
"vitest": "^0.34.6"
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@workspaces-plugin-config/package-using-ava",
"name": "@workspaces-plugin-config/shared",
"scripts": {
"test": "ava"
},
2 changes: 1 addition & 1 deletion packages/knip/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knip",
"version": "5.24.3",
"version": "5.24.4",
"description": "Find unused files, dependencies and exports in your TypeScript and JavaScript projects",
"homepage": "https://knip.dev",
"repository": {
17 changes: 12 additions & 5 deletions packages/knip/src/plugins/jest/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
import { dirname, isInternal, join, toAbsolute } from '#p/util/path.js';
import { hasDependency, load } from '#p/util/plugin.js';
import { hasDependency, load, resolveEntry } from '#p/util/plugin.js';
import { toEntryPattern } from '#p/util/protocols.js';
import type { JestConfig, JestInitialOptions } from './types.js';

@@ -32,6 +32,7 @@ const resolveExtensibleConfig = async (configFilePath: string) => {

const resolveDependencies = async (config: JestInitialOptions, options: PluginOptions): Promise<string[]> => {
const { configFileDir } = options;

if (config?.preset) {
const { preset } = config;
if (isInternal(preset)) {
@@ -40,9 +41,11 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
Object.assign(config, presetConfig);
}
}

const presets = (config.preset ? [config.preset] : []).map(preset =>
isInternal(preset) ? preset : join(preset, 'jest-preset')
);

const projects = [];
for (const project of config.projects ?? []) {
if (typeof project === 'string') {
@@ -52,6 +55,7 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
for (const dependency of dependencies) projects.push(dependency);
}
}

const runner = config.runner ? [config.runner] : [];
const environments = config.testEnvironment === 'jsdom' ? ['jest-environment-jsdom'] : [];
const resolvers = config.resolver ? [config.resolver] : [];
@@ -62,8 +66,6 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
: [];
const watchPlugins =
config.watchPlugins?.map(watchPlugin => (typeof watchPlugin === 'string' ? watchPlugin : watchPlugin[0])) ?? [];
const setupFiles = (config.setupFiles ?? []).map(toEntryPattern);
const setupFilesAfterEnv = (config.setupFilesAfterEnv ?? []).map(toEntryPattern);
const transform = config.transform
? Object.values(config.transform).map(transform => (typeof transform === 'string' ? transform : transform[0]))
: [];
@@ -72,11 +74,16 @@ const resolveDependencies = async (config: JestInitialOptions, options: PluginOp
? Object.values(config.moduleNameMapper).map(mapper => (typeof mapper === 'string' ? mapper : mapper[0]))
: []
).filter(value => !/\$[0-9]/.test(value));

const testResultsProcessor = config.testResultsProcessor ? [config.testResultsProcessor] : [];
const snapshotResolver = config.snapshotResolver ? [config.snapshotResolver] : [];
const testSequencer = config.testSequencer ? [config.testSequencer] : [];
const globalSetup = (config.globalSetup ? [config.globalSetup] : []).map(toEntryPattern);
const globalTeardown = (config.globalTeardown ? [config.globalTeardown] : []).map(toEntryPattern);

const resolve = (specifier: string) => resolveEntry(options, specifier);
const setupFiles = (config.setupFiles ?? []).map(resolve);
const setupFilesAfterEnv = (config.setupFilesAfterEnv ?? []).map(resolve);
const globalSetup = (config.globalSetup ? [config.globalSetup] : []).map(resolve);
const globalTeardown = (config.globalTeardown ? [config.globalTeardown] : []).map(resolve);

return [
...presets,
20 changes: 6 additions & 14 deletions packages/knip/src/plugins/vitest/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PackageJson } from '#p/types/package-json.js';
import type { IsPluginEnabled, Plugin, PluginOptions, ResolveConfig, ResolveEntryPaths } from '#p/types/plugins.js';
import { isAbsolute, join, relative } from '#p/util/path.js';
import { hasDependency, tryResolve } from '#p/util/plugin.js';
import { join } from '#p/util/path.js';
import { hasDependency, resolveEntry } from '#p/util/plugin.js';
import { toEntryPattern } from '#p/util/protocols.js';
import { getEnvPackageName, getExternalReporters } from './helpers.js';
import type { COMMAND, MODE, ViteConfig, ViteConfigOrFn, VitestWorkspaceConfig } from './types.js';
@@ -18,15 +18,6 @@ const config = ['vitest*.config.{js,mjs,ts,cjs,mts,cts}', 'vitest.{workspace,pro

const entry = ['**/*.{bench,test,test-d,spec}.?(c|m)[jt]s?(x)'];

const resolveEntry = (options: PluginOptions, rootDir: string, specifier: string) => {
const { configFileDir, configFileName } = options;
const resolvedPath = isAbsolute(specifier)
? specifier
: tryResolve(join(configFileDir, rootDir, specifier), join(configFileDir, rootDir, configFileName));
if (resolvedPath) return toEntryPattern(relative(configFileDir, resolvedPath));
return specifier;
};

const isVitestCoverageCommand = /vitest(.+)--coverage(?:\.enabled(?:=true)?)?/;

const hasScriptWithCoverage = (scripts: PackageJson['scripts']) =>
@@ -51,8 +42,9 @@ const findConfigDependencies = (localConfig: ViteConfig, options: PluginOptions)
const coverage = hasCoverageEnabled ? [`@vitest/coverage-${testConfig.coverage?.provider ?? 'v8'}`] : [];

const rootDir = testConfig.root ?? '.';
const setupFiles = [testConfig.setupFiles ?? []].flat().map(v => resolveEntry(options, rootDir, v));
const globalSetup = [testConfig.globalSetup ?? []].flat().map(v => resolveEntry(options, rootDir, v));
const setupFiles = [testConfig.setupFiles ?? []].flat().map(specifier => resolveEntry(options, specifier, rootDir));
const globalSetup = [testConfig.globalSetup ?? []].flat().map(specifier => resolveEntry(options, specifier, rootDir));

return [...environments, ...reporters, ...coverage, ...setupFiles, ...globalSetup];
};

@@ -101,7 +93,7 @@ export const resolveConfig: ResolveConfig<ViteConfigOrFn | VitestWorkspaceConfig
const entry = cfg.build?.lib?.entry ?? [];
const rootDir = cfg.test?.root ?? '.';
const deps = (typeof entry === 'string' ? [entry] : Object.values(entry)).map(specifier =>
resolveEntry(options, rootDir, specifier)
resolveEntry(options, specifier, rootDir)
);
for (const dependency of deps) dependencies.add(dependency);
}
13 changes: 11 additions & 2 deletions packages/knip/src/util/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export { _getDependenciesFromScripts as getDependenciesFromScripts } from '../binaries/index.js';
export { _loadJSON as loadJSON } from './fs.js';
export { _load as load } from './loader.js';
export { _tryResolve as tryResolve } from './require.js';
import type { RawPluginConfiguration } from '../types/config.js';
import type { Plugin, PluginOptions } from '../types/plugins.js';
import { arrayify } from './array.js';
import { _load as load } from './loader.js';
import { get } from './object.js';
import { basename } from './path.js';
import { basename, isAbsolute, join, relative } from './path.js';
import { toEntryPattern, toProductionEntryPattern } from './protocols.js';
import { _tryResolve as tryResolve } from './require.js';

export const toCamelCase = (name: string) =>
name.toLowerCase().replace(/(-[a-z])/g, group => group.toUpperCase().replace('-', ''));
@@ -131,3 +131,12 @@ export const toUnconfig = toConfigMap(['json', 'ts', 'mts', 'cts', 'js', 'mjs',
rcSuffix: '',
configFiles: false,
});

export const resolveEntry = (options: PluginOptions, specifier: string, rootDir = '.') => {
const { configFileDir, configFileName } = options;
const resolvedPath = isAbsolute(specifier)
? specifier
: tryResolve(join(configFileDir, rootDir, specifier), join(configFileDir, rootDir, configFileName));
if (resolvedPath) return toEntryPattern(relative(configFileDir, resolvedPath));
return specifier;
};
2 changes: 1 addition & 1 deletion packages/knip/src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '5.24.3';
export const version = '5.24.4';
11 changes: 6 additions & 5 deletions packages/knip/test/workspaces-plugin-config.test.ts
Original file line number Diff line number Diff line change
@@ -15,8 +15,8 @@ test('Use root plugin config in workspaces', async () => {

assert.deepEqual(counters, {
...baseCounters,
total: 25,
processed: 25,
total: 26,
processed: 26,
});
});

@@ -33,13 +33,14 @@ test('Use root plugin config in workspaces (strict production)', async () => {
new Set([
join(cwd, 'packages/frontend/components/component.js'),
join(cwd, 'packages/package1/components/component.js'),
join(cwd, 'packages/package1/jest-setup.ts'),
])
);

assert.deepEqual(counters, {
...baseCounters,
files: 2,
total: 6,
processed: 6,
files: 3,
total: 7,
processed: 7,
});
});