@knighted/module 1.0.0-alpha.9 → 1.0.0-beta.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.
Files changed (55) hide show
  1. package/README.md +28 -16
  2. package/dist/assignmentExpression.d.ts +12 -0
  3. package/dist/cjs/assignmentExpression.d.cts +12 -0
  4. package/dist/cjs/expressionStatement.d.cts +2 -3
  5. package/dist/cjs/format.cjs +96 -22
  6. package/dist/cjs/format.d.cts +3 -4
  7. package/dist/cjs/formatters/assignmentExpression.cjs +37 -0
  8. package/dist/cjs/formatters/expressionStatement.cjs +36 -55
  9. package/dist/cjs/formatters/identifier.cjs +31 -31
  10. package/dist/cjs/formatters/memberExpression.cjs +24 -11
  11. package/dist/cjs/formatters/metaProperty.cjs +23 -35
  12. package/dist/cjs/helpers/identifier.cjs +132 -0
  13. package/dist/cjs/helpers/scope.cjs +12 -0
  14. package/dist/cjs/identifier.d.cts +31 -5
  15. package/dist/cjs/memberExpression.d.cts +2 -3
  16. package/dist/cjs/metaProperty.d.cts +2 -3
  17. package/dist/cjs/module.cjs +24 -25
  18. package/dist/cjs/parse.cjs +3 -14
  19. package/dist/cjs/parse.d.cts +1 -1
  20. package/dist/cjs/scope.d.cts +6 -0
  21. package/dist/cjs/types.d.cts +32 -4
  22. package/dist/cjs/utils.cjs +218 -0
  23. package/dist/cjs/utils.d.cts +25 -0
  24. package/dist/cjs/walk.cjs +75 -0
  25. package/dist/cjs/walk.d.cts +20 -0
  26. package/dist/expressionStatement.d.ts +2 -3
  27. package/dist/format.d.ts +3 -4
  28. package/dist/format.js +98 -23
  29. package/dist/formatters/assignmentExpression.d.ts +12 -0
  30. package/dist/formatters/assignmentExpression.js +30 -0
  31. package/dist/formatters/expressionStatement.d.ts +2 -3
  32. package/dist/formatters/expressionStatement.js +36 -55
  33. package/dist/formatters/identifier.d.ts +11 -4
  34. package/dist/formatters/identifier.js +31 -31
  35. package/dist/formatters/memberExpression.d.ts +2 -3
  36. package/dist/formatters/memberExpression.js +24 -11
  37. package/dist/formatters/metaProperty.d.ts +2 -3
  38. package/dist/formatters/metaProperty.js +23 -35
  39. package/dist/helpers/identifier.d.ts +31 -0
  40. package/dist/helpers/identifier.js +127 -0
  41. package/dist/helpers/scope.d.ts +6 -0
  42. package/dist/helpers/scope.js +7 -0
  43. package/dist/identifier.d.ts +31 -5
  44. package/dist/memberExpression.d.ts +2 -3
  45. package/dist/metaProperty.d.ts +2 -3
  46. package/dist/module.js +24 -25
  47. package/dist/parse.d.ts +1 -1
  48. package/dist/parse.js +3 -14
  49. package/dist/scope.d.ts +6 -0
  50. package/dist/types.d.ts +32 -4
  51. package/dist/utils.d.ts +25 -0
  52. package/dist/utils.js +209 -0
  53. package/dist/walk.d.ts +20 -0
  54. package/dist/walk.js +69 -0
  55. package/package.json +43 -25
