cspell-lib 8.1.2 → 8.1.3

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.
@@ -320,6 +320,9 @@ function resolveFilename(filename, relativeTo) {
320
320
  if (filename instanceof URL)
321
321
  return { filename: toFilePathOrHref(filename) };
322
322
  const r = resolveFile(filename, relativeTo);
323
+ if (r.warning) {
324
+ logWarning(r.warning);
325
+ }
323
326
  return {
324
327
  filename: r.filename.startsWith('file:/') ? fileURLToPath(r.filename) : r.filename,
325
328
  error: r.found ? undefined : new Error(`Failed to resolve file: "${filename}"`),
@@ -1,8 +1,19 @@
1
1
  import { fileURLOrPathToPath, isFileURL, isURLLike } from './url.js';
2
2
  export interface ResolveFileResult {
3
+ /**
4
+ * Absolute path or URL to the file.
5
+ */
3
6
  filename: string;
4
7
  relativeTo: string | undefined;
5
8
  found: boolean;
9
+ /**
10
+ * A warning message if the file was found, but there was a problem.
11
+ */
12
+ warning?: string;
13
+ /**
14
+ * The method used to resolve the file.
15
+ */
16
+ method: string;
6
17
  }
7
18
  /**
8
19
  * Resolve filename to absolute paths.
@@ -11,6 +22,7 @@ export interface ResolveFileResult {
11
22
  * @param relativeTo absolute path
12
23
  */
13
24
  export declare function resolveFile(filename: string, relativeTo: string | URL): ResolveFileResult;
25
+ export declare function _resolveFile(filename: string, relativeTo: string | URL): ResolveFileResult;
14
26
  /**
15
27
  * Check to see if it is a URL.
16
28
  * Note: URLs are absolute!
@@ -8,7 +8,7 @@ import resolveFrom from 'resolve-from';
8
8
  import { fileURLToPath } from 'url';
9
9
  import { srcDirectory } from '../../lib-cjs/pkg-info.cjs';
10
10
  import { fileURLOrPathToPath, isDataURL, isFileURL, isURLLike, resolveFileWithURL, toFilePathOrHref, toURL, } from './url.js';
11
- const testNodeModules = /^node_modules\//;
11
+ const regExpStartsWidthNodeModules = /^node_modules[/\\]/;
12
12
  /**
13
13
  * Resolve filename to absolute paths.
14
14
  * It tries to look for local files as well as node_modules
@@ -16,6 +16,14 @@ const testNodeModules = /^node_modules\//;
16
16
  * @param relativeTo absolute path
17
17
  */
18
18
  export function resolveFile(filename, relativeTo) {
19
+ const result = _resolveFile(filename, relativeTo);
20
+ const match = filename.match(regExpStartsWidthNodeModules);
21
+ if (match) {
22
+ result.warning ??= `Import of '${filename}' should not start with '${match[0]}' in '${toFilePathOrHref(relativeTo)}'. Use '${filename.replace(regExpStartsWidthNodeModules, '')}' or a relative path instead.`;
23
+ }
24
+ return result;
25
+ }
26
+ export function _resolveFile(filename, relativeTo) {
19
27
  filename = filename.replace(/^~/, os.homedir());
20
28
  const steps = [
21
29
  { filename, fn: tryUrl },
@@ -25,20 +33,21 @@ export function resolveFile(filename, relativeTo) {
25
33
  { filename, fn: tryResolveExists },
26
34
  { filename, fn: tryNodeResolveDefaultPaths },
27
35
  { filename, fn: tryResolveFrom },
28
- { filename: filename.replace(testNodeModules, ''), fn: tryResolveFrom },
29
36
  { filename, fn: tryResolveGlobal },
37
+ { filename, fn: tryLegacyResolve },
30
38
  ];
31
39
  for (const step of steps) {
32
40
  const r = step.fn(step.filename, relativeTo);
33
41
  if (r?.found)
34
42
  return r;
35
43
  }
36
- const r = tryUrl(filename, relativeTo);
37
- return (r || {
44
+ const result = tryUrl(filename, relativeTo) || {
38
45
  filename: isRelative(filename) ? joinWith(filename, relativeTo) : filename.toString(),
39
46
  relativeTo: relativeTo.toString(),
40
47
  found: false,
41
- });
48
+ method: 'not found',
49
+ };
50
+ return result;
42
51
  }
43
52
  /**
44
53
  * Check to see if it is a URL.
@@ -55,9 +64,10 @@ function tryUrl(filename, relativeToURL) {
55
64
  filename: file,
56
65
  relativeTo: undefined,
57
66
  found: fs.existsSync(file),
67
+ method: 'tryUrl',
58
68
  };
59
69
  }
60
- return { filename: filename.toString(), relativeTo: undefined, found: true };
70
+ return { filename: filename.toString(), relativeTo: undefined, found: true, method: 'tryUrl' };
61
71
  }
62
72
  if (isURLLike(relativeToURL) && !isDataURL(relativeToURL)) {
63
73
  const relToURL = toURL(relativeToURL);
@@ -67,6 +77,7 @@ function tryUrl(filename, relativeToURL) {
67
77
  filename: toFilePathOrHref(url),
68
78
  relativeTo: toFilePathOrHref(relToURL),
69
79
  found: !isRelToAFile || fs.existsSync(url),
80
+ method: 'tryUrl',
70
81
  };
71
82
  }
72
83
  return undefined;
@@ -77,7 +88,7 @@ function tryCreateRequire(filename, relativeTo) {
77
88
  const require = createRequire(relativeTo);
78
89
  try {
79
90
  const r = require.resolve(filename);
80
- return { filename: r, relativeTo: relativeTo.toString(), found: true };
91
+ return { filename: r, relativeTo: relativeTo.toString(), found: true, method: 'tryCreateRequire' };
81
92
  }
82
93
  catch (_) {
83
94
  return undefined;
@@ -86,7 +97,7 @@ function tryCreateRequire(filename, relativeTo) {
86
97
  function tryNodeResolveDefaultPaths(filename) {
87
98
  try {
88
99
  const r = require.resolve(filename);
89
- return { filename: r, relativeTo: undefined, found: true };
100
+ return { filename: r, relativeTo: undefined, found: true, method: 'tryNodeResolveDefaultPaths' };
90
101
  }
91
102
  catch (_) {
92
103
  return undefined;
@@ -94,7 +105,7 @@ function tryNodeResolveDefaultPaths(filename) {
94
105
  }
95
106
  function tryNodeRequireResolve(filenameOrURL, relativeTo) {
96
107
  const filename = fileURLOrPathToPath(filenameOrURL);
97
- const relativeToPath = fileURLOrPathToPath(relativeTo);
108
+ const relativeToPath = pathFromRelativeTo(relativeTo);
98
109
  const home = os.homedir();
99
110
  function calcPaths(p) {
100
111
  const paths = [p];
@@ -110,7 +121,7 @@ function tryNodeRequireResolve(filenameOrURL, relativeTo) {
110
121
  const paths = calcPaths(path.resolve(relativeToPath));
111
122
  try {
112
123
  const r = require.resolve(filename, { paths });
113
- return { filename: r, relativeTo: relativeToPath, found: true };
124
+ return { filename: r, relativeTo: relativeToPath, found: true, method: 'tryNodeRequireResolve' };
114
125
  }
115
126
  catch (_) {
116
127
  return undefined;
@@ -120,7 +131,7 @@ function tryImportResolve(filename, relativeTo) {
120
131
  try {
121
132
  const paths = isRelative(filename) ? [relativeTo] : [relativeTo, srcDirectory];
122
133
  const resolved = fileURLToPath(importResolveModuleName(filename, paths));
123
- return { filename: resolved, relativeTo: relativeTo.toString(), found: true };
134
+ return { filename: resolved, relativeTo: relativeTo.toString(), found: true, method: 'tryImportResolve' };
124
135
  }
125
136
  catch (_) {
126
137
  return undefined;
@@ -128,35 +139,59 @@ function tryImportResolve(filename, relativeTo) {
128
139
  }
129
140
  function tryResolveGlobal(filename) {
130
141
  const r = resolveGlobal(filename);
131
- return (r && { filename: r, relativeTo: undefined, found: true }) || undefined;
142
+ return (r && { filename: r, relativeTo: undefined, found: true, method: 'tryResolveGlobal' }) || undefined;
132
143
  }
133
144
  function tryResolveExists(filename, relativeTo) {
134
- if (filename instanceof URL || isURLLike(filename) || isURLLike(relativeTo))
145
+ if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) {
135
146
  return undefined;
136
- const toTry = [{ filename }, { filename: path.resolve(relativeTo.toString(), filename), relativeTo }];
147
+ }
148
+ relativeTo = pathFromRelativeTo(relativeTo);
149
+ const toTry = [{ filename }, { filename: path.resolve(relativeTo, filename), relativeTo }];
137
150
  for (const { filename, relativeTo } of toTry) {
138
151
  const found = path.isAbsolute(filename) && fs.existsSync(filename);
139
152
  if (found)
140
- return { filename, relativeTo: relativeTo?.toString(), found };
153
+ return { filename, relativeTo: relativeTo?.toString(), found, method: 'tryResolveExists' };
141
154
  }
142
155
  filename = path.resolve(filename);
143
156
  return {
144
157
  filename,
145
158
  relativeTo: path.resolve('.'),
146
159
  found: fs.existsSync(filename),
160
+ method: 'tryResolveExists',
147
161
  };
148
162
  }
149
163
  function tryResolveFrom(filename, relativeTo) {
150
164
  if (relativeTo instanceof URL)
151
165
  return undefined;
152
166
  try {
153
- return { filename: resolveFrom(relativeTo, filename), relativeTo, found: true };
167
+ return {
168
+ filename: resolveFrom(pathFromRelativeTo(relativeTo), filename),
169
+ relativeTo,
170
+ found: true,
171
+ method: 'tryResolveFrom',
172
+ };
154
173
  }
155
174
  catch (error) {
156
175
  // Failed to resolve a relative module request
157
176
  return undefined;
158
177
  }
159
178
  }
179
+ function tryLegacyResolve(filename, relativeTo) {
180
+ if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) {
181
+ return undefined;
182
+ }
183
+ const relativeToPath = isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo.toString();
184
+ const match = filename.match(regExpStartsWidthNodeModules);
185
+ if (match) {
186
+ const fixedFilename = filename.replace(regExpStartsWidthNodeModules, '');
187
+ const found = tryImportResolve(fixedFilename, relativeToPath) || tryResolveFrom(fixedFilename, relativeToPath);
188
+ if (found?.found) {
189
+ found.method = 'tryLegacyResolve';
190
+ return found;
191
+ }
192
+ }
193
+ return undefined;
194
+ }
160
195
  function isRelative(filename) {
161
196
  if (filename instanceof URL)
162
197
  return false;
@@ -175,6 +210,9 @@ function joinWith(filename, relativeTo) {
175
210
  ? toFilePathOrHref(new URL(filename, relativeTo))
176
211
  : path.resolve(relativeTo, filename);
177
212
  }
213
+ function pathFromRelativeTo(relativeTo) {
214
+ return relativeTo instanceof URL || isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo;
215
+ }
178
216
  export const __testing__ = {
179
217
  isRelative,
180
218
  isFileURL,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-lib",
3
- "version": "8.1.2",
3
+ "version": "8.1.3",
4
4
  "description": "A library of useful functions used across various cspell tools.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -57,21 +57,21 @@
57
57
  },
58
58
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
59
59
  "dependencies": {
60
- "@cspell/cspell-bundled-dicts": "8.1.2",
61
- "@cspell/cspell-pipe": "8.1.2",
62
- "@cspell/cspell-resolver": "8.1.2",
63
- "@cspell/cspell-types": "8.1.2",
64
- "@cspell/dynamic-import": "8.1.2",
65
- "@cspell/strong-weak-map": "8.1.2",
60
+ "@cspell/cspell-bundled-dicts": "8.1.3",
61
+ "@cspell/cspell-pipe": "8.1.3",
62
+ "@cspell/cspell-resolver": "8.1.3",
63
+ "@cspell/cspell-types": "8.1.3",
64
+ "@cspell/dynamic-import": "8.1.3",
65
+ "@cspell/strong-weak-map": "8.1.3",
66
66
  "clear-module": "^4.1.2",
67
67
  "comment-json": "^4.2.3",
68
68
  "configstore": "^6.0.0",
69
- "cspell-config-lib": "8.1.2",
70
- "cspell-dictionary": "8.1.2",
71
- "cspell-glob": "8.1.2",
72
- "cspell-grammar": "8.1.2",
73
- "cspell-io": "8.1.2",
74
- "cspell-trie-lib": "8.1.2",
69
+ "cspell-config-lib": "8.1.3",
70
+ "cspell-dictionary": "8.1.3",
71
+ "cspell-glob": "8.1.3",
72
+ "cspell-grammar": "8.1.3",
73
+ "cspell-io": "8.1.3",
74
+ "cspell-trie-lib": "8.1.3",
75
75
  "fast-equals": "^5.0.1",
76
76
  "gensequence": "^6.0.0",
77
77
  "import-fresh": "^3.3.0",
@@ -95,5 +95,5 @@
95
95
  "cspell-dict-nl-nl": "^1.1.2",
96
96
  "lorem-ipsum": "^2.0.8"
97
97
  },
98
- "gitHead": "ba4eef94480562e7641298b32cb99723fd64aeca"
98
+ "gitHead": "ea4335117b7c0e7b9ec22738315c82fae24ea997"
99
99
  }