@knighted/module 1.3.0 → 1.3.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.
Files changed (46) hide show
  1. package/dist/ast.d.ts +1 -0
  2. package/dist/async.d.ts +4 -0
  3. package/dist/buildEsmPrelude.d.ts +14 -0
  4. package/dist/cjs/ast.d.cts +1 -0
  5. package/dist/cjs/async.d.cts +4 -0
  6. package/dist/cjs/buildEsmPrelude.d.cts +14 -0
  7. package/dist/cjs/exportBagToEsm.d.cts +13 -0
  8. package/dist/cjs/format.cjs +78 -852
  9. package/dist/cjs/formatVisitor.d.cts +42 -0
  10. package/dist/cjs/helpers/async.cjs +57 -0
  11. package/dist/cjs/idiomaticPlan.d.cts +28 -0
  12. package/dist/cjs/interopHelpers.d.cts +5 -0
  13. package/dist/cjs/lowerCjsRequireToImports.d.cts +17 -0
  14. package/dist/cjs/lowerEsmToCjs.d.cts +22 -0
  15. package/dist/cjs/pipeline/buildEsmPrelude.cjs +65 -0
  16. package/dist/cjs/pipeline/exportBagToEsm.cjs +81 -0
  17. package/dist/cjs/pipeline/formatVisitor.cjs +171 -0
  18. package/dist/cjs/pipeline/idiomaticPlan.cjs +224 -0
  19. package/dist/cjs/pipeline/interopHelpers.cjs +10 -0
  20. package/dist/cjs/pipeline/lowerCjsRequireToImports.cjs +110 -0
  21. package/dist/cjs/pipeline/lowerEsmToCjs.cjs +204 -0
  22. package/dist/exportBagToEsm.d.ts +13 -0
  23. package/dist/format.js +74 -848
  24. package/dist/formatVisitor.d.ts +42 -0
  25. package/dist/helpers/ast.d.ts +1 -0
  26. package/dist/helpers/async.d.ts +4 -0
  27. package/dist/helpers/async.js +50 -0
  28. package/dist/idiomaticPlan.d.ts +28 -0
  29. package/dist/interopHelpers.d.ts +5 -0
  30. package/dist/lowerCjsRequireToImports.d.ts +17 -0
  31. package/dist/lowerEsmToCjs.d.ts +22 -0
  32. package/dist/pipeline/buildEsmPrelude.d.ts +14 -0
  33. package/dist/pipeline/buildEsmPrelude.js +59 -0
  34. package/dist/pipeline/exportBagToEsm.d.ts +13 -0
  35. package/dist/pipeline/exportBagToEsm.js +75 -0
  36. package/dist/pipeline/formatVisitor.d.ts +42 -0
  37. package/dist/pipeline/formatVisitor.js +166 -0
  38. package/dist/pipeline/idiomaticPlan.d.ts +28 -0
  39. package/dist/pipeline/idiomaticPlan.js +218 -0
  40. package/dist/pipeline/interopHelpers.d.ts +5 -0
  41. package/dist/pipeline/interopHelpers.js +5 -0
  42. package/dist/pipeline/lowerCjsRequireToImports.d.ts +17 -0
  43. package/dist/pipeline/lowerCjsRequireToImports.js +102 -0
  44. package/dist/pipeline/lowerEsmToCjs.d.ts +22 -0
  45. package/dist/pipeline/lowerEsmToCjs.js +197 -0
  46. package/package.json +1 -1
