cspell-lib
Advanced tools
Comparing version 8.1.2 to 8.1.3
@@ -323,2 +323,5 @@ import assert from 'assert'; | ||
const r = resolveFile(filename, relativeTo); | ||
if (r.warning) { | ||
logWarning(r.warning); | ||
} | ||
return { | ||
@@ -325,0 +328,0 @@ filename: r.filename.startsWith('file:/') ? fileURLToPath(r.filename) : r.filename, |
import { fileURLOrPathToPath, isFileURL, isURLLike } from './url.js'; | ||
export interface ResolveFileResult { | ||
/** | ||
* Absolute path or URL to the file. | ||
*/ | ||
filename: string; | ||
relativeTo: string | undefined; | ||
found: boolean; | ||
/** | ||
* A warning message if the file was found, but there was a problem. | ||
*/ | ||
warning?: string; | ||
/** | ||
* The method used to resolve the file. | ||
*/ | ||
method: string; | ||
} | ||
@@ -14,2 +25,3 @@ /** | ||
export declare function resolveFile(filename: string, relativeTo: string | URL): ResolveFileResult; | ||
export declare function _resolveFile(filename: string, relativeTo: string | URL): ResolveFileResult; | ||
/** | ||
@@ -16,0 +28,0 @@ * Check to see if it is a URL. |
@@ -11,3 +11,3 @@ import { createRequire } from 'node:module'; | ||
import { fileURLOrPathToPath, isDataURL, isFileURL, isURLLike, resolveFileWithURL, toFilePathOrHref, toURL, } from './url.js'; | ||
const testNodeModules = /^node_modules\//; | ||
const regExpStartsWidthNodeModules = /^node_modules[/\\]/; | ||
/** | ||
@@ -20,2 +20,10 @@ * Resolve filename to absolute paths. | ||
export function resolveFile(filename, relativeTo) { | ||
const result = _resolveFile(filename, relativeTo); | ||
const match = filename.match(regExpStartsWidthNodeModules); | ||
if (match) { | ||
result.warning ??= `Import of '${filename}' should not start with '${match[0]}' in '${toFilePathOrHref(relativeTo)}'. Use '${filename.replace(regExpStartsWidthNodeModules, '')}' or a relative path instead.`; | ||
} | ||
return result; | ||
} | ||
export function _resolveFile(filename, relativeTo) { | ||
filename = filename.replace(/^~/, os.homedir()); | ||
@@ -30,4 +38,4 @@ const steps = [ | ||
{ filename, fn: tryResolveFrom }, | ||
{ filename: filename.replace(testNodeModules, ''), fn: tryResolveFrom }, | ||
{ filename, fn: tryResolveGlobal }, | ||
{ filename, fn: tryLegacyResolve }, | ||
]; | ||
@@ -39,8 +47,9 @@ for (const step of steps) { | ||
} | ||
const r = tryUrl(filename, relativeTo); | ||
return (r || { | ||
const result = tryUrl(filename, relativeTo) || { | ||
filename: isRelative(filename) ? joinWith(filename, relativeTo) : filename.toString(), | ||
relativeTo: relativeTo.toString(), | ||
found: false, | ||
}); | ||
method: 'not found', | ||
}; | ||
return result; | ||
} | ||
@@ -62,5 +71,6 @@ /** | ||
found: fs.existsSync(file), | ||
method: 'tryUrl', | ||
}; | ||
} | ||
return { filename: filename.toString(), relativeTo: undefined, found: true }; | ||
return { filename: filename.toString(), relativeTo: undefined, found: true, method: 'tryUrl' }; | ||
} | ||
@@ -75,2 +85,3 @@ if (isURLLike(relativeToURL) && !isDataURL(relativeToURL)) { | ||
found: !isRelToAFile || fs.existsSync(url), | ||
method: 'tryUrl', | ||
}; | ||
@@ -86,3 +97,3 @@ } | ||
const r = require.resolve(filename); | ||
return { filename: r, relativeTo: relativeTo.toString(), found: true }; | ||
return { filename: r, relativeTo: relativeTo.toString(), found: true, method: 'tryCreateRequire' }; | ||
} | ||
@@ -96,3 +107,3 @@ catch (_) { | ||
const r = require.resolve(filename); | ||
return { filename: r, relativeTo: undefined, found: true }; | ||
return { filename: r, relativeTo: undefined, found: true, method: 'tryNodeResolveDefaultPaths' }; | ||
} | ||
@@ -105,3 +116,3 @@ catch (_) { | ||
const filename = fileURLOrPathToPath(filenameOrURL); | ||
const relativeToPath = fileURLOrPathToPath(relativeTo); | ||
const relativeToPath = pathFromRelativeTo(relativeTo); | ||
const home = os.homedir(); | ||
@@ -122,3 +133,3 @@ function calcPaths(p) { | ||
const r = require.resolve(filename, { paths }); | ||
return { filename: r, relativeTo: relativeToPath, found: true }; | ||
return { filename: r, relativeTo: relativeToPath, found: true, method: 'tryNodeRequireResolve' }; | ||
} | ||
@@ -133,3 +144,3 @@ catch (_) { | ||
const resolved = fileURLToPath(importResolveModuleName(filename, paths)); | ||
return { filename: resolved, relativeTo: relativeTo.toString(), found: true }; | ||
return { filename: resolved, relativeTo: relativeTo.toString(), found: true, method: 'tryImportResolve' }; | ||
} | ||
@@ -142,12 +153,14 @@ catch (_) { | ||
const r = resolveGlobal(filename); | ||
return (r && { filename: r, relativeTo: undefined, found: true }) || undefined; | ||
return (r && { filename: r, relativeTo: undefined, found: true, method: 'tryResolveGlobal' }) || undefined; | ||
} | ||
function tryResolveExists(filename, relativeTo) { | ||
if (filename instanceof URL || isURLLike(filename) || isURLLike(relativeTo)) | ||
if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) { | ||
return undefined; | ||
const toTry = [{ filename }, { filename: path.resolve(relativeTo.toString(), filename), relativeTo }]; | ||
} | ||
relativeTo = pathFromRelativeTo(relativeTo); | ||
const toTry = [{ filename }, { filename: path.resolve(relativeTo, filename), relativeTo }]; | ||
for (const { filename, relativeTo } of toTry) { | ||
const found = path.isAbsolute(filename) && fs.existsSync(filename); | ||
if (found) | ||
return { filename, relativeTo: relativeTo?.toString(), found }; | ||
return { filename, relativeTo: relativeTo?.toString(), found, method: 'tryResolveExists' }; | ||
} | ||
@@ -159,2 +172,3 @@ filename = path.resolve(filename); | ||
found: fs.existsSync(filename), | ||
method: 'tryResolveExists', | ||
}; | ||
@@ -166,3 +180,8 @@ } | ||
try { | ||
return { filename: resolveFrom(relativeTo, filename), relativeTo, found: true }; | ||
return { | ||
filename: resolveFrom(pathFromRelativeTo(relativeTo), filename), | ||
relativeTo, | ||
found: true, | ||
method: 'tryResolveFrom', | ||
}; | ||
} | ||
@@ -174,2 +193,18 @@ catch (error) { | ||
} | ||
function tryLegacyResolve(filename, relativeTo) { | ||
if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) { | ||
return undefined; | ||
} | ||
const relativeToPath = isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo.toString(); | ||
const match = filename.match(regExpStartsWidthNodeModules); | ||
if (match) { | ||
const fixedFilename = filename.replace(regExpStartsWidthNodeModules, ''); | ||
const found = tryImportResolve(fixedFilename, relativeToPath) || tryResolveFrom(fixedFilename, relativeToPath); | ||
if (found?.found) { | ||
found.method = 'tryLegacyResolve'; | ||
return found; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function isRelative(filename) { | ||
@@ -193,2 +228,5 @@ if (filename instanceof URL) | ||
} | ||
function pathFromRelativeTo(relativeTo) { | ||
return relativeTo instanceof URL || isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo; | ||
} | ||
export const __testing__ = { | ||
@@ -195,0 +233,0 @@ isRelative, |
{ | ||
"name": "cspell-lib", | ||
"version": "8.1.2", | ||
"version": "8.1.3", | ||
"description": "A library of useful functions used across various cspell tools.", | ||
@@ -60,17 +60,17 @@ "type": "module", | ||
"dependencies": { | ||
"@cspell/cspell-bundled-dicts": "8.1.2", | ||
"@cspell/cspell-pipe": "8.1.2", | ||
"@cspell/cspell-resolver": "8.1.2", | ||
"@cspell/cspell-types": "8.1.2", | ||
"@cspell/dynamic-import": "8.1.2", | ||
"@cspell/strong-weak-map": "8.1.2", | ||
"@cspell/cspell-bundled-dicts": "8.1.3", | ||
"@cspell/cspell-pipe": "8.1.3", | ||
"@cspell/cspell-resolver": "8.1.3", | ||
"@cspell/cspell-types": "8.1.3", | ||
"@cspell/dynamic-import": "8.1.3", | ||
"@cspell/strong-weak-map": "8.1.3", | ||
"clear-module": "^4.1.2", | ||
"comment-json": "^4.2.3", | ||
"configstore": "^6.0.0", | ||
"cspell-config-lib": "8.1.2", | ||
"cspell-dictionary": "8.1.2", | ||
"cspell-glob": "8.1.2", | ||
"cspell-grammar": "8.1.2", | ||
"cspell-io": "8.1.2", | ||
"cspell-trie-lib": "8.1.2", | ||
"cspell-config-lib": "8.1.3", | ||
"cspell-dictionary": "8.1.3", | ||
"cspell-glob": "8.1.3", | ||
"cspell-grammar": "8.1.3", | ||
"cspell-io": "8.1.3", | ||
"cspell-trie-lib": "8.1.3", | ||
"fast-equals": "^5.0.1", | ||
@@ -99,3 +99,3 @@ "gensequence": "^6.0.0", | ||
}, | ||
"gitHead": "ba4eef94480562e7641298b32cb99723fd64aeca" | ||
"gitHead": "ea4335117b7c0e7b9ec22738315c82fae24ea997" | ||
} |
399117
9841
+ Added@cspell/cspell-bundled-dicts@8.1.3(transitive)
+ Added@cspell/cspell-pipe@8.1.3(transitive)
+ Added@cspell/cspell-resolver@8.1.3(transitive)
+ Added@cspell/cspell-service-bus@8.1.3(transitive)
+ Added@cspell/cspell-types@8.1.3(transitive)
+ Added@cspell/dynamic-import@8.1.3(transitive)
+ Added@cspell/strong-weak-map@8.1.3(transitive)
+ Addedcspell-config-lib@8.1.3(transitive)
+ Addedcspell-dictionary@8.1.3(transitive)
+ Addedcspell-glob@8.1.3(transitive)
+ Addedcspell-grammar@8.1.3(transitive)
+ Addedcspell-io@8.1.3(transitive)
+ Addedcspell-trie-lib@8.1.3(transitive)
- Removed@cspell/cspell-bundled-dicts@8.1.2(transitive)
- Removed@cspell/cspell-pipe@8.1.2(transitive)
- Removed@cspell/cspell-resolver@8.1.2(transitive)
- Removed@cspell/cspell-service-bus@8.1.2(transitive)
- Removed@cspell/cspell-types@8.1.2(transitive)
- Removed@cspell/dynamic-import@8.1.2(transitive)
- Removed@cspell/strong-weak-map@8.1.2(transitive)
- Removedcspell-config-lib@8.1.2(transitive)
- Removedcspell-dictionary@8.1.2(transitive)
- Removedcspell-glob@8.1.2(transitive)
- Removedcspell-grammar@8.1.2(transitive)
- Removedcspell-io@8.1.2(transitive)
- Removedcspell-trie-lib@8.1.2(transitive)
Updated@cspell/cspell-pipe@8.1.3
Updated@cspell/cspell-types@8.1.3
Updated@cspell/dynamic-import@8.1.3
Updatedcspell-config-lib@8.1.3
Updatedcspell-dictionary@8.1.3
Updatedcspell-glob@8.1.3
Updatedcspell-grammar@8.1.3
Updatedcspell-io@8.1.3
Updatedcspell-trie-lib@8.1.3