knip 6.18.0 → 6.19.0

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.
@@ -1,10 +1,13 @@
1
+ import { _syncGlob } from '../../util/glob.js';
1
2
  import { toAlias, toIgnore, toProductionEntry } from '../../util/input.js';
2
3
  import { hasDependency } from '../../util/plugin.js';
3
- import { collectPropertyValues } from '../../typescript/ast-helpers.js';
4
+ import { config as viteConfig } from '../vite/index.js';
5
+ import { collectPropertyValues, findCallArg, findProperty, getPropertyValues, hasImportSpecifier, } from '../../typescript/ast-helpers.js';
6
+ import { _parseFile } from '../../typescript/ast-nodes.js';
4
7
  const title = 'SvelteKit';
5
8
  const enablers = ['@sveltejs/kit'];
6
9
  const isEnabled = ({ dependencies }) => hasDependency(dependencies, enablers);
7
- const config = ['svelte.config.js'];
10
+ const config = ['svelte.config.js', ...viteConfig];
8
11
  const production = [
9
12
  'src/routes/**/+{page,server,page.server,error,layout,layout.server}{,@*}.{js,ts,svelte}',
10
13
  'src/hooks.{server,client}.{js,ts}',
@@ -13,20 +16,41 @@ const production = [
13
16
  'src/service-worker/index.{js,ts}',
14
17
  'src/instrumentation.server.{js,ts}',
15
18
  ];
16
- const getLibPath = (program) => {
19
+ const isConfigInViteConfig = (dir, readFile) => {
20
+ for (const filePath of _syncGlob({ cwd: dir, patterns: viteConfig })) {
21
+ const text = readFile(filePath);
22
+ if (text && findCallArg(_parseFile(filePath, text).program, 'sveltekit'))
23
+ return true;
24
+ }
25
+ return false;
26
+ };
27
+ const getLibFromViteConfig = (program) => {
28
+ const call = findCallArg(program, 'sveltekit');
29
+ const files = call ? findProperty(call, 'files') : undefined;
30
+ const values = files ? getPropertyValues(files, 'lib') : new Set();
31
+ return values.size > 0 ? Array.from(values)[0] : 'src/lib';
32
+ };
33
+ const getLibFromSvelteConfig = (program) => {
17
34
  const values = collectPropertyValues(program, 'lib');
18
35
  return values.size > 0 ? Array.from(values)[0] : 'src/lib';
19
36
  };
20
- const resolveFromAST = program => {
21
- const lib = getLibPath(program);
22
- return [
23
- ...production.map(pattern => toProductionEntry(pattern)),
24
- toAlias('$lib', [`./${lib}`]),
25
- toAlias('$lib/*', [`./${lib}/*`]),
26
- toIgnore('\\$app/.+', 'unresolved'),
27
- toIgnore('\\$env/.+', 'unresolved'),
28
- toIgnore('\\$service-worker', 'unresolved'),
29
- ];
37
+ const toInputs = (lib) => [
38
+ ...production.map(pattern => toProductionEntry(pattern)),
39
+ toAlias('$lib', [`./${lib}`]),
40
+ toAlias('$lib/*', [`./${lib}/*`]),
41
+ toIgnore('\\$app/.+', 'unresolved'),
42
+ toIgnore('\\$env/.+', 'unresolved'),
43
+ toIgnore('\\$service-worker', 'unresolved'),
44
+ ];
45
+ const resolveFromAST = (program, options) => {
46
+ if (options.configFileName.startsWith('vite.config')) {
47
+ if (!hasImportSpecifier(program, '@sveltejs/kit/vite', 'sveltekit'))
48
+ return [];
49
+ return toInputs(getLibFromViteConfig(program));
50
+ }
51
+ if (isConfigInViteConfig(options.configFileDir, options.readFile))
52
+ return [];
53
+ return toInputs(getLibFromSvelteConfig(program));
30
54
  };
31
55
  const plugin = {
32
56
  title,
@@ -15,9 +15,10 @@ const parseFile = (filePath, sourceText) => {
15
15
  export const _parseFile = timerify(parseFile);
16
16
  export const buildLineStarts = (sourceText) => {
17
17
  const starts = [0];
18
- for (let i = 0; i < sourceText.length; i++) {
19
- if (sourceText.charCodeAt(i) === 10)
20
- starts.push(i + 1);
18
+ let index = sourceText.indexOf('\n');
19
+ while (index !== -1) {
20
+ starts.push(index + 1);
21
+ index = sourceText.indexOf('\n', index + 1);
21
22
  }
22
23
  return starts;
23
24
  };
@@ -28,12 +28,11 @@ export const getPackageNameFromFilePath = (value) => {
28
28
  return name;
29
29
  };
30
30
  export const getPackageNameFromSpecifier = (specifier) => isInNodeModules(specifier) ? getPackageNameFromFilePath(specifier) : getPackageNameFromModuleSpecifier(specifier);
31
- const matchPackageNameStart = /^(@[a-z0-9._~]|[a-z0-9])/i;
31
+ const isPackageNameStart = (ch) => (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57);
32
+ const isPackageScopeStart = (ch) => isPackageNameStart(ch) || ch === 46 || ch === 95 || ch === 126;
32
33
  export const isStartsLikePackageName = (specifier) => {
33
34
  const ch = specifier.charCodeAt(0);
34
- if (ch === 46 || ch === 47 || ch === 35 || ch === 126 || ch === 36)
35
- return false;
36
- return matchPackageNameStart.test(specifier);
35
+ return ch === 64 ? isPackageScopeStart(specifier.charCodeAt(1)) : isPackageNameStart(ch);
37
36
  };
38
37
  export const stripVersionFromSpecifier = (specifier) => specifier.replace(/(\S+)@.*/, '$1');
39
38
  const stripNodeModulesFromPath = (command) => command.replace(/(?:\.{0,2}\/)*node_modules\//, '');
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "6.18.0";
1
+ export declare const version = "6.19.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = '6.18.0';
1
+ export const version = '6.19.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knip",
3
- "version": "6.18.0",
3
+ "version": "6.19.0",
4
4
  "description": "Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects",
5
5
  "keywords": [
6
6
  "analysis",