@@ -0,0 +1,42 @@
1
+ import type MagicString from 'magic-string';
2
+ import type { Node, CallExpressionNode } from '../helpers/ast.cjs';
3
+ import type { IdentifierName } from 'oxc-parser';
4
+ import type { WarnOnce } from './exportBagToEsm.cjs';
5
+ import type { FormatterOptions, ExportsMeta } from '../types.cjs';
6
+ type AssignmentExpressionFn = (typeof import('../formatters/assignmentExpression.cjs'))['assignmentExpression'];
7
+ type IdentifierFn = (typeof import('../formatters/identifier.cjs'))['identifier'];
8
+ type MemberExpressionFn = (typeof import('../formatters/memberExpression.cjs'))['memberExpression'];
9
+ type MetaPropertyFn = (typeof import('../formatters/metaProperty.cjs'))['metaProperty'];
10
+ type VisitorThis = {
11
+ skip(): void;
12
+ };
13
+ type FormatWalkState = {
14
+ importMetaRef: boolean;
15
+ requireMainNeedsRealpath: boolean;
16
+ needsCreateRequire: boolean;
17
+ needsRequireResolveHelper: boolean;
18
+ };
19
+ type FormatWalkContext = {
20
+ code: MagicString;
21
+ opts: FormatterOptions;
22
+ warnOnce: WarnOnce;
23
+ shadowedBindings: Set<string>;
24
+ requireMainStrategy: FormatterOptions['requireMainStrategy'];
25
+ nestedRequireStrategy: FormatterOptions['nestedRequireStrategy'];
26
+ shouldRaiseEsm: boolean;
27
+ fullTransform: boolean;
28
+ useExportsBag: boolean;
29
+ exportsMeta: ExportsMeta;
30
+ isRequireMainMember: (node: Node, shadowed: Set<string>) => boolean;
31
+ isRequireCall: (node: Node, shadowed: Set<string>) => node is CallExpressionNode;
32
+ isStaticRequire: (node: Node, shadowed: Set<string>) => node is CallExpressionNode;
33
+ isAsyncContext: (ancestors: Node[]) => boolean;
34
+ isValidUrl: (value: string) => boolean;
35
+ isIdentifierName: (node: Node) => node is IdentifierName;
36
+ assignmentExpression: AssignmentExpressionFn;
37
+ metaProperty: MetaPropertyFn;
38
+ memberExpression: MemberExpressionFn;
39
+ identifier: IdentifierFn;
40
+ };
41
+ declare const buildFormatVisitor: (ctx: FormatWalkContext, state: FormatWalkState) => (this: VisitorThis, node: Node, ancestors: Node[]) => Promise<void>;
42
+ export { buildFormatVisitor, type FormatWalkState };
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isAsyncContext = exports.hasTopLevelAwait = void 0;
7
+ var _ast = require("./ast.cjs");
8
+ const hasTopLevelAwait = program => {
9
+ let found = false;
10
+ const walkNode = (node, inFunction) => {
11
+ if (found) return;
12
+ if (!(0, _ast.isAstNode)(node)) return;
13
+ switch (node.type) {
14
+ case 'FunctionDeclaration':
15
+ case 'FunctionExpression':
16
+ case 'ArrowFunctionExpression':
17
+ case 'ClassDeclaration':
18
+ case 'ClassExpression':
19
+ inFunction = true;
20
+ break;
21
+ }
22
+ if (!inFunction && node.type === 'AwaitExpression') {
23
+ found = true;
24
+ return;
25
+ }
26
+ for (const value of Object.values(node)) {
27
+ if (!value) continue;
28
+ if (Array.isArray(value)) {
29
+ for (const item of value) {
30
+ if (item && typeof item === 'object') {
31
+ walkNode(item, inFunction);
32
+ if (found) return;
33
+ }
34
+ }
35
+ } else if (value && typeof value === 'object') {
36
+ walkNode(value, inFunction);
37
+ if (found) return;
38
+ }
39
+ }
40
+ };
41
+ walkNode(program, false);
42
+ return found;
43
+ };
44
+ exports.hasTopLevelAwait = hasTopLevelAwait;
45
+ const isAsyncContext = ancestors => {
46
+ for (let i = ancestors.length - 1; i >= 0; i -= 1) {
47
+ const node = ancestors[i];
48
+ if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
49
+ return !!node.async;
50
+ }
51
+ if (node.type === 'ClassDeclaration' || node.type === 'ClassExpression') {
52
+ return false;
53
+ }
54
+ }
55
+ return true;
56
+ };
57
+ exports.isAsyncContext = isAsyncContext;
@@ -0,0 +1,28 @@
1
+ import type MagicString from 'magic-string';
2
+ import type { ExportsMap } from '../helpers/ast.cjs';
3
+ import type { FormatterOptions } from '../types.cjs';
4
+ type Replacement = {
5
+ start: number;
6
+ end: number;
7
+ };
8
+ type IdiomaticPlan = {
9
+ replacements: Replacement[];
10
+ exports: string[];
11
+ };
12
+ type IdiomaticPlanResult = {
13
+ ok: true;
14
+ plan: IdiomaticPlan;
15
+ } | {
16
+ ok: false;
17
+ reason: string;
18
+ };
19
+ type BuildIdiomaticPlanParams = {
20
+ src: string;
21
+ code: MagicString;
22
+ exportTable: ExportsMap;
23
+ shadowedBindings: Set<string>;
24
+ idiomaticMode: FormatterOptions['idiomaticExports'];
25
+ };
26
+ declare const buildIdiomaticPlan: ({ src, code, exportTable, shadowedBindings, idiomaticMode, }: BuildIdiomaticPlanParams) => IdiomaticPlanResult;
27
+ export { buildIdiomaticPlan };
28
+ export type { IdiomaticPlan, IdiomaticPlanResult };
@@ -0,0 +1,5 @@
1
+ declare const defaultInteropName = "__interopDefault";
2
+ declare const interopHelper = "const __interopDefault = mod => (mod && mod.__esModule ? mod.default : mod);\n";
3
+ declare const requireInteropName = "__requireDefault";
4
+ declare const requireInteropHelper = "const __requireDefault = mod => (mod && typeof mod === 'object' && 'default' in mod ? mod.default : mod);\n";
5
+ export { defaultInteropName, interopHelper, requireInteropHelper, requireInteropName };
@@ -0,0 +1,17 @@
1
+ import MagicString from 'magic-string';
2
+ import { type CallExpressionNode, type ProgramNode, type Node } from '../helpers/ast.cjs';
3
+ type RequireTransform = {
4
+ start: number;
5
+ end: number;
6
+ code: string;
7
+ };
8
+ declare const isStaticRequire: (node: Node, shadowed: Set<string>) => node is CallExpressionNode;
9
+ declare const isRequireCall: (node: Node, shadowed: Set<string>) => node is CallExpressionNode;
10
+ declare const lowerCjsRequireToImports: (program: ProgramNode, code: MagicString, shadowed: Set<string>) => {
11
+ transforms: RequireTransform[];
12
+ imports: string[];
13
+ hoisted: string[];
14
+ needsCreateRequire: boolean;
15
+ needsInteropHelper: boolean;
16
+ };
17
+ export { isRequireCall, isStaticRequire, lowerCjsRequireToImports, type RequireTransform };
@@ -0,0 +1,22 @@
1
+ import MagicString from 'magic-string';
2
+ import { type ProgramNode } from '../helpers/ast.cjs';
3
+ import type { FormatterOptions } from '../types.cjs';
4
+ declare const exportAssignment: (name: string, expr: string, live: "strict" | "loose" | "off") => string;
5
+ type ImportTransform = {
6
+ start: number;
7
+ end: number;
8
+ code: string;
9
+ needsInterop: boolean;
10
+ };
11
+ type ExportTransform = {
12
+ start: number;
13
+ end: number;
14
+ code: string;
15
+ needsInterop?: boolean;
16
+ };
17
+ declare const lowerEsmToCjs: (program: ProgramNode, code: MagicString, opts: FormatterOptions, containsTopLevelAwait: boolean) => {
18
+ importTransforms: ImportTransform[];
19
+ exportTransforms: ExportTransform[];
20
+ needsInterop: boolean;
21
+ };
22
+ export { exportAssignment, lowerEsmToCjs, type ExportTransform, type ImportTransform };
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildEsmPrelude = void 0;
7
+ var _exports = require("../utils/exports.cjs");
8
+ var _interopHelpers = require("./interopHelpers.cjs");
9
+ const buildEsmPrelude = options => {
10
+ const {
11
+ needsCreateRequire,
12
+ needsRequireResolveHelper,
13
+ requireMainNeedsRealpath,
14
+ hoistedImports,
15
+ hoistedStatements,
16
+ needsImportInterop,
17
+ importMetaPreludeMode,
18
+ useExportsBag
19
+ } = options;
20
+ let importMetaRef = options.importMetaRef;
21
+ const importPrelude = [];
22
+ if (needsCreateRequire || needsRequireResolveHelper) {
23
+ importMetaRef = true;
24
+ }
25
+ if (needsCreateRequire || needsRequireResolveHelper) {
26
+ importPrelude.push('import { createRequire } from "node:module";\n');
27
+ }
28
+ if (needsRequireResolveHelper) {
29
+ importPrelude.push('import { fileURLToPath } from "node:url";\n');
30
+ }
31
+ if (requireMainNeedsRealpath) {
32
+ importPrelude.push('import { realpathSync } from "node:fs";\n');
33
+ importPrelude.push('import { pathToFileURL } from "node:url";\n');
34
+ }
35
+ if (hoistedImports.length) {
36
+ importPrelude.push(...hoistedImports);
37
+ }
38
+ const setupPrelude = [];
39
+ if (needsImportInterop) {
40
+ setupPrelude.push(_interopHelpers.requireInteropHelper);
41
+ }
42
+ if (hoistedStatements.length) {
43
+ setupPrelude.push(...hoistedStatements);
44
+ }
45
+ const requireInit = needsCreateRequire ? 'const require = createRequire(import.meta.url);\n' : '';
46
+ const requireResolveInit = needsRequireResolveHelper ? needsCreateRequire ? `const __requireResolve = (id, parent) => {
47
+ const resolved = require.resolve(id, parent);
48
+ return resolved.startsWith("file://") ? fileURLToPath(resolved) : resolved;
49
+ };\n` : `const __requireResolve = (id, parent) => {
50
+ const req = createRequire(parent ?? import.meta.url);
51
+ const resolved = req.resolve(id, parent);
52
+ return resolved.startsWith("file://") ? fileURLToPath(resolved) : resolved;
53
+ };\n` : '';
54
+ const exportsBagInit = useExportsBag ? `let ${_exports.exportsRename} = {};
55
+ ` : '';
56
+ const modulePrelude = '';
57
+ const prelude = `${importPrelude.join('')}${importPrelude.length ? '\n' : ''}${setupPrelude.join('')}${setupPrelude.length ? '\n' : ''}${requireInit}${requireResolveInit}${exportsBagInit}${modulePrelude}`;
58
+ const importMetaTouch = (() => {
59
+ if (importMetaPreludeMode === 'on') return 'void import.meta.filename;\n';
60
+ if (importMetaPreludeMode === 'off') return '';
61
+ return importMetaRef ? 'void import.meta.filename;\n' : '';
62
+ })();
63
+ return `${prelude}${importMetaTouch}`;
64
+ };
65
+ exports.buildEsmPrelude = buildEsmPrelude;
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.exportBagToEsm = void 0;
7
+ var _exports = require("../utils/exports.cjs");
8
+ const exportBagToEsm = params => {
9
+ const {
10
+ code,
11
+ exportTable,
12
+ warnOnce
13
+ } = params;
14
+ let importMetaRef = params.importMetaRef;
15
+ const isValidExportName = name => /^[$A-Z_a-z][$\w]*$/.test(name);
16
+ const asExportName = name => isValidExportName(name) ? name : JSON.stringify(name);
17
+ const accessProp = name => isValidExportName(name) ? `${_exports.exportsRename}.${name}` : `${_exports.exportsRename}[${JSON.stringify(name)}]`;
18
+ const exportValueFor = name => {
19
+ if (name === '__dirname') {
20
+ importMetaRef = true;
21
+ return 'import.meta.dirname';
22
+ }
23
+ if (name === '__filename') {
24
+ importMetaRef = true;
25
+ return 'import.meta.filename';
26
+ }
27
+ return name;
28
+ };
29
+ const tempNameFor = name => {
30
+ const sanitized = name.replace(/[^$\w]/g, '_') || 'value';
31
+ const safe = /^[0-9]/.test(sanitized) ? `_${sanitized}` : sanitized;
32
+ return `__export_${safe}`;
33
+ };
34
+ for (const [key, entry] of exportTable) {
35
+ if (entry.reassignments.length) {
36
+ const loc = entry.reassignments[0];
37
+ warnOnce(`cjs-export-reassignment:${key}`, `Export '${key}' is reassigned after export; ESM live bindings may change consumer behavior.`, {
38
+ start: loc.start,
39
+ end: loc.end
40
+ });
41
+ }
42
+ }
43
+ const lines = [];
44
+ const defaultEntry = exportTable.get('default');
45
+ if (defaultEntry) {
46
+ const def = defaultEntry.fromIdentifier ?? _exports.exportsRename;
47
+ const defExpr = exportValueFor(def);
48
+ if (defExpr !== def) {
49
+ const temp = tempNameFor(def);
50
+ lines.push(`const ${temp} = ${defExpr};`);
51
+ lines.push(`export default ${temp};`);
52
+ } else {
53
+ lines.push(`export default ${defExpr};`);
54
+ }
55
+ }
56
+ for (const [key, entry] of exportTable) {
57
+ if (key === 'default') continue;
58
+ if (!isValidExportName(key)) {
59
+ warnOnce(`cjs-string-export:${key}`, `Synthesized string-literal export '${key}'. Some tooling may require bracket access to use it.`);
60
+ }
61
+ if (entry.fromIdentifier) {
62
+ const resolved = exportValueFor(entry.fromIdentifier);
63
+ if (resolved !== entry.fromIdentifier) {
64
+ const temp = tempNameFor(entry.fromIdentifier);
65
+ lines.push(`const ${temp} = ${resolved};`);
66
+ lines.push(`export { ${temp} as ${asExportName(key)} };`);
67
+ } else {
68
+ lines.push(`export { ${resolved} as ${asExportName(key)} };`);
69
+ }
70
+ } else {
71
+ const temp = tempNameFor(key);
72
+ lines.push(`const ${temp} = ${accessProp(key)};`);
73
+ lines.push(`export { ${temp} as ${asExportName(key)} };`);
74
+ }
75
+ }
76
+ if (lines.length) {
77
+ code.append(`\n${lines.join('\n')}\n`);
78
+ }
79
+ return importMetaRef;
80
+ };
81
+ exports.exportBagToEsm = exportBagToEsm;
@@ -0,0 +1,171 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.buildFormatVisitor = void 0;
7
+ var _exports = require("../utils/exports.cjs");
8
+ // Narrow typing for ancestorWalk's `this`
9
+
10
+ const buildFormatVisitor = (ctx, state) => {
11
+ return async function enter(node, ancestors) {
12
+ const parent = ancestors[ancestors.length - 2] ?? null;
13
+ const {
14
+ code,
15
+ opts,
16
+ warnOnce,
17
+ shadowedBindings,
18
+ requireMainStrategy,
19
+ nestedRequireStrategy,
20
+ shouldRaiseEsm,
21
+ fullTransform,
22
+ useExportsBag,
23
+ exportsMeta,
24
+ isRequireMainMember,
25
+ isRequireCall,
26
+ isStaticRequire,
27
+ isAsyncContext,
28
+ isValidUrl,
29
+ isIdentifierName,
30
+ assignmentExpression,
31
+ metaProperty,
32
+ memberExpression,
33
+ identifier
34
+ } = ctx;
35
+ if (shouldRaiseEsm && node.type === 'ReturnStatement' && parent?.type === 'Program') {
36
+ warnOnce('top-level-return', 'Top-level return is not allowed in ESM; the transformed module will fail to parse.', {
37
+ start: node.start,
38
+ end: node.end
39
+ });
40
+ }
41
+ if (shouldRaiseEsm && node.type === 'BinaryExpression') {
42
+ const op = node.operator;
43
+ const isEquality = op === '===' || op === '==' || op === '!==' || op === '!=';
44
+ if (isEquality) {
45
+ const leftMain = isRequireMainMember(node.left, shadowedBindings);
46
+ const rightMain = isRequireMainMember(node.right, shadowedBindings);
47
+ const leftModule = node.left.type === 'Identifier' && node.left.name === 'module' && !shadowedBindings.has('module');
48
+ const rightModule = node.right.type === 'Identifier' && node.right.name === 'module' && !shadowedBindings.has('module');
49
+ if (leftMain && rightModule || rightMain && leftModule) {
50
+ const negate = op === '!==' || op === '!=';
51
+ const mainExpr = requireMainStrategy === 'import-meta-main' ? 'import.meta.main' : 'import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href';
52
+ if (requireMainStrategy === 'realpath') {
53
+ state.requireMainNeedsRealpath = true;
54
+ state.importMetaRef = true;
55
+ }
56
+ if (requireMainStrategy === 'import-meta-main') {
57
+ state.importMetaRef = true;
58
+ }
59
+ code.update(node.start, node.end, negate ? `!(${mainExpr})` : mainExpr);
60
+ return;
61
+ }
62
+ }
63
+ }
64
+ if (shouldRaiseEsm && node.type === 'WithStatement') {
65
+ throw new Error('Cannot transform to ESM: with statements are not supported.');
66
+ }
67
+ if (shouldRaiseEsm && node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'eval' && !shadowedBindings.has('eval')) {
68
+ throw new Error('Cannot transform to ESM: eval is not supported.');
69
+ }
70
+ if (shouldRaiseEsm && node.type === 'CallExpression' && isRequireCall(node, shadowedBindings)) {
71
+ const isStatic = isStaticRequire(node, shadowedBindings);
72
+ const parent = ancestors[ancestors.length - 2] ?? null;
73
+ const grandparent = ancestors[ancestors.length - 3] ?? null;
74
+ const greatGrandparent = ancestors[ancestors.length - 4] ?? null;
75
+ const topLevelExprStmt = parent?.type === 'ExpressionStatement' && grandparent?.type === 'Program';
76
+ const topLevelVarDecl = parent?.type === 'VariableDeclarator' && grandparent?.type === 'VariableDeclaration' && greatGrandparent?.type === 'Program';
77
+ const hoistableTopLevel = isStatic && (topLevelExprStmt || topLevelVarDecl);
78
+ if (!isStatic || !hoistableTopLevel) {
79
+ if (nestedRequireStrategy === 'dynamic-import') {
80
+ const asyncCapable = isAsyncContext(ancestors);
81
+ if (asyncCapable) {
82
+ const arg = node.arguments[0];
83
+ const argSrc = arg ? code.slice(arg.start, arg.end) : 'undefined';
84
+ const literalVal = arg?.value;
85
+ const isJson = arg?.type === 'Literal' && typeof literalVal === 'string' && (literalVal.split(/[?#]/)[0] ?? literalVal).endsWith('.json');
86
+ const importTarget = isJson ? `${argSrc} with { type: "json" }` : argSrc;
87
+ code.update(node.start, node.end, `(await import(${importTarget}))`);
88
+ return;
89
+ }
90
+ }
91
+ state.needsCreateRequire = true;
92
+ }
93
+ }
94
+ if (node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') {
95
+ const skipped = ['__filename', '__dirname'];
96
+ const skippedParams = node.params.filter(param => param.type === 'Identifier' && skipped.includes(param.name));
97
+ const skippedFuncIdentifier = node.id?.type === 'Identifier' && skipped.includes(node.id.name);
98
+ if (skippedParams.length || skippedFuncIdentifier) {
99
+ this.skip();
100
+ }
101
+ }
102
+ if (node.type === 'AssignmentExpression' && node.left.type === 'MemberExpression' && node.left.object.type === 'MetaProperty' && node.left.property.type === 'Identifier' && node.left.property.name === 'url') {
103
+ if (node.right.type === 'Literal' && typeof node.right.value === 'string') {
104
+ if (!isValidUrl(node.right.value)) {
105
+ const rhs = code.snip(node.right.start, node.right.end).toString();
106
+ const assignment = code.snip(node.start, node.end).toString();
107
+ code.update(node.start, node.end, `/* Invalid assignment: ${rhs} is not a URL. ${assignment} */`);
108
+ this.skip();
109
+ }
110
+ }
111
+ }
112
+ if (node.type === 'MemberExpression' && node.property.type === 'Identifier' && ['__filename', '__dirname'].includes(node.property.name)) {
113
+ this.skip();
114
+ }
115
+ if (node.type === 'MemberExpression' && node.object.type === 'Identifier' && node.object.name === 'module' && node.property.type === 'Identifier' && node.property.name === 'exports' && parent?.type === 'ExpressionStatement') {
116
+ if (opts.target === 'module') {
117
+ code.update(node.start, node.end, ';');
118
+ this.skip();
119
+ }
120
+ }
121
+ if (node.type === 'AssignmentExpression') {
122
+ await assignmentExpression({
123
+ node,
124
+ parent,
125
+ code,
126
+ opts,
127
+ meta: exportsMeta
128
+ });
129
+ }
130
+ if (node.type === 'MetaProperty') {
131
+ metaProperty(node, parent, code, opts);
132
+ }
133
+ if (node.type === 'MemberExpression') {
134
+ memberExpression(node, parent, code, opts, shadowedBindings, {
135
+ onRequireResolve: () => {
136
+ if (shouldRaiseEsm) state.needsRequireResolveHelper = true;
137
+ },
138
+ requireResolveName: '__requireResolve',
139
+ onDiagnostic: (codeId, message, loc) => {
140
+ if (shouldRaiseEsm) warnOnce(codeId, message, loc);
141
+ }
142
+ }, useExportsBag, fullTransform);
143
+ }
144
+ if (shouldRaiseEsm && node.type === 'ThisExpression') {
145
+ const bindsThis = ancestor => {
146
+ return ancestor.type === 'FunctionDeclaration' || ancestor.type === 'FunctionExpression' || ancestor.type === 'ClassDeclaration' || ancestor.type === 'ClassExpression';
147
+ };
148
+ const bindingAncestor = ancestors.find(ancestor => bindsThis(ancestor));
149
+ const isTopLevel = !bindingAncestor;
150
+ if (isTopLevel) {
151
+ code.update(node.start, node.end, _exports.exportsRename);
152
+ return;
153
+ }
154
+ }
155
+ if (isIdentifierName(node)) {
156
+ if (shouldRaiseEsm && (node.name === '__dirname' || node.name === '__filename')) {
157
+ state.importMetaRef = true;
158
+ }
159
+ identifier({
160
+ node,
161
+ ancestors,
162
+ code,
163
+ opts,
164
+ meta: exportsMeta,
165
+ shadowed: shadowedBindings,
166
+ useExportsBag
167
+ });
168
+ }
169
+ };
170
+ };
171
+ exports.buildFormatVisitor = buildFormatVisitor;