@knighted/module 1.0.0-beta.0 → 1.0.0-beta.1
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/dist/cjs/exports.d.cts +6 -0
- package/dist/cjs/format.cjs +45 -12
- package/dist/cjs/format.d.cts +3 -2
- package/dist/cjs/formatters/assignmentExpression.cjs +1 -1
- package/dist/cjs/formatters/identifier.cjs +4 -4
- package/dist/cjs/formatters/memberExpression.cjs +2 -2
- package/dist/cjs/identifiers.d.cts +19 -0
- package/dist/cjs/lang.d.cts +4 -0
- package/dist/cjs/module.cjs +4 -4
- package/dist/cjs/types.d.cts +8 -0
- package/dist/cjs/url.d.cts +2 -0
- package/dist/cjs/utils/exports.cjs +227 -0
- package/dist/cjs/utils/identifiers.cjs +190 -0
- package/dist/cjs/utils/lang.cjs +25 -0
- package/dist/cjs/utils/url.cjs +16 -0
- package/dist/cjs/utils.cjs +71 -1
- package/dist/cjs/utils.d.cts +3 -2
- package/dist/exports.d.ts +6 -0
- package/dist/format.d.ts +3 -2
- package/dist/format.js +42 -9
- package/dist/formatters/assignmentExpression.js +1 -1
- package/dist/formatters/identifier.js +2 -2
- package/dist/formatters/memberExpression.js +1 -1
- package/dist/identifiers.d.ts +19 -0
- package/dist/lang.d.ts +4 -0
- package/dist/module.js +3 -3
- package/dist/src/format.d.ts +9 -0
- package/dist/src/module.d.ts +3 -0
- package/dist/src/parse.d.ts +2 -0
- package/dist/src/types.d.ts +43 -0
- package/dist/src/utils/exports.d.ts +6 -0
- package/dist/src/utils/identifiers.d.ts +19 -0
- package/dist/src/utils/lang.d.ts +4 -0
- package/dist/src/utils/url.d.ts +2 -0
- package/dist/src/utils.d.ts +26 -0
- package/dist/src/walk.d.ts +20 -0
- package/dist/types.d.ts +8 -0
- package/dist/url.d.ts +2 -0
- package/dist/utils/exports.js +221 -0
- package/dist/utils/identifiers.js +183 -0
- package/dist/utils/lang.js +20 -0
- package/dist/utils/url.js +10 -0
- package/dist/utils.d.ts +3 -2
- package/dist/utils.js +70 -1
- package/package.json +4 -4
- /package/dist/{formatters → src/formatters}/assignmentExpression.d.ts +0 -0
- /package/dist/{formatters → src/formatters}/expressionStatement.d.ts +0 -0
- /package/dist/{formatters → src/formatters}/identifier.d.ts +0 -0
- /package/dist/{formatters → src/formatters}/memberExpression.d.ts +0 -0
- /package/dist/{formatters → src/formatters}/metaProperty.d.ts +0 -0
- /package/dist/{helpers → src/helpers}/identifier.d.ts +0 -0
- /package/dist/{helpers → src/helpers}/scope.d.ts +0 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isValidUrl = void 0;
|
|
7
|
+
// URL validation used when rewriting import.meta.url assignments.
|
|
8
|
+
const isValidUrl = url => {
|
|
9
|
+
try {
|
|
10
|
+
new URL(url);
|
|
11
|
+
return true;
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.isValidUrl = isValidUrl;
|
package/dist/cjs/utils.cjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.requireMainRgx = exports.isValidUrl = exports.getLangFromExt = exports.exportsRename = exports.collectScopeIdentifiers = exports.collectModuleIdentifiers = void 0;
|
|
6
|
+
exports.requireMainRgx = exports.isValidUrl = exports.getLangFromExt = exports.exportsRename = exports.collectScopeIdentifiers = exports.collectModuleIdentifiers = exports.collectCjsExports = void 0;
|
|
7
7
|
var _nodePath = require("node:path");
|
|
8
8
|
var _walk = require("./walk.cjs");
|
|
9
9
|
var _scope = require("./helpers/scope.cjs");
|
|
@@ -35,6 +35,76 @@ const isValidUrl = url => {
|
|
|
35
35
|
exports.isValidUrl = isValidUrl;
|
|
36
36
|
const exportsRename = exports.exportsRename = '__exports';
|
|
37
37
|
const requireMainRgx = exports.requireMainRgx = /(require\.main\s*===\s*module|module\s*===\s*require\.main)/g;
|
|
38
|
+
const resolveExportTarget = node => {
|
|
39
|
+
if (node.type !== 'MemberExpression') return null;
|
|
40
|
+
const base = node.object;
|
|
41
|
+
const prop = node.property;
|
|
42
|
+
if (prop.type !== 'Identifier') return null;
|
|
43
|
+
if (base.type === 'Identifier' && base.name === 'exports') {
|
|
44
|
+
return {
|
|
45
|
+
key: prop.name,
|
|
46
|
+
via: 'exports'
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (base.type === 'MemberExpression' && base.object.type === 'Identifier' && base.object.name === 'module' && base.property.type === 'Identifier' && base.property.name === 'exports') {
|
|
50
|
+
return {
|
|
51
|
+
key: prop.name,
|
|
52
|
+
via: 'module.exports'
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (base.type === 'Identifier' && base.name === 'module' && prop.type === 'Identifier' && prop.name === 'exports') {
|
|
56
|
+
return {
|
|
57
|
+
key: 'default',
|
|
58
|
+
via: 'module.exports'
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
return null;
|
|
62
|
+
};
|
|
63
|
+
const collectCjsExports = async ast => {
|
|
64
|
+
const exportsMap = new Map();
|
|
65
|
+
const localToExport = new Map();
|
|
66
|
+
await (0, _walk.ancestorWalk)(ast, {
|
|
67
|
+
enter(node) {
|
|
68
|
+
if (node.type === 'AssignmentExpression') {
|
|
69
|
+
const target = resolveExportTarget(node.left);
|
|
70
|
+
if (target) {
|
|
71
|
+
const entry = exportsMap.get(target.key) ?? {
|
|
72
|
+
key: target.key,
|
|
73
|
+
writes: [],
|
|
74
|
+
via: new Set(),
|
|
75
|
+
reassignments: []
|
|
76
|
+
};
|
|
77
|
+
entry.via.add(target.via);
|
|
78
|
+
entry.writes.push(node);
|
|
79
|
+
if (node.right.type === 'Identifier') {
|
|
80
|
+
entry.fromIdentifier ??= node.right.name;
|
|
81
|
+
if (entry.fromIdentifier) {
|
|
82
|
+
const set = localToExport.get(entry.fromIdentifier) ?? new Set();
|
|
83
|
+
set.add(target.key);
|
|
84
|
+
localToExport.set(entry.fromIdentifier, set);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exportsMap.set(target.key, entry);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (node.left.type === 'Identifier') {
|
|
91
|
+
const keys = localToExport.get(node.left.name);
|
|
92
|
+
if (keys) {
|
|
93
|
+
keys.forEach(key => {
|
|
94
|
+
const entry = exportsMap.get(key);
|
|
95
|
+
if (entry) {
|
|
96
|
+
entry.reassignments.push(node);
|
|
97
|
+
exportsMap.set(key, entry);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
return exportsMap;
|
|
106
|
+
};
|
|
107
|
+
exports.collectCjsExports = collectCjsExports;
|
|
38
108
|
const collectScopeIdentifiers = (node, scopes) => {
|
|
39
109
|
const {
|
|
40
110
|
type
|
package/dist/cjs/utils.d.cts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Node } from 'oxc-parser';
|
|
2
2
|
import type { Specifier } from '@knighted/specifier';
|
|
3
|
-
import type { IdentMeta, Scope } from './types.cjs';
|
|
3
|
+
import type { IdentMeta, Scope, CjsExport } from './types.cjs';
|
|
4
4
|
type UpdateSrcLang = Parameters<Specifier['updateSrc']>[1];
|
|
5
5
|
declare const getLangFromExt: (filename: string) => UpdateSrcLang;
|
|
6
6
|
declare const isValidUrl: (url: string) => boolean;
|
|
7
7
|
declare const exportsRename = "__exports";
|
|
8
8
|
declare const requireMainRgx: RegExp;
|
|
9
|
+
declare const collectCjsExports: (ast: Node) => Promise<Map<string, CjsExport>>;
|
|
9
10
|
declare const collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
|
|
10
11
|
/**
|
|
11
12
|
* Collects all module scope identifiers in the AST.
|
|
@@ -22,4 +23,4 @@ declare const collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
|
|
|
22
23
|
* which are also valid module scope identifiers.
|
|
23
24
|
*/
|
|
24
25
|
declare const collectModuleIdentifiers: (ast: Node, hoisting?: boolean) => Promise<Map<string, IdentMeta>>;
|
|
25
|
-
export { getLangFromExt, isValidUrl, collectScopeIdentifiers, collectModuleIdentifiers, exportsRename, requireMainRgx, };
|
|
26
|
+
export { getLangFromExt, isValidUrl, collectScopeIdentifiers, collectModuleIdentifiers, collectCjsExports, exportsRename, requireMainRgx, };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { CjsExport } from '../types.js';
|
|
3
|
+
declare const exportsRename = "__exports";
|
|
4
|
+
declare const requireMainRgx: RegExp;
|
|
5
|
+
declare const collectCjsExports: (ast: Node) => Promise<Map<string, CjsExport>>;
|
|
6
|
+
export { exportsRename, requireMainRgx, collectCjsExports };
|
package/dist/format.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type { ParseResult } from 'oxc-parser';
|
|
2
2
|
import type { FormatterOptions } from './types.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Node added support for import.meta.main.
|
|
5
|
+
* Added in: v24.2.0, v22.18.0
|
|
6
|
+
* @see https://nodejs.org/api/esm.html#importmetamain
|
|
6
7
|
*/
|
|
7
8
|
declare const format: (src: string, ast: ParseResult, opts: FormatterOptions) => Promise<string>;
|
|
8
9
|
export { format };
|
package/dist/format.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import MagicString from 'magic-string';
|
|
2
|
-
import { identifier } from '
|
|
3
|
-
import { metaProperty } from '
|
|
4
|
-
import { memberExpression } from '
|
|
5
|
-
import { assignmentExpression } from '
|
|
6
|
-
import { isValidUrl
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { identifier } from '#formatters/identifier.js';
|
|
3
|
+
import { metaProperty } from '#formatters/metaProperty.js';
|
|
4
|
+
import { memberExpression } from '#formatters/memberExpression.js';
|
|
5
|
+
import { assignmentExpression } from '#formatters/assignmentExpression.js';
|
|
6
|
+
import { isValidUrl } from '#utils/url.js';
|
|
7
|
+
import { exportsRename, collectCjsExports } from '#utils/exports.js';
|
|
8
|
+
import { collectModuleIdentifiers } from '#utils/identifiers.js';
|
|
9
|
+
import { isIdentifierName } from '#helpers/identifier.js';
|
|
10
|
+
import { ancestorWalk } from '#walk';
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
+
* Node added support for import.meta.main.
|
|
14
|
+
* Added in: v24.2.0, v22.18.0
|
|
15
|
+
* @see https://nodejs.org/api/esm.html#importmetamain
|
|
13
16
|
*/
|
|
14
17
|
const format = async (src, ast, opts) => {
|
|
15
18
|
const code = new MagicString(src);
|
|
@@ -19,6 +22,7 @@ const format = async (src, ast, opts) => {
|
|
|
19
22
|
hasDefaultExportBeenReassigned: false,
|
|
20
23
|
hasDefaultExportBeenAssigned: false
|
|
21
24
|
};
|
|
25
|
+
const exportTable = opts.target === 'module' ? await collectCjsExports(ast.program) : null;
|
|
22
26
|
await collectModuleIdentifiers(ast.program);
|
|
23
27
|
if (opts.target === 'module' && opts.transformSyntax) {
|
|
24
28
|
/**
|
|
@@ -104,6 +108,35 @@ void import.meta.filename;
|
|
|
104
108
|
}
|
|
105
109
|
}
|
|
106
110
|
});
|
|
111
|
+
if (opts.target === 'module' && opts.transformSyntax && exportTable) {
|
|
112
|
+
const isValidExportName = name => /^[$A-Z_a-z][$\w]*$/.test(name);
|
|
113
|
+
const asExportName = name => isValidExportName(name) ? name : JSON.stringify(name);
|
|
114
|
+
const accessProp = name => isValidExportName(name) ? `${exportsRename}.${name}` : `${exportsRename}[${JSON.stringify(name)}]`;
|
|
115
|
+
const tempNameFor = name => {
|
|
116
|
+
const sanitized = name.replace(/[^$\w]/g, '_') || 'value';
|
|
117
|
+
const safe = /^[0-9]/.test(sanitized) ? `_${sanitized}` : sanitized;
|
|
118
|
+
return `__export_${safe}`;
|
|
119
|
+
};
|
|
120
|
+
const lines = [];
|
|
121
|
+
const defaultEntry = exportTable.get('default');
|
|
122
|
+
if (defaultEntry) {
|
|
123
|
+
const def = defaultEntry.fromIdentifier ?? exportsRename;
|
|
124
|
+
lines.push(`export default ${def};`);
|
|
125
|
+
}
|
|
126
|
+
for (const [key, entry] of exportTable) {
|
|
127
|
+
if (key === 'default') continue;
|
|
128
|
+
if (entry.fromIdentifier) {
|
|
129
|
+
lines.push(`export { ${entry.fromIdentifier} as ${asExportName(key)} };`);
|
|
130
|
+
} else {
|
|
131
|
+
const temp = tempNameFor(key);
|
|
132
|
+
lines.push(`const ${temp} = ${accessProp(key)};`);
|
|
133
|
+
lines.push(`export { ${temp} as ${asExportName(key)} };`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (lines.length) {
|
|
137
|
+
code.append(`\n${lines.join('\n')}\n`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
107
140
|
return code.toString();
|
|
108
141
|
};
|
|
109
142
|
export { format };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { exportsRename } from '
|
|
2
|
-
import { identifier as ident } from '
|
|
1
|
+
import { exportsRename } from '#utils/exports.js';
|
|
2
|
+
import { identifier as ident } from '#helpers/identifier.js';
|
|
3
3
|
export const identifier = ({
|
|
4
4
|
node,
|
|
5
5
|
ancestors,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { exportsRename } from '
|
|
1
|
+
import { exportsRename } from '#utils/exports.js';
|
|
2
2
|
export const memberExpression = (node, parent, src, options) => {
|
|
3
3
|
if (options.target === 'module') {
|
|
4
4
|
if (node.object.type === 'Identifier' && node.property.type === 'Identifier' && node.object.name === 'module' && node.property.name === 'exports') {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { IdentMeta, Scope } from '../types.js';
|
|
3
|
+
declare const collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Collects all module scope identifiers in the AST.
|
|
6
|
+
*
|
|
7
|
+
* Ignores identifiers that are in functions or classes.
|
|
8
|
+
* Ignores new scopes for StaticBlock nodes (can only reference static class members).
|
|
9
|
+
*
|
|
10
|
+
* Special case handling for these which create their own scopes,
|
|
11
|
+
* but are also valid module scope identifiers:
|
|
12
|
+
* - ClassDeclaration
|
|
13
|
+
* - FunctionDeclaration
|
|
14
|
+
*
|
|
15
|
+
* Special case handling for var inside BlockStatement
|
|
16
|
+
* which are also valid module scope identifiers.
|
|
17
|
+
*/
|
|
18
|
+
declare const collectModuleIdentifiers: (ast: Node, hoisting?: boolean) => Promise<Map<string, IdentMeta>>;
|
|
19
|
+
export { collectScopeIdentifiers, collectModuleIdentifiers };
|
package/dist/lang.d.ts
ADDED
package/dist/module.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { resolve } from 'node:path';
|
|
2
2
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { specifier } from '@knighted/specifier';
|
|
4
|
-
import { parse } from '
|
|
5
|
-
import { format } from '
|
|
6
|
-
import { getLangFromExt } from '
|
|
4
|
+
import { parse } from '#parse';
|
|
5
|
+
import { format } from '#format';
|
|
6
|
+
import { getLangFromExt } from '#utils/lang.js';
|
|
7
7
|
const defaultOptions = {
|
|
8
8
|
target: 'commonjs',
|
|
9
9
|
sourceType: 'auto',
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ParseResult } from 'oxc-parser';
|
|
2
|
+
import type { FormatterOptions } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Node added support for import.meta.main.
|
|
5
|
+
* Added in: v24.2.0, v22.18.0
|
|
6
|
+
* @see https://nodejs.org/api/esm.html#importmetamain
|
|
7
|
+
*/
|
|
8
|
+
declare const format: (src: string, ast: ParseResult, opts: FormatterOptions) => Promise<string>;
|
|
9
|
+
export { format };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Node, Span, IdentifierName, IdentifierReference, BindingIdentifier, LabelIdentifier, TSIndexSignatureName } from 'oxc-parser';
|
|
2
|
+
export type RewriteSpecifier = '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts' | ((value: string) => string | null | undefined);
|
|
3
|
+
export type ModuleOptions = {
|
|
4
|
+
target: 'module' | 'commonjs';
|
|
5
|
+
sourceType?: 'auto' | 'module' | 'commonjs';
|
|
6
|
+
transformSyntax?: boolean;
|
|
7
|
+
liveBindings?: 'strict' | 'loose' | 'off';
|
|
8
|
+
rewriteSpecifier?: RewriteSpecifier;
|
|
9
|
+
dirFilename?: 'inject' | 'preserve' | 'error';
|
|
10
|
+
importMeta?: 'preserve' | 'shim' | 'error';
|
|
11
|
+
requireSource?: 'builtin' | 'create-require';
|
|
12
|
+
cjsDefault?: 'module-exports' | 'auto' | 'none';
|
|
13
|
+
topLevelAwait?: 'error' | 'wrap' | 'preserve';
|
|
14
|
+
out?: string;
|
|
15
|
+
inPlace?: boolean;
|
|
16
|
+
};
|
|
17
|
+
export type SpannedNode = Node & Span;
|
|
18
|
+
export type ExportsMeta = {
|
|
19
|
+
hasExportsBeenReassigned: boolean;
|
|
20
|
+
hasDefaultExportBeenReassigned: boolean;
|
|
21
|
+
hasDefaultExportBeenAssigned: boolean;
|
|
22
|
+
defaultExportValue: unknown;
|
|
23
|
+
};
|
|
24
|
+
export type CjsExport = {
|
|
25
|
+
key: string;
|
|
26
|
+
writes: SpannedNode[];
|
|
27
|
+
fromIdentifier?: string;
|
|
28
|
+
via: Set<'exports' | 'module.exports'>;
|
|
29
|
+
reassignments: SpannedNode[];
|
|
30
|
+
hasGetter?: boolean;
|
|
31
|
+
};
|
|
32
|
+
export type IdentMeta = {
|
|
33
|
+
declare: SpannedNode[];
|
|
34
|
+
read: SpannedNode[];
|
|
35
|
+
};
|
|
36
|
+
export type Scope = {
|
|
37
|
+
type: string;
|
|
38
|
+
name: string;
|
|
39
|
+
node: Node;
|
|
40
|
+
idents: Set<string>;
|
|
41
|
+
};
|
|
42
|
+
export type FormatterOptions = Omit<ModuleOptions, 'out' | 'inPlace'>;
|
|
43
|
+
export type Identifier = IdentifierName | IdentifierReference | BindingIdentifier | LabelIdentifier | TSIndexSignatureName;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { CjsExport } from '../types.js';
|
|
3
|
+
declare const exportsRename = "__exports";
|
|
4
|
+
declare const requireMainRgx: RegExp;
|
|
5
|
+
declare const collectCjsExports: (ast: Node) => Promise<Map<string, CjsExport>>;
|
|
6
|
+
export { exportsRename, requireMainRgx, collectCjsExports };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { IdentMeta, Scope } from '../types.js';
|
|
3
|
+
declare const collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
|
|
4
|
+
/**
|
|
5
|
+
* Collects all module scope identifiers in the AST.
|
|
6
|
+
*
|
|
7
|
+
* Ignores identifiers that are in functions or classes.
|
|
8
|
+
* Ignores new scopes for StaticBlock nodes (can only reference static class members).
|
|
9
|
+
*
|
|
10
|
+
* Special case handling for these which create their own scopes,
|
|
11
|
+
* but are also valid module scope identifiers:
|
|
12
|
+
* - ClassDeclaration
|
|
13
|
+
* - FunctionDeclaration
|
|
14
|
+
*
|
|
15
|
+
* Special case handling for var inside BlockStatement
|
|
16
|
+
* which are also valid module scope identifiers.
|
|
17
|
+
*/
|
|
18
|
+
declare const collectModuleIdentifiers: (ast: Node, hoisting?: boolean) => Promise<Map<string, IdentMeta>>;
|
|
19
|
+
export { collectScopeIdentifiers, collectModuleIdentifiers };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
import type { Specifier } from '@knighted/specifier';
|
|
3
|
+
import type { IdentMeta, Scope, CjsExport } from './types.js';
|
|
4
|
+
type UpdateSrcLang = Parameters<Specifier['updateSrc']>[1];
|
|
5
|
+
declare const getLangFromExt: (filename: string) => UpdateSrcLang;
|
|
6
|
+
declare const isValidUrl: (url: string) => boolean;
|
|
7
|
+
declare const exportsRename = "__exports";
|
|
8
|
+
declare const requireMainRgx: RegExp;
|
|
9
|
+
declare const collectCjsExports: (ast: Node) => Promise<Map<string, CjsExport>>;
|
|
10
|
+
declare const collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
|
|
11
|
+
/**
|
|
12
|
+
* Collects all module scope identifiers in the AST.
|
|
13
|
+
*
|
|
14
|
+
* Ignores identifiers that are in functions or classes.
|
|
15
|
+
* Ignores new scopes for StaticBlock nodes (can only reference static class members).
|
|
16
|
+
*
|
|
17
|
+
* Special case handling for these which create their own scopes,
|
|
18
|
+
* but are also valid module scope identifiers:
|
|
19
|
+
* - ClassDeclaration
|
|
20
|
+
* - FunctionDeclaration
|
|
21
|
+
*
|
|
22
|
+
* Special case handling for var inside BlockStatement
|
|
23
|
+
* which are also valid module scope identifiers.
|
|
24
|
+
*/
|
|
25
|
+
declare const collectModuleIdentifiers: (ast: Node, hoisting?: boolean) => Promise<Map<string, IdentMeta>>;
|
|
26
|
+
export { getLangFromExt, isValidUrl, collectScopeIdentifiers, collectModuleIdentifiers, collectCjsExports, exportsRename, requireMainRgx, };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Node } from 'oxc-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Using visitorKeys instead of oxc Visitor to keep
|
|
4
|
+
* an ancestor-aware enter/leave API with this.skip()
|
|
5
|
+
* without per-node method boilerplate.
|
|
6
|
+
*/
|
|
7
|
+
type AncestorContext = {
|
|
8
|
+
skip: () => void;
|
|
9
|
+
};
|
|
10
|
+
type AncestorVisitor = {
|
|
11
|
+
enter?: (this: AncestorContext, node: Node, ancestors: Node[]) => void | Promise<void>;
|
|
12
|
+
leave?: (this: AncestorContext, node: Node, ancestors: Node[]) => void | Promise<void>;
|
|
13
|
+
};
|
|
14
|
+
type WalkVisitor = {
|
|
15
|
+
enter?: (this: AncestorContext, node: Node, parent: Node | null) => void | Promise<void>;
|
|
16
|
+
leave?: (this: AncestorContext, node: Node, parent: Node | null) => void | Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
declare const ancestorWalk: (node: Node, visitors: AncestorVisitor) => Promise<void>;
|
|
19
|
+
declare const walk: (node: Node, visitors: WalkVisitor) => Promise<void>;
|
|
20
|
+
export { ancestorWalk, walk };
|
package/dist/types.d.ts
CHANGED
|
@@ -21,6 +21,14 @@ export type ExportsMeta = {
|
|
|
21
21
|
hasDefaultExportBeenAssigned: boolean;
|
|
22
22
|
defaultExportValue: unknown;
|
|
23
23
|
};
|
|
24
|
+
export type CjsExport = {
|
|
25
|
+
key: string;
|
|
26
|
+
writes: SpannedNode[];
|
|
27
|
+
fromIdentifier?: string;
|
|
28
|
+
via: Set<'exports' | 'module.exports'>;
|
|
29
|
+
reassignments: SpannedNode[];
|
|
30
|
+
hasGetter?: boolean;
|
|
31
|
+
};
|
|
24
32
|
export type IdentMeta = {
|
|
25
33
|
declare: SpannedNode[];
|
|
26
34
|
read: SpannedNode[];
|
package/dist/url.d.ts
ADDED