@ms-cloudpack/path-string-parsing 1.2.2 → 1.2.4
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.
- package/lib/normalizeRelativePath.js +1 -1
- package/lib/normalizeRelativePath.js.map +1 -1
- package/lib/parseImportString.d.ts +1 -0
- package/lib/parseImportString.d.ts.map +1 -1
- package/lib/parseImportString.js +6 -1
- package/lib/parseImportString.js.map +1 -1
- package/lib/parseNamedImports.d.ts +2 -1
- package/lib/parseNamedImports.d.ts.map +1 -1
- package/lib/parseNamedImports.js +17 -12
- package/lib/parseNamedImports.js.map +1 -1
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +1 -1
|
@@ -32,6 +32,6 @@ export function normalizeRelativePath(originalPath) {
|
|
|
32
32
|
// entry points like "main" could also use a leading slash to indicate the package root.
|
|
33
33
|
return '.' + normalizedPath;
|
|
34
34
|
}
|
|
35
|
-
return normalizedPath
|
|
35
|
+
return /^\.\.?\//.test(normalizedPath) ? normalizedPath : `./${normalizedPath}`;
|
|
36
36
|
}
|
|
37
37
|
//# sourceMappingURL=normalizeRelativePath.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeRelativePath.js","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAgC;IACpE,IAAI,CAAC,YAAY,IAAI,YAAY,KAAK,GAAG,EAAE,CAAC;QAC1C,OAAO,GAAG,CAAC;IACb,CAAC;IAED,8FAA8F;IAC9F,gGAAgG;IAChG,+FAA+F;IAC/F,uFAAuF;IACvF,yFAAyF;IACzF,wFAAwF;IACxF,IAAI,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,iEAAiE,YAAY,KAAK;YAChF,kGAAkG;YAClG,gGAAgG,CACnG,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,cAAc,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;IAE3C,IAAI,cAAc,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;QAC9B,sFAAsF;QACtF,wFAAwF;QACxF,OAAO,GAAG,GAAG,cAAc,CAAC;IAC9B,CAAC;IAED,OAAO,
|
|
1
|
+
{"version":3,"file":"normalizeRelativePath.js","sourceRoot":"","sources":["../src/normalizeRelativePath.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,qBAAqB,CAAC,YAAgC;IACpE,IAAI,CAAC,YAAY,IAAI,YAAY,KAAK,GAAG,EAAE,CAAC;QAC1C,OAAO,GAAG,CAAC;IACb,CAAC;IAED,8FAA8F;IAC9F,gGAAgG;IAChG,+FAA+F;IAC/F,uFAAuF;IACvF,yFAAyF;IACzF,wFAAwF;IACxF,IAAI,iBAAiB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CACb,iEAAiE,YAAY,KAAK;YAChF,kGAAkG;YAClG,gGAAgG,CACnG,CAAC;IACJ,CAAC;IAED,oCAAoC;IACpC,MAAM,cAAc,GAAG,KAAK,CAAC,YAAY,CAAC,CAAC;IAE3C,IAAI,cAAc,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;QAC9B,sFAAsF;QACtF,wFAAwF;QACxF,OAAO,GAAG,GAAG,cAAc,CAAC;IAC9B,CAAC;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,KAAK,cAAc,EAAE,CAAC;AAClF,CAAC","sourcesContent":["import { slash } from './slash.js';\n\n/**\n * Normalize a path:\n * - If it's empty, undefined, or `.`, return `.`.\n * - If it's relative, normalize slashes and ensure it starts with `./`.\n * - If it starts with `/`, add a leading `.` (this is an absolute path but is sometimes treated\n * as package-relative)\n * - If it's an absolute Windows path (starts with a drive letter), throw an error.\n * - If it's absolute, throw an error (since this likely indicates a bug in the calling code).\n *\n * Note that this will NOT remove a trailing slash (if present).\n */\nexport function normalizeRelativePath(originalPath: string | undefined): string {\n if (!originalPath || originalPath === '.') {\n return '.';\n }\n\n // Throw on an absolute Windows path: this is definitely absolute regardless of the context it\n // came from, and if the calling code attempts to join it to another path as if it was relative,\n // that will have bad results. It's more likely on Windows that we could unintentionally end up\n // with an absolute path since there's no way to do relative paths across drive letters\n // (path.relative() will return an absolute path), so we throw here to make it clear that\n // Cloudpack needs to have specific handling for any cases where that might be possible.\n if (/^[a-zA-Z]:[\\\\/]/.test(originalPath)) {\n throw new Error(\n `Received an absolute path where a relative path was expected: ${originalPath}.\\n` +\n 'This indicates a bug in Cloudpack, likely due to calling path.relative() with two Windows paths ' +\n 'on different drives (which returns an absolute path) and then assuming the result is relative.',\n );\n }\n\n // Ensure we have the right slashes.\n const normalizedPath = slash(originalPath);\n\n if (normalizedPath[0] === '/') {\n // Technically a path starting with / is absolute, but it's possible that package.json\n // entry points like \"main\" could also use a leading slash to indicate the package root.\n return '.' + normalizedPath;\n }\n\n return /^\\.\\.?\\//.test(normalizedPath) ? normalizedPath : `./${normalizedPath}`;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseImportString.d.ts","sourceRoot":"","sources":["../src/parseImportString.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,SAAK,GAAG,kBAAkB,
|
|
1
|
+
{"version":3,"file":"parseImportString.d.ts","sourceRoot":"","sources":["../src/parseImportString.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,qDAAqD;IACrD,WAAW,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,OAAO,EAAE,MAAM,CAAC;IAChB;;;OAGG;IACH,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,SAAK,GAAG,kBAAkB,CAkBvE"}
|
package/lib/parseImportString.js
CHANGED
|
@@ -12,9 +12,14 @@ import { normalizeRelativePath } from './normalizeRelativePath.js';
|
|
|
12
12
|
* - `@foo/bar@1.2.3/path/to/file`
|
|
13
13
|
*/
|
|
14
14
|
export function parseImportString(importString = '') {
|
|
15
|
+
if (/^\.\.?\//.test(importString)) {
|
|
16
|
+
// If it's a relative path (with forward slash), return as-is, since it doesn't need normalization
|
|
17
|
+
// and won't have a version
|
|
18
|
+
return { packageName: '.', version: '', importPath: importString };
|
|
19
|
+
}
|
|
15
20
|
const matches = importString.match(
|
|
16
21
|
// 1: packageName 2: version 3: importPath
|
|
17
|
-
/^\/?((?:@[-\w.:]+\/)?[-\w.:]+)(?:@([-\w.]+))?(
|
|
22
|
+
/^\/?((?:@[-\w.:]+\/)?[-\w.:]+)(?:@([-\w.]+))?(?:([-\w./]+))?/) || [];
|
|
18
23
|
return {
|
|
19
24
|
packageName: matches[1] || '.',
|
|
20
25
|
version: matches[2] || '',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseImportString.js","sourceRoot":"","sources":["../src/parseImportString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"parseImportString.js","sourceRoot":"","sources":["../src/parseImportString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAcnE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAAC,YAAY,GAAG,EAAE;IACjD,IAAI,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;QAClC,kGAAkG;QAClG,2BAA2B;QAC3B,OAAO,EAAE,WAAW,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,CAAC;IACrE,CAAC;IAED,MAAM,OAAO,GACX,YAAY,CAAC,KAAK;IAChB,0DAA0D;IAC1D,8DAA8D,CAC/D,IAAI,EAAE,CAAC;IAEV,OAAO;QACL,WAAW,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,GAAG;QAC9B,OAAO,EAAE,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE;QACzB,UAAU,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;KAC9C,CAAC;AACJ,CAAC","sourcesContent":["import { normalizeRelativePath } from './normalizeRelativePath.js';\n\nexport type ImportStringResult = {\n /** Package name, or `'.'` for the current package */\n packageName: string;\n /** Optional package version (default: empty string) */\n version: string;\n /**\n * Optional import path (default: `'.'`).\n * If this is set to a non-default value, it will be normalized to start with `./`.\n */\n importPath: string;\n};\n\n/**\n * Parse an import path: usually this will be a standard JS import, but this function also allows\n * an optional `@version` suffix after the package name.\n *\n * Examples of supported import strings:\n * - `foo`\n * - `foo@1.2.3`\n * - `foo@1.2.3/path/to/file`\n * - `@foo/bar`\n * - `@foo/bar@1.2.3`\n * - `@foo/bar@1.2.3/path/to/file`\n */\nexport function parseImportString(importString = ''): ImportStringResult {\n if (/^\\.\\.?\\//.test(importString)) {\n // If it's a relative path (with forward slash), return as-is, since it doesn't need normalization\n // and won't have a version\n return { packageName: '.', version: '', importPath: importString };\n }\n\n const matches =\n importString.match(\n // 1: packageName 2: version 3: importPath\n /^\\/?((?:@[-\\w.:]+\\/)?[-\\w.:]+)(?:@([-\\w.]+))?(?:([-\\w./]+))?/,\n ) || [];\n\n return {\n packageName: matches[1] || '.',\n version: matches[2] || '',\n importPath: normalizeRelativePath(matches[3]),\n };\n}\n"]}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Parses
|
|
2
|
+
* Parses a JS import/export statement (from a path) into its component parts.
|
|
3
|
+
* Does NOT handle typescript (import/export type) or exports without paths.
|
|
3
4
|
* @param importLine - The import string to parse.
|
|
4
5
|
* @returns A string array with the named imports, default and/or '*'.
|
|
5
6
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseNamedImports.d.ts","sourceRoot":"","sources":["../src/parseNamedImports.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"parseNamedImports.d.ts","sourceRoot":"","sources":["../src/parseNamedImports.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAkC9D"}
|
package/lib/parseNamedImports.js
CHANGED
|
@@ -1,30 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Parses
|
|
2
|
+
* Parses a JS import/export statement (from a path) into its component parts.
|
|
3
|
+
* Does NOT handle typescript (import/export type) or exports without paths.
|
|
3
4
|
* @param importLine - The import string to parse.
|
|
4
5
|
* @returns A string array with the named imports, default and/or '*'.
|
|
5
6
|
*/
|
|
6
7
|
export function parseNamedImports(importLine) {
|
|
7
8
|
// Regex from: https://github.com/antonkc/MOR/blob/main/matchJsImports.md
|
|
8
|
-
// With some edits to catch
|
|
9
|
-
|
|
10
|
-
const
|
|
9
|
+
// With some edits to catch export * from "package" and removing lots of unneeded groups.
|
|
10
|
+
// (This won't handle comments; if that's ever needed, we should use acorn for full parsing.)
|
|
11
|
+
const matches = importLine.match(
|
|
12
|
+
// 1 default 2 * 3 names 4 path
|
|
13
|
+
/^\s*(?:import|export)\s*(?=[\s*{])\s*((?:[$\w]+(?:\s+as\s+[$\w]+)?(?=\s*,\s*[{*]|\s+from))?)[\s,]*((?:\*\s*as\s+[$\w]+)?|\*)[\s,]*(?:\{\s*((?:[$\w]+(?:\s+as\s+[$\w]+)?\s*,?\s*)*)\})?\s*from\s*["']([^"']*)/);
|
|
11
14
|
// If no matches return empty array
|
|
12
|
-
if (matches
|
|
13
|
-
return
|
|
15
|
+
if (!matches) {
|
|
16
|
+
return [];
|
|
14
17
|
}
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
const names = [];
|
|
19
|
+
// Default export
|
|
20
|
+
if (matches[1]) {
|
|
17
21
|
names.push('default');
|
|
18
22
|
}
|
|
19
|
-
//
|
|
20
|
-
if (matches[
|
|
23
|
+
// Imported or exported names
|
|
24
|
+
if (matches[3]) {
|
|
21
25
|
// Clean out the braces, whitespace, and get alias
|
|
22
|
-
const namedImports = matches[
|
|
26
|
+
const namedImports = matches[3].matchAll(/([\w$]+)(?:\s+as\s+[\w$]+,?)?/g);
|
|
23
27
|
for (const namedImport of namedImports) {
|
|
24
28
|
names.push(namedImport[1]);
|
|
25
29
|
}
|
|
26
30
|
}
|
|
27
|
-
|
|
31
|
+
// If there is a namespace export, push it to the names array
|
|
32
|
+
if (matches[2]) {
|
|
28
33
|
names.push('*');
|
|
29
34
|
}
|
|
30
35
|
return names;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseNamedImports.js","sourceRoot":"","sources":["../src/parseNamedImports.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"parseNamedImports.js","sourceRoot":"","sources":["../src/parseNamedImports.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,UAAkB;IAClD,yEAAyE;IACzE,yFAAyF;IACzF,6FAA6F;IAC7F,MAAM,OAAO,GAAG,UAAU,CAAC,KAAK;IAC9B,2MAA2M;IAC3M,8MAA8M,CAC/M,CAAC;IACF,mCAAmC;IACnC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,KAAK,GAAa,EAAE,CAAC;IAC3B,iBAAiB;IACjB,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACxB,CAAC;IAED,6BAA6B;IAC7B,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACf,kDAAkD;QAClD,MAAM,YAAY,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,gCAAgC,CAAC,CAAC;QAC3E,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;YACvC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAED,6DAA6D;IAC7D,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACf,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAClB,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["/**\n * Parses a JS import/export statement (from a path) into its component parts.\n * Does NOT handle typescript (import/export type) or exports without paths.\n * @param importLine - The import string to parse.\n * @returns A string array with the named imports, default and/or '*'.\n */\nexport function parseNamedImports(importLine: string): string[] {\n // Regex from: https://github.com/antonkc/MOR/blob/main/matchJsImports.md\n // With some edits to catch export * from \"package\" and removing lots of unneeded groups.\n // (This won't handle comments; if that's ever needed, we should use acorn for full parsing.)\n const matches = importLine.match(\n // 1 default 2 * 3 names 4 path\n /^\\s*(?:import|export)\\s*(?=[\\s*{])\\s*((?:[$\\w]+(?:\\s+as\\s+[$\\w]+)?(?=\\s*,\\s*[{*]|\\s+from))?)[\\s,]*((?:\\*\\s*as\\s+[$\\w]+)?|\\*)[\\s,]*(?:\\{\\s*((?:[$\\w]+(?:\\s+as\\s+[$\\w]+)?\\s*,?\\s*)*)\\})?\\s*from\\s*[\"']([^\"']*)/,\n );\n // If no matches return empty array\n if (!matches) {\n return [];\n }\n\n const names: string[] = [];\n // Default export\n if (matches[1]) {\n names.push('default');\n }\n\n // Imported or exported names\n if (matches[3]) {\n // Clean out the braces, whitespace, and get alias\n const namedImports = matches[3].matchAll(/([\\w$]+)(?:\\s+as\\s+[\\w$]+,?)?/g);\n for (const namedImport of namedImports) {\n names.push(namedImport[1]);\n }\n }\n\n // If there is a namespace export, push it to the names array\n if (matches[2]) {\n names.push('*');\n }\n\n return names;\n}\n"]}
|
package/lib/tsdoc-metadata.json
CHANGED