@@ -0,0 +1,127 @@
1
+ import { analyze } from 'periscopic';
2
+
3
+ /**
4
+ * Focus exclusively on IdentifierName type as it has the name property,
5
+ * which is what the identifer utilities are interested in.
6
+ *
7
+ * Explicitly ignore the TSThisParameter type as it is not a valid identifier name.
8
+ */
9
+ const isIdentifierName = node => {
10
+ return node.type === 'Identifier' && typeof node.name === 'string' && node.name !== 'this';
11
+ };
12
+ const scopeCache = new WeakMap();
13
+ const getScopeContext = program => {
14
+ const cached = scopeCache.get(program);
15
+ if (cached) {
16
+ return cached;
17
+ }
18
+ const {
19
+ scope
20
+ } = analyze(program);
21
+ const context = {
22
+ scope
23
+ };
24
+ scopeCache.set(program, context);
25
+ return context;
26
+ };
27
+
28
+ /**
29
+ * All methods receive the full set of ancestors, which
30
+ * specifically includes the node itself as the last element.
31
+ * The second to last element is the parent node, and so on.
32
+ * The first element is the root node.
33
+ */
34
+ const identifier = {
35
+ isNamed: node => {
36
+ return isIdentifierName(node);
37
+ },
38
+ isMetaProperty(ancestors) {
39
+ const parent = ancestors[ancestors.length - 2];
40
+ return parent.type === 'MetaProperty' || parent.type === 'MemberExpression' && parent.object.type === 'MetaProperty';
41
+ },
42
+ isModuleScope(ancestors, includeImports = false) {
43
+ const node = ancestors[ancestors.length - 1];
44
+ const parent = ancestors[ancestors.length - 2];
45
+ const program = ancestors[0];
46
+ if (!identifier.isNamed(node) || identifier.isMetaProperty(ancestors) || parent.type === 'LabeledStatement' || parent.type === 'BreakStatement' || parent.type === 'ContinueStatement') {
47
+ return false;
48
+ }
49
+ if (parent.type === 'ImportSpecifier' || parent.type === 'ImportDefaultSpecifier' || parent.type === 'ImportNamespaceSpecifier') {
50
+ return includeImports && parent.local.name === node.name;
51
+ }
52
+ if (parent.type === 'Property' && parent.key === node && !parent.computed) {
53
+ return false;
54
+ }
55
+ if (parent.type === 'MemberExpression' && parent.property === node && !parent.computed) {
56
+ return false;
57
+ }
58
+ const {
59
+ scope: rootScope
60
+ } = getScopeContext(program);
61
+ const owner = rootScope.find_owner(node.name);
62
+ if (!owner) {
63
+ return node.name === 'exports';
64
+ }
65
+ return owner === rootScope;
66
+ },
67
+ isMemberExpressionRoot(ancestors) {
68
+ const node = ancestors[ancestors.length - 1];
69
+ const parent = ancestors[ancestors.length - 2];
70
+ const grandParent = ancestors[ancestors.length - 3];
71
+ return parent.type === 'MemberExpression' && parent.object === node && grandParent.type !== 'MemberExpression';
72
+ },
73
+ isDeclaration(ancestors) {
74
+ const node = ancestors[ancestors.length - 1];
75
+ const parent = ancestors[ancestors.length - 2];
76
+ return (parent.type === 'VariableDeclarator' || parent.type === 'FunctionDeclaration' || parent.type === 'ClassDeclaration') && parent.id === node;
77
+ },
78
+ isClassOrFuncDeclarationId(ancestors) {
79
+ const node = ancestors[ancestors.length - 1];
80
+ const parent = ancestors[ancestors.length - 2];
81
+ return (parent.type === 'ClassDeclaration' || parent.type === 'FunctionDeclaration') && parent.id === node && ancestors.length <= 3;
82
+ },
83
+ isVarDeclarationInGlobalScope(ancestors) {
84
+ const node = ancestors[ancestors.length - 1];
85
+ const parent = ancestors[ancestors.length - 2];
86
+ const grandParent = ancestors[ancestors.length - 3];
87
+ const varBoundScopes = ['ClassDeclaration', 'ClassExpression', 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression'];
88
+ return parent.type === 'VariableDeclarator' && parent.id === node && grandParent.type === 'VariableDeclaration' && grandParent.kind === 'var' && ancestors.every(ancestor => {
89
+ return !varBoundScopes.includes(ancestor.type);
90
+ });
91
+ },
92
+ isIife(ancestors) {
93
+ const parent = ancestors[ancestors.length - 2];
94
+ return parent.type === 'FunctionExpression' && ancestors.some(ancestor => ancestor.type === 'ParenthesizedExpression');
95
+ },
96
+ isFunctionExpressionId(ancestors) {
97
+ const node = ancestors[ancestors.length - 1];
98
+ const parent = ancestors[ancestors.length - 2];
99
+ return parent.type === 'FunctionExpression' && parent.id === node;
100
+ },
101
+ isExportSpecifierAlias(ancestors) {
102
+ const node = ancestors[ancestors.length - 1];
103
+ const parent = ancestors[ancestors.length - 2];
104
+ return parent.type === 'ExportSpecifier' && parent.exported === node;
105
+ },
106
+ isClassPropertyKey(ancestors) {
107
+ const node = ancestors[ancestors.length - 1];
108
+ const parent = ancestors[ancestors.length - 2];
109
+ return parent.type === 'PropertyDefinition' && parent.key === node;
110
+ },
111
+ isMethodDefinitionKey(ancestors) {
112
+ const node = ancestors[ancestors.length - 1];
113
+ const parent = ancestors[ancestors.length - 2];
114
+ return parent.type === 'MethodDefinition' && parent.key === node;
115
+ },
116
+ isMemberKey(ancestors) {
117
+ const node = ancestors[ancestors.length - 1];
118
+ const parent = ancestors[ancestors.length - 2];
119
+ return parent.type === 'MemberExpression' && parent.property === node;
120
+ },
121
+ isPropertyKey(ancestors) {
122
+ const node = ancestors[ancestors.length - 1];
123
+ const parent = ancestors[ancestors.length - 2];
124
+ return parent.type === 'Property' && parent.key === node;
125
+ }
126
+ };
127
+ export { identifier, isIdentifierName };
@@ -0,0 +1,6 @@
1
+ import type { Node } from 'oxc-parser';
2
+ declare const scopes: string[];
3
+ declare const scope: {
4
+ isScope(node: Node): boolean;
5
+ };
6
+ export { scopes, scope };
@@ -0,0 +1,7 @@
1
+ const scopes = ['BlockStatement', 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'ClassDeclaration', 'ClassExpression', 'ClassBody', 'StaticBlock'];
2
+ const scope = {
3
+ isScope(node) {
4
+ return scopes.includes(node.type);
5
+ }
6
+ };
7
+ export { scopes, scope };
@@ -1,5 +1,31 @@
1
- import MagicString from 'magic-string';
2
- import type { NodePath } from '@babel/traverse';
3
- import type { Identifier } from '@babel/types';
4
- import type { FormatterOptions } from '../types.js';
5
- export declare const identifier: (nodePath: NodePath<Identifier>, src: MagicString, options: FormatterOptions) => void;
1
+ import type { Node, IdentifierName } from 'oxc-parser';
2
+ /**
3
+ * Focus exclusively on IdentifierName type as it has the name property,
4
+ * which is what the identifer utilities are interested in.
5
+ *
6
+ * Explicitly ignore the TSThisParameter type as it is not a valid identifier name.
7
+ */
8
+ declare const isIdentifierName: (node: Node) => node is IdentifierName;
9
+ /**
10
+ * All methods receive the full set of ancestors, which
11
+ * specifically includes the node itself as the last element.
12
+ * The second to last element is the parent node, and so on.
13
+ * The first element is the root node.
14
+ */
15
+ declare const identifier: {
16
+ isNamed: (node: Node) => node is IdentifierName;
17
+ isMetaProperty(ancestors: Node[]): boolean;
18
+ isModuleScope(ancestors: Node[], includeImports?: boolean): boolean;
19
+ isMemberExpressionRoot(ancestors: Node[]): boolean;
20
+ isDeclaration(ancestors: Node[]): boolean;
21
+ isClassOrFuncDeclarationId(ancestors: Node[]): boolean;
22
+ isVarDeclarationInGlobalScope(ancestors: Node[]): boolean;
23
+ isIife(ancestors: Node[]): boolean;
24
+ isFunctionExpressionId(ancestors: Node[]): boolean;
25
+ isExportSpecifierAlias(ancestors: Node[]): boolean;
26
+ isClassPropertyKey(ancestors: Node[]): boolean;
27
+ isMethodDefinitionKey(ancestors: Node[]): boolean;
28
+ isMemberKey(ancestors: Node[]): boolean;
29
+ isPropertyKey(ancestors: Node[]): boolean;
30
+ };
31
+ export { identifier, isIdentifierName };
@@ -1,5 +1,4 @@
1
1
  import MagicString from 'magic-string';
2
- import type { NodePath } from '@babel/traverse';
3
- import type { MemberExpression } from '@babel/types';
2
+ import type { MemberExpression, Node } from 'oxc-parser';
4
3
  import type { FormatterOptions } from '../types.js';
5
- export declare const memberExpression: (nodePath: NodePath<MemberExpression>, src: MagicString, options: FormatterOptions) => void;
4
+ export declare const memberExpression: (node: MemberExpression, parent: Node | null, src: MagicString, options: FormatterOptions) => void;
@@ -1,5 +1,4 @@
1
1
  import MagicString from 'magic-string';
2
- import type { NodePath } from '@babel/traverse';
3
- import type { MetaProperty } from '@babel/types';
2
+ import type { Node, MetaProperty } from 'oxc-parser';
4
3
  import type { FormatterOptions } from '../types.js';
5
- export declare const metaProperty: (nodePath: NodePath<MetaProperty>, src: MagicString, options: FormatterOptions) => void;
4
+ export declare const metaProperty: (node: MetaProperty, parent: Node | null, src: MagicString, options: FormatterOptions) => void;
package/dist/module.js CHANGED
@@ -1,28 +1,22 @@
1
- import { resolve, extname } from 'node:path';
1
+ import { resolve } from 'node:path';
2
2
  import { readFile, writeFile } from 'node:fs/promises';
3
3
  import { specifier } from '@knighted/specifier';
4
4
  import { parse } from './parse.js';
5
5
  import { format } from './format.js';
6
+ import { getLangFromExt } from './utils.js';
6
7
  const defaultOptions = {
7
- type: 'commonjs',
8
+ target: 'commonjs',
9
+ sourceType: 'auto',
10
+ transformSyntax: true,
11
+ liveBindings: 'strict',
12
+ rewriteSpecifier: undefined,
13
+ dirFilename: 'inject',
14
+ importMeta: 'shim',
15
+ requireSource: 'builtin',
16
+ cjsDefault: 'auto',
17
+ topLevelAwait: 'error',
8
18
  out: undefined,
9
- modules: false,
10
- specifier: undefined
11
- };
12
- const getLangFromExt = filename => {
13
- const ext = extname(filename);
14
- if (/\.js$/.test(ext)) {
15
- return 'js';
16
- }
17
- if (/\.ts$/.test(ext)) {
18
- return 'ts';
19
- }
20
- if (ext === '.tsx') {
21
- return 'tsx';
22
- }
23
- if (ext === '.jsx') {
24
- return 'jsx';
25
- }
19
+ inPlace: false
26
20
  };
27
21
  const transform = async (filename, options = defaultOptions) => {
28
22
  const opts = {
@@ -31,24 +25,29 @@ const transform = async (filename, options = defaultOptions) => {
31
25
  };
32
26
  const file = resolve(filename);
33
27
  const code = (await readFile(file)).toString();
34
- const ast = parse(code);
35
- let source = format(code, ast, opts).toString();
36
- if (options.specifier) {
28
+ const ast = parse(filename, code);
29
+ let source = await format(code, ast, opts);
30
+ if (opts.rewriteSpecifier) {
37
31
  const code = await specifier.updateSrc(source, getLangFromExt(filename), ({
38
32
  value
39
33
  }) => {
34
+ if (typeof opts.rewriteSpecifier === 'function') {
35
+ return opts.rewriteSpecifier(value) ?? undefined;
36
+ }
37
+
40
38
  // Collapse any BinaryExpression or NewExpression to test for a relative specifier
41
39
  const collapsed = value.replace(/['"`+)\s]|new String\(/g, '');
42
40
  const relative = /^(?:\.|\.\.)\//;
43
41
  if (relative.test(collapsed)) {
44
42
  // $2 is for any closing quotation/parens around BE or NE
45
- return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"`]*)?$/, `$1${options.specifier}$2`);
43
+ return value.replace(/(.+)\.(?:m|c)?(?:j|t)s([)'"]*)?$/, `$1${opts.rewriteSpecifier}$2`);
46
44
  }
47
45
  });
48
46
  source = code;
49
47
  }
50
- if (opts.out) {
51
- await writeFile(resolve(opts.out), source);
48
+ const outputPath = opts.inPlace ? file : opts.out ? resolve(opts.out) : undefined;
49
+ if (outputPath) {
50
+ await writeFile(outputPath, source);
52
51
  }
53
52
  return source;
54
53
  };
package/dist/parse.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const parse: (source: string, dts?: boolean) => import("@babel/parser").ParseResult<import("@babel/types").File>;
1
+ declare const parse: (filename: string, code: string) => import("oxc-parser").ParseResult;
2
2
  export { parse };
package/dist/parse.js CHANGED
@@ -1,16 +1,5 @@
1
- import { parse as babelParse } from '@babel/parser';
2
- const parse = (source, dts = false) => {
3
- const ast = babelParse(source, {
4
- sourceType: 'module',
5
- allowAwaitOutsideFunction: true,
6
- allowReturnOutsideFunction: true,
7
- allowImportExportEverywhere: true,
8
- plugins: ['jsx', ['importAttributes', {
9
- deprecatedAssertSyntax: true
10
- }], ['typescript', {
11
- dts
12
- }]]
13
- });
14
- return ast;
1
+ import { parseSync } from 'oxc-parser';
2
+ const parse = (filename, code) => {
3
+ return parseSync(filename, code);
15
4
  };
16
5
  export { parse };
@@ -0,0 +1,6 @@
1
+ import type { Node } from 'oxc-parser';
2
+ declare const scopes: string[];
3
+ declare const scope: {
4
+ isScope(node: Node): boolean;
5
+ };
6
+ export { scopes, scope };
package/dist/types.d.ts CHANGED
@@ -1,7 +1,35 @@
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);
1
3
  export type ModuleOptions = {
2
- type?: 'module' | 'commonjs';
3
- modules?: boolean;
4
- specifier?: '.js' | '.mjs' | '.cjs' | '.ts' | '.mts' | '.cts';
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';
5
14
  out?: string;
15
+ inPlace?: boolean;
6
16
  };
7
- export type FormatterOptions = Omit<ModuleOptions, 'out'> & Required<Pick<ModuleOptions, 'type'>>;
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 IdentMeta = {
25
+ declare: SpannedNode[];
26
+ read: SpannedNode[];
27
+ };
28
+ export type Scope = {
29
+ type: string;
30
+ name: string;
31
+ node: Node;
32
+ idents: Set<string>;
33
+ };
34
+ export type FormatterOptions = Omit<ModuleOptions, 'out' | 'inPlace'>;
35
+ export type Identifier = IdentifierName | IdentifierReference | BindingIdentifier | LabelIdentifier | TSIndexSignatureName;
@@ -0,0 +1,25 @@
1
+ import type { Node } from 'oxc-parser';
2
+ import type { Specifier } from '@knighted/specifier';
3
+ import type { IdentMeta, Scope } 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 collectScopeIdentifiers: (node: Node, scopes: Scope[]) => void;
10
+ /**
11
+ * Collects all module scope identifiers in the AST.
12
+ *
13
+ * Ignores identifiers that are in functions or classes.
14
+ * Ignores new scopes for StaticBlock nodes (can only reference static class members).
15
+ *
16
+ * Special case handling for these which create their own scopes,
17
+ * but are also valid module scope identifiers:
18
+ * - ClassDeclaration
19
+ * - FunctionDeclaration
20
+ *
21
+ * Special case handling for var inside BlockStatement
22
+ * which are also valid module scope identifiers.
23
+ */
24
+ declare const collectModuleIdentifiers: (ast: Node, hoisting?: boolean) => Promise<Map<string, IdentMeta>>;
25
+ export { getLangFromExt, isValidUrl, collectScopeIdentifiers, collectModuleIdentifiers, exportsRename, requireMainRgx, };
package/dist/utils.js ADDED
@@ -0,0 +1,209 @@
1
+ import { extname } from 'node:path';
2
+ import { ancestorWalk } from './walk.js';
3
+ import { scopes as scopeNodes } from './helpers/scope.js';
4
+ import { identifier } from './helpers/identifier.js';
5
+ const getLangFromExt = filename => {
6
+ const ext = extname(filename);
7
+ if (ext.endsWith('.js')) {
8
+ return 'js';
9
+ }
10
+ if (ext.endsWith('.ts')) {
11
+ return 'ts';
12
+ }
13
+ if (ext === '.tsx') {
14
+ return 'tsx';
15
+ }
16
+ if (ext === '.jsx') {
17
+ return 'jsx';
18
+ }
19
+ };
20
+ const isValidUrl = url => {
21
+ try {
22
+ new URL(url);
23
+ return true;
24
+ } catch {
25
+ return false;
26
+ }
27
+ };
28
+ const exportsRename = '__exports';
29
+ const requireMainRgx = /(require\.main\s*===\s*module|module\s*===\s*require\.main)/g;
30
+ const collectScopeIdentifiers = (node, scopes) => {
31
+ const {
32
+ type
33
+ } = node;
34
+ switch (type) {
35
+ case 'BlockStatement':
36
+ case 'ClassBody':
37
+ scopes.push({
38
+ node,
39
+ type: 'Block',
40
+ name: type,
41
+ idents: new Set()
42
+ });
43
+ break;
44
+ case 'FunctionDeclaration':
45
+ case 'FunctionExpression':
46
+ case 'ArrowFunctionExpression':
47
+ {
48
+ const name = node.id ? node.id.name : 'anonymous';
49
+ const scope = {
50
+ node,
51
+ name,
52
+ type: 'Function',
53
+ idents: new Set()
54
+ };
55
+ node.params.map(param => {
56
+ if (param.type === 'TSParameterProperty') {
57
+ return param.parameter;
58
+ }
59
+ if (param.type === 'RestElement') {
60
+ return param.argument;
61
+ }
62
+ if (param.type === 'AssignmentPattern') {
63
+ return param.left;
64
+ }
65
+ return param;
66
+ }).filter(identifier.isNamed).forEach(param => {
67
+ scope.idents.add(param.name);
68
+ });
69
+
70
+ /**
71
+ * If a FunctionExpression has an id, it is a named function expression.
72
+ * The function expression name shadows the module scope identifier, so
73
+ * we don't want to count reads of module identifers that have the same name.
74
+ * They also do not cause a SyntaxError if the function expression name is
75
+ * the same as a module scope identifier.
76
+ *
77
+ * TODO: Is this necessary for FunctionDeclaration?
78
+ */
79
+ if (node.type === 'FunctionExpression' && node.id) {
80
+ scope.idents.add(node.id.name);
81
+ }
82
+
83
+ // First add the function to any previous scopes
84
+ if (scopes.length > 0) {
85
+ scopes[scopes.length - 1].idents.add(name);
86
+ }
87
+
88
+ // Then add the function scope to the scopes stack
89
+ scopes.push(scope);
90
+ }
91
+ break;
92
+ case 'ClassDeclaration':
93
+ {
94
+ const className = node.id ? node.id.name : 'anonymous';
95
+
96
+ // First add the class to any previous scopes
97
+ if (scopes.length > 0) {
98
+ scopes[scopes.length - 1].idents.add(className);
99
+ }
100
+
101
+ // Then add the class to the scopes stack
102
+ scopes.push({
103
+ node,
104
+ name: className,
105
+ type: 'Class',
106
+ idents: new Set()
107
+ });
108
+ }
109
+ break;
110
+ case 'ClassExpression':
111
+ {}
112
+ break;
113
+ case 'VariableDeclaration':
114
+ if (scopes.length > 0) {
115
+ const scope = scopes[scopes.length - 1];
116
+ node.declarations.forEach(decl => {
117
+ if (decl.type === 'VariableDeclarator' && decl.id.type === 'Identifier') {
118
+ scope.idents.add(decl.id.name);
119
+ }
120
+ });
121
+ }
122
+ break;
123
+ }
124
+ };
125
+
126
+ /**
127
+ * Collects all module scope identifiers in the AST.
128
+ *
129
+ * Ignores identifiers that are in functions or classes.
130
+ * Ignores new scopes for StaticBlock nodes (can only reference static class members).
131
+ *
132
+ * Special case handling for these which create their own scopes,
133
+ * but are also valid module scope identifiers:
134
+ * - ClassDeclaration
135
+ * - FunctionDeclaration
136
+ *
137
+ * Special case handling for var inside BlockStatement
138
+ * which are also valid module scope identifiers.
139
+ */
140
+ const collectModuleIdentifiers = async (ast, hoisting = true) => {
141
+ const identifiers = new Map();
142
+ const globalReads = new Map();
143
+ const scopes = [];
144
+ await ancestorWalk(ast, {
145
+ enter(node, ancestors) {
146
+ const {
147
+ type
148
+ } = node;
149
+ collectScopeIdentifiers(node, scopes);
150
+
151
+ // Add module scope identifiers to the registry map
152
+
153
+ if (type === 'Identifier') {
154
+ const {
155
+ name
156
+ } = node;
157
+ const meta = identifiers.get(name) ?? {
158
+ declare: [],
159
+ read: []
160
+ };
161
+ const isDeclaration = identifier.isDeclaration(ancestors);
162
+ const inScope = scopes.some(scope => scope.idents.has(name) || scope.name === name);
163
+ if (hoisting && !identifier.isDeclaration(ancestors) && !identifier.isFunctionExpressionId(ancestors) && !identifier.isExportSpecifierAlias(ancestors) && !identifier.isClassPropertyKey(ancestors) && !identifier.isMethodDefinitionKey(ancestors) && !identifier.isMemberKey(ancestors) && !identifier.isPropertyKey(ancestors) && !identifier.isIife(ancestors) && !inScope) {
164
+ if (globalReads.has(name)) {
165
+ globalReads.get(name)?.push(node);
166
+ } else {
167
+ globalReads.set(name, [node]);
168
+ }
169
+ }
170
+ if (isDeclaration) {
171
+ const isModuleScope = identifier.isModuleScope(ancestors);
172
+ const isClassOrFuncDeclaration = identifier.isClassOrFuncDeclarationId(ancestors);
173
+ const isVarDeclarationInGlobalScope = identifier.isVarDeclarationInGlobalScope(ancestors);
174
+ if (isModuleScope || isClassOrFuncDeclaration || isVarDeclarationInGlobalScope) {
175
+ meta.declare.push(node);
176
+
177
+ // Check for hoisted reads
178
+ if (hoisting && globalReads.has(name)) {
179
+ const reads = globalReads.get(name);
180
+ if (reads) {
181
+ reads.forEach(read => {
182
+ if (!meta.read.includes(read)) {
183
+ meta.read.push(read);
184
+ }
185
+ });
186
+ }
187
+ }
188
+ identifiers.set(name, meta);
189
+ }
190
+ } else {
191
+ if (identifiers.has(name) && !inScope && !identifier.isIife(ancestors) && !identifier.isFunctionExpressionId(ancestors) && !identifier.isExportSpecifierAlias(ancestors) && !identifier.isClassPropertyKey(ancestors) && !identifier.isMethodDefinitionKey(ancestors) && !identifier.isMemberKey(ancestors) && !identifier.isPropertyKey(ancestors)) {
192
+ // Closure is referencing module scope identifier
193
+ meta.read.push(node);
194
+ }
195
+ }
196
+ }
197
+ },
198
+ leave(node) {
199
+ const {
200
+ type
201
+ } = node;
202
+ if (scopeNodes.includes(type)) {
203
+ scopes.pop();
204
+ }
205
+ }
206
+ });
207
+ return identifiers;
208
+ };
209
+ export { getLangFromExt, isValidUrl, collectScopeIdentifiers, collectModuleIdentifiers, exportsRename, requireMainRgx };
package/dist/walk.d.ts ADDED
@@ -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/walk.js ADDED
@@ -0,0 +1,69 @@
1
+ import { visitorKeys } from 'oxc-parser';
2
+
3
+ /**
4
+ * Using visitorKeys instead of oxc Visitor to keep
5
+ * an ancestor-aware enter/leave API with this.skip()
6
+ * without per-node method boilerplate.
7
+ */
8
+
9
+ let skipDepth = -1;
10
+ const isNodeLike = value => {
11
+ return Boolean(value && typeof value === 'object' && 'type' in value);
12
+ };
13
+ const traverse = async (node, visitors, ancestors, depth) => {
14
+ if (!node) return;
15
+ const keys = visitorKeys[node.type] ?? [];
16
+ const ctx = {
17
+ skip: () => {
18
+ skipDepth = depth;
19
+ }
20
+ };
21
+ ancestors.push(node);
22
+ if (visitors.enter) {
23
+ await visitors.enter.call(ctx, node, ancestors);
24
+ }
25
+ if (skipDepth === -1 || depth < skipDepth) {
26
+ const fields = node;
27
+ for (const key of keys) {
28
+ const child = fields[key];
29
+ if (Array.isArray(child)) {
30
+ for (const nested of child) {
31
+ if (isNodeLike(nested)) {
32
+ await traverse(nested, visitors, ancestors, depth + 1);
33
+ }
34
+ }
35
+ } else if (isNodeLike(child)) {
36
+ await traverse(child, visitors, ancestors, depth + 1);
37
+ }
38
+ }
39
+ }
40
+ if (visitors.leave) {
41
+ await visitors.leave.call(ctx, node, ancestors);
42
+ }
43
+ ancestors.pop();
44
+ if (skipDepth === depth) {
45
+ skipDepth = -1;
46
+ }
47
+ };
48
+ const ancestorWalk = async (node, visitors) => {
49
+ skipDepth = -1;
50
+ await traverse(node, visitors, [], 0);
51
+ };
52
+ const walk = async (node, visitors) => {
53
+ const wrap = {
54
+ enter(current, ancestors) {
55
+ const parent = ancestors[ancestors.length - 2] ?? null;
56
+ if (visitors.enter) {
57
+ return visitors.enter.call(this, current, parent);
58
+ }
59
+ },
60
+ leave(current, ancestors) {
61
+ const parent = ancestors[ancestors.length - 2] ?? null;
62
+ if (visitors.leave) {
63
+ return visitors.leave.call(this, current, parent);
64
+ }
65
+ }
66
+ };
67
+ await ancestorWalk(node, wrap);
68
+ };
69
+ export { ancestorWalk, walk };