@sentry/bundler-plugins 11.19.0 → 11.20.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.
- package/build/cjs/core/version.js +1 -1
- package/build/cjs/core/version.js.map +1 -1
- package/build/cjs/rewriter/arrays.js +25 -0
- package/build/cjs/rewriter/arrays.js.map +1 -0
- package/build/cjs/rewriter/ast.js +102 -0
- package/build/cjs/rewriter/ast.js.map +1 -0
- package/build/cjs/rewriter/class-extends.js +67 -0
- package/build/cjs/rewriter/class-extends.js.map +1 -0
- package/build/cjs/rewriter/exports.js +241 -0
- package/build/cjs/rewriter/exports.js.map +1 -0
- package/build/cjs/rewriter/imports.js +22 -0
- package/build/cjs/rewriter/imports.js.map +1 -0
- package/build/cjs/rewriter/index.js +45 -0
- package/build/cjs/rewriter/module-shape.js +120 -0
- package/build/cjs/rewriter/module-shape.js.map +1 -0
- package/build/cjs/rewriter/parse.js +64 -0
- package/build/cjs/rewriter/parse.js.map +1 -0
- package/build/cjs/rewriter/report.js +17 -0
- package/build/cjs/rewriter/report.js.map +1 -0
- package/build/cjs/rewriter/rewriter.js +68 -0
- package/build/cjs/rewriter/rewriter.js.map +1 -0
- package/build/esm/core/version.js +1 -1
- package/build/esm/core/version.js.map +1 -1
- package/build/esm/package.json +1 -1
- package/build/esm/rewriter/arrays.js +25 -0
- package/build/esm/rewriter/arrays.js.map +1 -0
- package/build/esm/rewriter/ast.js +95 -0
- package/build/esm/rewriter/ast.js.map +1 -0
- package/build/esm/rewriter/class-extends.js +67 -0
- package/build/esm/rewriter/class-extends.js.map +1 -0
- package/build/esm/rewriter/exports.js +231 -0
- package/build/esm/rewriter/exports.js.map +1 -0
- package/build/esm/rewriter/imports.js +21 -0
- package/build/esm/rewriter/imports.js.map +1 -0
- package/build/esm/rewriter/index.js +11 -0
- package/build/esm/rewriter/module-shape.js +118 -0
- package/build/esm/rewriter/module-shape.js.map +1 -0
- package/build/esm/rewriter/parse.js +60 -0
- package/build/esm/rewriter/parse.js.map +1 -0
- package/build/esm/rewriter/report.js +16 -0
- package/build/esm/rewriter/report.js.map +1 -0
- package/build/esm/rewriter/rewriter.js +65 -0
- package/build/esm/rewriter/rewriter.js.map +1 -0
- package/build/types/core/version.d.ts +1 -1
- package/build/types/rewriter/arrays.d.ts +12 -0
- package/build/types/rewriter/arrays.d.ts.map +1 -0
- package/build/types/rewriter/ast.d.ts +120 -0
- package/build/types/rewriter/ast.d.ts.map +1 -0
- package/build/types/rewriter/class-extends.d.ts +16 -0
- package/build/types/rewriter/class-extends.d.ts.map +1 -0
- package/build/types/rewriter/exports.d.ts +88 -0
- package/build/types/rewriter/exports.d.ts.map +1 -0
- package/build/types/rewriter/imports.d.ts +7 -0
- package/build/types/rewriter/imports.d.ts.map +1 -0
- package/build/types/rewriter/index.d.ts +10 -0
- package/build/types/rewriter/index.d.ts.map +1 -0
- package/build/types/rewriter/module-shape.d.ts +47 -0
- package/build/types/rewriter/module-shape.d.ts.map +1 -0
- package/build/types/rewriter/parse.d.ts +27 -0
- package/build/types/rewriter/parse.d.ts.map +1 -0
- package/build/types/rewriter/report.d.ts +12 -0
- package/build/types/rewriter/report.d.ts.map +1 -0
- package/build/types/rewriter/rewriter.d.ts +64 -0
- package/build/types/rewriter/rewriter.d.ts.map +1 -0
- package/package.json +9 -4
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
const require_ast = require("./ast.js");
|
|
2
|
+
const require_parse = require("./parse.js");
|
|
3
|
+
// src/rewriter/module-shape.ts
|
|
4
|
+
/** Binding name used for a module's default export. */
|
|
5
|
+
const DEFAULT_EXPORT = "default";
|
|
6
|
+
function emptyShape() {
|
|
7
|
+
return {
|
|
8
|
+
classes: /* @__PURE__ */ new Map(),
|
|
9
|
+
imports: /* @__PURE__ */ new Map(),
|
|
10
|
+
namespaces: /* @__PURE__ */ new Map(),
|
|
11
|
+
reexports: /* @__PURE__ */ new Map(),
|
|
12
|
+
localExports: /* @__PURE__ */ new Map(),
|
|
13
|
+
starExports: []
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** Collect the shape of a module from its parsed AST. */
|
|
17
|
+
function shapeFromAst(ast) {
|
|
18
|
+
const shape = emptyShape();
|
|
19
|
+
for (const node of ast.body) switch (node.type) {
|
|
20
|
+
case "ClassDeclaration":
|
|
21
|
+
addAstClass(shape, node);
|
|
22
|
+
break;
|
|
23
|
+
case "ImportDeclaration":
|
|
24
|
+
addAstImports(shape, node);
|
|
25
|
+
break;
|
|
26
|
+
case "ExportNamedDeclaration":
|
|
27
|
+
addAstNamedExport(shape, node);
|
|
28
|
+
break;
|
|
29
|
+
case "ExportDefaultDeclaration": {
|
|
30
|
+
const decl = node.declaration;
|
|
31
|
+
if (decl.type === "ClassDeclaration" || decl.type === "ClassExpression") {
|
|
32
|
+
shape.defaultExportIsClass = true;
|
|
33
|
+
shape.defaultExportSuper = superRefFromNode(decl.superClass);
|
|
34
|
+
addAstClass(shape, decl);
|
|
35
|
+
} else if (decl.type === "Identifier") shape.defaultExportName = decl.name;
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case "ExportAllDeclaration": {
|
|
39
|
+
const starNode = node;
|
|
40
|
+
const source = starNode.exported || require_ast.isTypeOnlyExport(starNode) ? void 0 : asString(starNode.source?.value);
|
|
41
|
+
if (source) shape.starExports.push(source);
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return shape;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Extract the module shape from unprocessed source, such as a sibling module
|
|
49
|
+
* read off disk. Returns `undefined` when the source does not parse.
|
|
50
|
+
*/
|
|
51
|
+
function shapeFromSource(code, id) {
|
|
52
|
+
const lang = require_parse.langFromId(id);
|
|
53
|
+
for (const candidate of lang === "tsx" ? [lang] : [lang, "tsx"]) try {
|
|
54
|
+
return shapeFromAst(require_parse.parseModule(code, id, candidate));
|
|
55
|
+
} catch {}
|
|
56
|
+
}
|
|
57
|
+
function superRefFromNode(superClass) {
|
|
58
|
+
if (!superClass) return void 0;
|
|
59
|
+
if (superClass.type === "Identifier") return {
|
|
60
|
+
kind: "identifier",
|
|
61
|
+
name: superClass.name
|
|
62
|
+
};
|
|
63
|
+
if (superClass.type === "MemberExpression") {
|
|
64
|
+
const member = superClass;
|
|
65
|
+
const object = member.object?.type === "Identifier" ? member.object.name : void 0;
|
|
66
|
+
const property = member.property?.name;
|
|
67
|
+
if (object && property) return {
|
|
68
|
+
kind: "member",
|
|
69
|
+
object,
|
|
70
|
+
property
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function addAstClass(shape, classNode) {
|
|
75
|
+
if (classNode.id?.name) shape.classes.set(classNode.id.name, superRefFromNode(classNode.superClass));
|
|
76
|
+
}
|
|
77
|
+
function addAstImports(shape, node) {
|
|
78
|
+
const source = asString(node.source?.value);
|
|
79
|
+
if (!source || node.importKind === "type") return;
|
|
80
|
+
for (const specifier of node.specifiers ?? []) {
|
|
81
|
+
const local = specifier.local?.name;
|
|
82
|
+
if (!local || specifier.importKind === "type") continue;
|
|
83
|
+
const imported = require_ast.exportedName(specifier.imported);
|
|
84
|
+
if (specifier.type === "ImportSpecifier" && imported) shape.imports.set(local, {
|
|
85
|
+
source,
|
|
86
|
+
imported
|
|
87
|
+
});
|
|
88
|
+
else if (specifier.type === "ImportDefaultSpecifier") shape.imports.set(local, {
|
|
89
|
+
source,
|
|
90
|
+
imported: DEFAULT_EXPORT
|
|
91
|
+
});
|
|
92
|
+
else if (specifier.type === "ImportNamespaceSpecifier") shape.namespaces.set(local, source);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function addAstNamedExport(shape, node) {
|
|
96
|
+
if (node.declaration?.type === "ClassDeclaration") {
|
|
97
|
+
addAstClass(shape, node.declaration);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (require_ast.isTypeOnlyExport(node)) return;
|
|
101
|
+
const source = asString(node.source?.value);
|
|
102
|
+
for (const specifier of node.specifiers ?? []) {
|
|
103
|
+
const exported = require_ast.exportedName(specifier.exported);
|
|
104
|
+
const local = require_ast.exportedName(specifier.local);
|
|
105
|
+
if (!exported || !local || require_ast.isTypeOnlyExport(specifier)) continue;
|
|
106
|
+
if (source) shape.reexports.set(exported, {
|
|
107
|
+
source,
|
|
108
|
+
imported: local
|
|
109
|
+
});
|
|
110
|
+
else shape.localExports.set(exported, local);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function asString(value) {
|
|
114
|
+
return typeof value === "string" && value ? value : void 0;
|
|
115
|
+
}
|
|
116
|
+
exports.DEFAULT_EXPORT = DEFAULT_EXPORT;
|
|
117
|
+
exports.shapeFromAst = shapeFromAst;
|
|
118
|
+
exports.shapeFromSource = shapeFromSource;
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=module-shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"module-shape.js","names":["isTypeOnlyExport","langFromId","parseModule","exportedName"],"sources":["../../../src/rewriter/module-shape.ts"],"sourcesContent":["import type {\n BaseNode,\n ClassDeclarationNode,\n ExportAllNode,\n ExportDefaultNode,\n ExportNamedNode,\n IdentifierNode,\n ImportDeclarationNode,\n MemberExpressionNode,\n ProgramBody,\n} from './ast';\nimport { exportedName, isTypeOnlyExport } from './ast';\nimport { langFromId, parseModule } from './parse';\n\n/** Binding name used for a module's default export. */\nexport const DEFAULT_EXPORT = 'default';\n\n/** A resolved superclass reference: a bare identifier or a `ns.Member` expression. */\nexport type SuperRef = { kind: 'identifier'; name: string } | { kind: 'member'; object: string; property: string };\n\n/** The declarations of a single module that binding resolution needs. */\nexport interface ModuleShape {\n /** Top-level class names → their superclass reference (absent when the class has no `extends`). */\n classes: Map<string, SuperRef | undefined>;\n /** Local binding name → the import it came from (`imported` is `default` for a default import). */\n imports: Map<string, { source: string; imported: string }>;\n /** Local binding name → module specifier, for `import * as ns from '...'`. */\n namespaces: Map<string, string>;\n /** Exported name → the re-export it came from, for `export { x as y } from '...'`. */\n reexports: Map<string, { source: string; imported: string }>;\n /** Exported name → local binding name, for `export { x as y }` without a source. */\n localExports: Map<string, string>;\n /** Module specifiers of bare `export * from '...'` declarations. */\n starExports: string[];\n /** `export default <Identifier>` — the local name it refers to. */\n defaultExportName?: string;\n /** True when the module's default export is a class declaration/expression. */\n defaultExportIsClass?: boolean;\n /** Superclass of an anonymous `export default class extends X`. */\n defaultExportSuper?: SuperRef;\n}\n\nfunction emptyShape(): ModuleShape {\n return {\n classes: new Map(),\n imports: new Map(),\n namespaces: new Map(),\n reexports: new Map(),\n localExports: new Map(),\n starExports: [],\n };\n}\n\n/** Collect the shape of a module from its parsed AST. */\nexport function shapeFromAst(ast: ProgramBody): ModuleShape {\n const shape = emptyShape();\n\n for (const node of ast.body) {\n switch (node.type) {\n case 'ClassDeclaration':\n addAstClass(shape, node);\n break;\n case 'ImportDeclaration':\n addAstImports(shape, node);\n break;\n case 'ExportNamedDeclaration':\n addAstNamedExport(shape, node);\n break;\n case 'ExportDefaultDeclaration': {\n const decl = (node as ExportDefaultNode).declaration;\n if (decl.type === 'ClassDeclaration' || decl.type === 'ClassExpression') {\n shape.defaultExportIsClass = true;\n shape.defaultExportSuper = superRefFromNode((decl as ClassDeclarationNode).superClass);\n // `export default class Foo {}` also binds `Foo` locally.\n addAstClass(shape, decl);\n } else if (decl.type === 'Identifier') {\n shape.defaultExportName = (decl as IdentifierNode).name;\n }\n break;\n }\n case 'ExportAllDeclaration': {\n const starNode = node as ExportAllNode;\n // `export * as ns from '...'` binds a namespace object, not the individual exports.\n const source = starNode.exported || isTypeOnlyExport(starNode) ? undefined : asString(starNode.source?.value);\n if (source) shape.starExports.push(source);\n break;\n }\n }\n }\n\n return shape;\n}\n\n/**\n * Extract the module shape from unprocessed source, such as a sibling module\n * read off disk. Returns `undefined` when the source does not parse.\n */\nexport function shapeFromSource(code: string, id: string): ModuleShape | undefined {\n const lang = langFromId(id);\n // A module read off disk may hold TypeScript or JSX whatever its id says,\n // and `tsx` reads everything the other dialects do.\n for (const candidate of lang === 'tsx' ? [lang] : [lang, 'tsx' as const]) {\n try {\n return shapeFromAst(parseModule(code, id, candidate));\n } catch {\n // Try the next dialect.\n }\n }\n return undefined;\n}\n\nfunction superRefFromNode(superClass: BaseNode | null | undefined): SuperRef | undefined {\n if (!superClass) return undefined;\n\n if (superClass.type === 'Identifier') {\n return { kind: 'identifier', name: (superClass as IdentifierNode).name };\n }\n\n if (superClass.type === 'MemberExpression') {\n const member = superClass as MemberExpressionNode;\n const object = member.object?.type === 'Identifier' ? member.object.name : undefined;\n const property = member.property?.name;\n if (object && property) return { kind: 'member', object, property };\n }\n\n return undefined;\n}\n\nfunction addAstClass(shape: ModuleShape, classNode: ClassDeclarationNode): void {\n if (classNode.id?.name) shape.classes.set(classNode.id.name, superRefFromNode(classNode.superClass));\n}\n\nfunction addAstImports(shape: ModuleShape, node: ImportDeclarationNode): void {\n const source = asString(node.source?.value);\n // Type-only imports never contribute a runtime base class.\n if (!source || node.importKind === 'type') return;\n\n for (const specifier of node.specifiers ?? []) {\n const local = specifier.local?.name;\n if (!local || specifier.importKind === 'type') continue;\n\n const imported = exportedName(specifier.imported);\n if (specifier.type === 'ImportSpecifier' && imported) {\n shape.imports.set(local, { source, imported });\n } else if (specifier.type === 'ImportDefaultSpecifier') {\n shape.imports.set(local, { source, imported: DEFAULT_EXPORT });\n } else if (specifier.type === 'ImportNamespaceSpecifier') {\n shape.namespaces.set(local, source);\n }\n }\n}\n\nfunction addAstNamedExport(shape: ModuleShape, node: ExportNamedNode): void {\n if (node.declaration?.type === 'ClassDeclaration') {\n addAstClass(shape, node.declaration);\n return;\n }\n if (isTypeOnlyExport(node)) return;\n\n const source = asString(node.source?.value);\n for (const specifier of node.specifiers ?? []) {\n const exported = exportedName(specifier.exported);\n const local = exportedName(specifier.local);\n if (!exported || !local || isTypeOnlyExport(specifier)) continue;\n\n if (source) {\n shape.reexports.set(exported, { source, imported: local });\n } else {\n shape.localExports.set(exported, local);\n }\n }\n}\n\nfunction asString(value: unknown): string | undefined {\n return typeof value === 'string' && value ? value : undefined;\n}\n"],"mappings":";;;;AAeA,MAAa,iBAAiB;AA2B9B,SAAS,aAA0B;CACjC,OAAO;EACL,yBAAS,IAAI,IAAI;EACjB,yBAAS,IAAI,IAAI;EACjB,4BAAY,IAAI,IAAI;EACpB,2BAAW,IAAI,IAAI;EACnB,8BAAc,IAAI,IAAI;EACtB,aAAa,CAAC;CAChB;AACF;;AAGA,SAAgB,aAAa,KAA+B;CAC1D,MAAM,QAAQ,WAAW;CAEzB,KAAK,MAAM,QAAQ,IAAI,MACrB,QAAQ,KAAK,MAAb;EACE,KAAK;GACH,YAAY,OAAO,IAAI;GACvB;EACF,KAAK;GACH,cAAc,OAAO,IAAI;GACzB;EACF,KAAK;GACH,kBAAkB,OAAO,IAAI;GAC7B;EACF,KAAK,4BAA4B;GAC/B,MAAM,OAAQ,KAA2B;GACzC,IAAI,KAAK,SAAS,sBAAsB,KAAK,SAAS,mBAAmB;IACvE,MAAM,uBAAuB;IAC7B,MAAM,qBAAqB,iBAAkB,KAA8B,UAAU;IAErF,YAAY,OAAO,IAAI;GACzB,OAAO,IAAI,KAAK,SAAS,cACvB,MAAM,oBAAqB,KAAwB;GAErD;EACF;EACA,KAAK,wBAAwB;GAC3B,MAAM,WAAW;GAEjB,MAAM,SAAS,SAAS,YAAYA,YAAAA,iBAAiB,QAAQ,IAAI,KAAA,IAAY,SAAS,SAAS,QAAQ,KAAK;GAC5G,IAAI,QAAQ,MAAM,YAAY,KAAK,MAAM;GACzC;EACF;CACF;CAGF,OAAO;AACT;;;;;AAMA,SAAgB,gBAAgB,MAAc,IAAqC;CACjF,MAAM,OAAOC,cAAAA,WAAW,EAAE;CAG1B,KAAK,MAAM,aAAa,SAAS,QAAQ,CAAC,IAAI,IAAI,CAAC,MAAM,KAAc,GACrE,IAAI;EACF,OAAO,aAAaC,cAAAA,YAAY,MAAM,IAAI,SAAS,CA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B,MAAM,SAAS,SAAS,KAAK,QAAQ,KAAK;CAC1C,KAAK,MAAM,aAAa,KAAK,cAAc,CAAC,GAAG;EAC7C,MAAM,WAAWG,YAAAA,aAAa,UAAU,QAAQ;EAChD,MAAM,QAAQA,YAAAA,aAAa,UAAU,KAAK;EAC1C,IAAI,CAAC,YAAY,CAAC,SAASH,YAAAA,iBAAiB,SAAS,GAAG;EAExD,IAAI,QACF,MAAM,UAAU,IAAI,UAAU;GAAE;GAAQ,UAAU;EAAM,CAAC;OAEzD,MAAM,aAAa,IAAI,UAAU,KAAK;CAE1C;AACF;AAEA,SAAS,SAAS,OAAoC;CACpD,OAAO,OAAO,UAAU,YAAY,QAAQ,QAAQ,KAAA;AACtD"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
let _sentry_core = require("@sentry/core");
|
|
2
|
+
// src/rewriter/parse.ts
|
|
3
|
+
const PARSER_UNAVAILABLE_MESSAGE = "[@sentry/bundler-plugins] Could not load `oxc-parser` for this platform. Modules that need it will not be auto-instrumented.";
|
|
4
|
+
let oxc;
|
|
5
|
+
let warnedParserUnavailable = false;
|
|
6
|
+
const oxcLoaded = import("oxc-parser").then((module) => {
|
|
7
|
+
oxc = module;
|
|
8
|
+
return true;
|
|
9
|
+
}, () => {
|
|
10
|
+
oxc = null;
|
|
11
|
+
return false;
|
|
12
|
+
});
|
|
13
|
+
/** Resolves once `oxc-parser` has loaded or failed to load, with whether it is available. */
|
|
14
|
+
function loadParser() {
|
|
15
|
+
return oxcLoaded;
|
|
16
|
+
}
|
|
17
|
+
function isParserLoading() {
|
|
18
|
+
return oxc === void 0;
|
|
19
|
+
}
|
|
20
|
+
/** The source dialect a module id names by its extension. */
|
|
21
|
+
function langFromId(id) {
|
|
22
|
+
const extension = /\.([cm]?[jt]sx?)$/.exec((0, _sentry_core.stripUrlQueryAndFragment)(id))?.[1] ?? "js";
|
|
23
|
+
if (extension === "tsx") return "tsx";
|
|
24
|
+
if (extension === "jsx") return "jsx";
|
|
25
|
+
return extension.endsWith("ts") ? "ts" : "js";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The default {@link ModuleParser}, over `oxc-parser`. Throws when `oxc-parser`
|
|
29
|
+
* has not loaded; see {@link loadParser}.
|
|
30
|
+
*/
|
|
31
|
+
function parseModule(code, id, lang = langFromId(id)) {
|
|
32
|
+
if (!oxc) {
|
|
33
|
+
if (oxc === null && !warnedParserUnavailable) {
|
|
34
|
+
warnedParserUnavailable = true;
|
|
35
|
+
console.warn(PARSER_UNAVAILABLE_MESSAGE);
|
|
36
|
+
}
|
|
37
|
+
throw new Error(oxc === null ? PARSER_UNAVAILABLE_MESSAGE : "`oxc-parser` has not loaded yet");
|
|
38
|
+
}
|
|
39
|
+
const { program, errors } = oxc.parseSync(id, code, {
|
|
40
|
+
lang,
|
|
41
|
+
sourceType: "module",
|
|
42
|
+
preserveParens: false
|
|
43
|
+
});
|
|
44
|
+
if (errors.length > 0) throw new Error(errors[0]?.message);
|
|
45
|
+
return program;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* A parser over Rolldown's own `this.parse`, which reads TypeScript when told
|
|
49
|
+
* the dialect. Rollup's `this.parse` cannot, so it is not used.
|
|
50
|
+
*/
|
|
51
|
+
function bundlerParser(context) {
|
|
52
|
+
if (!context?.meta?.rolldownVersion || typeof context.parse !== "function") return void 0;
|
|
53
|
+
return (code, id) => context.parse?.(code, {
|
|
54
|
+
lang: langFromId(id),
|
|
55
|
+
preserveParens: false
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
exports.bundlerParser = bundlerParser;
|
|
59
|
+
exports.isParserLoading = isParserLoading;
|
|
60
|
+
exports.langFromId = langFromId;
|
|
61
|
+
exports.loadParser = loadParser;
|
|
62
|
+
exports.parseModule = parseModule;
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=parse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse.js","names":["stripUrlQueryAndFragment"],"sources":["../../../src/rewriter/parse.ts"],"sourcesContent":["import { stripUrlQueryAndFragment } from '@sentry/core';\nimport type * as OxcParser from 'oxc-parser';\nimport type { ProgramBody } from './ast';\n\nexport type ModuleLang = 'js' | 'jsx' | 'ts' | 'tsx';\n\n/** Parses a module into an ESTree program. Throws when the code does not parse. */\nexport type ModuleParser = (code: string, id: string) => ProgramBody;\n\n/** The part of a bundler's plugin context that can parse code. */\nexport interface ParserContext {\n meta?: { rolldownVersion?: string };\n parse?(input: string, options?: object | null): unknown;\n}\n\nconst PARSER_UNAVAILABLE_MESSAGE =\n '[@sentry/bundler-plugins] Could not load `oxc-parser` for this platform. ' +\n 'Modules that need it will not be auto-instrumented.';\n\n// `undefined` while loading, `null` when it failed to load.\nlet oxc: typeof OxcParser | null | undefined;\nlet warnedParserUnavailable = false;\n\n// The import is dynamic so that a platform without an `oxc-parser` native\n// binary gets a warning instead of a failed build.\nconst oxcLoaded: Promise<boolean> = import('oxc-parser').then(\n module => {\n oxc = module;\n return true;\n },\n () => {\n oxc = null;\n return false;\n },\n);\n\n/** Resolves once `oxc-parser` has loaded or failed to load, with whether it is available. */\nexport function loadParser(): Promise<boolean> {\n return oxcLoaded;\n}\n\nexport function isParserLoading(): boolean {\n return oxc === undefined;\n}\n\n/** The source dialect a module id names by its extension. */\nexport function langFromId(id: string): ModuleLang {\n const extension = /\\.([cm]?[jt]sx?)$/.exec(stripUrlQueryAndFragment(id))?.[1] ?? 'js';\n if (extension === 'tsx') return 'tsx';\n if (extension === 'jsx') return 'jsx';\n return extension.endsWith('ts') ? 'ts' : 'js';\n}\n\n/**\n * The default {@link ModuleParser}, over `oxc-parser`. Throws when `oxc-parser`\n * has not loaded; see {@link loadParser}.\n */\nexport function parseModule(code: string, id: string, lang: ModuleLang = langFromId(id)): ProgramBody {\n if (!oxc) {\n if (oxc === null && !warnedParserUnavailable) {\n warnedParserUnavailable = true;\n // eslint-disable-next-line no-console\n console.warn(PARSER_UNAVAILABLE_MESSAGE);\n }\n throw new Error(oxc === null ? PARSER_UNAVAILABLE_MESSAGE : '`oxc-parser` has not loaded yet');\n }\n\n // `preserveParens: false` matches the AST Vite produces.\n const { program, errors } = oxc.parseSync(id, code, { lang, sourceType: 'module', preserveParens: false });\n if (errors.length > 0) {\n throw new Error(errors[0]?.message);\n }\n return program;\n}\n\n/**\n * A parser over Rolldown's own `this.parse`, which reads TypeScript when told\n * the dialect. Rollup's `this.parse` cannot, so it is not used.\n */\nexport function bundlerParser(context: ParserContext | undefined): ModuleParser | undefined {\n if (!context?.meta?.rolldownVersion || typeof context.parse !== 'function') return undefined;\n return (code, id) => context.parse?.(code, { lang: langFromId(id), preserveParens: false }) as ProgramBody;\n}\n"],"mappings":";;AAeA,MAAM,6BACJ;AAIF,IAAI;AACJ,IAAI,0BAA0B;AAI9B,MAAM,YAA8B,OAAO,aAAa,CAAC,MACvD,WAAU;CACR,MAAM;CACN,OAAO;AACT,SACM;CACJ,MAAM;CACN,OAAO;AACT,CACF;;AAGA,SAAgB,aAA+B;CAC7C,OAAO;AACT;AAEA,SAAgB,kBAA2B;CACzC,OAAO,QAAQ,KAAA;AACjB;;AAGA,SAAgB,WAAW,IAAwB;CACjD,MAAM,YAAY,oBAAoB,MAAA,GAAKA,aAAAA,yBAAAA,CAAyB,EAAE,CAAC,CAAC,GAAG,MAAM;CACjF,IAAI,cAAc,OAAO,OAAO;CAChC,IAAI,cAAc,OAAO,OAAO;CAChC,OAAO,UAAU,SAAS,IAAI,IAAI,OAAO;AAC3C;;;;;AAMA,SAAgB,YAAY,MAAc,IAAY,OAAmB,WAAW,EAAE,GAAgB;CACpG,IAAI,CAAC,KAAK;EACR,IAAI,QAAQ,QAAQ,CAAC,yBAAyB;GAC5C,0BAA0B;GAE1B,QAAQ,KAAK,0BAA0B;EACzC;EACA,MAAM,IAAI,MAAM,QAAQ,OAAO,6BAA6B,iCAAiC;CAC/F;CAGA,MAAM,EAAE,SAAS,WAAW,IAAI,UAAU,IAAI,MAAM;EAAE;EAAM,YAAY;EAAU,gBAAgB;CAAM,CAAC;CACzG,IAAI,OAAO,SAAS,GAClB,MAAM,IAAI,MAAM,OAAO,EAAE,EAAE,OAAO;CAEpC,OAAO;AACT;;;;;AAMA,SAAgB,cAAc,SAA8D;CAC1F,IAAI,CAAC,SAAS,MAAM,mBAAmB,OAAO,QAAQ,UAAU,YAAY,OAAO,KAAA;CACnF,QAAQ,MAAM,OAAO,QAAQ,QAAQ,MAAM;EAAE,MAAM,WAAW,EAAE;EAAG,gBAAgB;CAAM,CAAC;AAC5F"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// src/rewriter/report.ts
|
|
2
|
+
function createReport() {
|
|
3
|
+
return {
|
|
4
|
+
wrapped: [],
|
|
5
|
+
skipped: [],
|
|
6
|
+
alreadyWrapped: []
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** The warning for the skipped sites of a module, or `undefined` when none were skipped. */
|
|
10
|
+
function formatReport(report, id) {
|
|
11
|
+
if (report.skipped.length === 0) return void 0;
|
|
12
|
+
return `[Sentry] Could not auto-instrument ${report.skipped.join(" and ")} in ${id}.`;
|
|
13
|
+
}
|
|
14
|
+
exports.createReport = createReport;
|
|
15
|
+
exports.formatReport = formatReport;
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=report.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report.js","names":[],"sources":["../../../src/rewriter/report.ts"],"sourcesContent":["/** What a rewrite did with the sites it found, by site label. */\nexport interface RewriteReport {\n wrapped: string[];\n /** Sites the rewrite could not wrap, for the user to wrap by hand. */\n skipped: string[];\n /** Sites the user already wrapped. */\n alreadyWrapped: string[];\n}\n\nexport function createReport(): RewriteReport {\n return { wrapped: [], skipped: [], alreadyWrapped: [] };\n}\n\n/** The warning for the skipped sites of a module, or `undefined` when none were skipped. */\nexport function formatReport(report: RewriteReport, id: string): string | undefined {\n if (report.skipped.length === 0) return undefined;\n return `[Sentry] Could not auto-instrument ${report.skipped.join(' and ')} in ${id}.`;\n}\n"],"mappings":";AASA,SAAgB,eAA8B;CAC5C,OAAO;EAAE,SAAS,CAAC;EAAG,SAAS,CAAC;EAAG,gBAAgB,CAAC;CAAE;AACxD;;AAGA,SAAgB,aAAa,QAAuB,IAAgC;CAClF,IAAI,OAAO,QAAQ,WAAW,GAAG,OAAO,KAAA;CACxC,OAAO,sCAAsC,OAAO,QAAQ,KAAK,OAAO,EAAE,MAAM,GAAG;AACrF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const require_runtime = require("../_virtual/_rolldown/runtime.js");
|
|
2
|
+
const require_parse = require("./parse.js");
|
|
3
|
+
const require_report = require("./report.js");
|
|
4
|
+
let magic_string = require("magic-string");
|
|
5
|
+
magic_string = require_runtime.__toESM(magic_string);
|
|
6
|
+
let node_path = require("node:path");
|
|
7
|
+
// src/rewriter/rewriter.ts
|
|
8
|
+
function createRewriteState(code) {
|
|
9
|
+
return {
|
|
10
|
+
ms: new magic_string.default(code),
|
|
11
|
+
needsImport: false,
|
|
12
|
+
wrappedLocals: /* @__PURE__ */ new Map(),
|
|
13
|
+
imports: /* @__PURE__ */ new Set(),
|
|
14
|
+
report: require_report.createReport()
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function createCallSiteRewriter(rules, options = {}) {
|
|
18
|
+
const rewrite = (matching, code, id, parse) => {
|
|
19
|
+
let ast;
|
|
20
|
+
try {
|
|
21
|
+
ast = parse(code, id);
|
|
22
|
+
} catch {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
const state = createRewriteState(code);
|
|
26
|
+
const ctx = {
|
|
27
|
+
ast,
|
|
28
|
+
code,
|
|
29
|
+
id,
|
|
30
|
+
state
|
|
31
|
+
};
|
|
32
|
+
const finish = () => {
|
|
33
|
+
const warning = require_report.formatReport(state.report, id);
|
|
34
|
+
if (warning) options.logger?.warn(warning);
|
|
35
|
+
if (!state.ms.hasChanged()) return null;
|
|
36
|
+
return {
|
|
37
|
+
code: state.ms.toString(),
|
|
38
|
+
map: state.ms.generateMap({ hires: true })
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const pending = applyRules(matching, ctx, 0);
|
|
42
|
+
return pending ? pending.then(finish) : finish();
|
|
43
|
+
};
|
|
44
|
+
return { transform(code, id, context) {
|
|
45
|
+
const matching = rules.filter((rule) => matches(rule.include, code, id));
|
|
46
|
+
if (matching.length === 0) return null;
|
|
47
|
+
const parse = options.parse ?? require_parse.bundlerParser(context) ?? require_parse.parseModule;
|
|
48
|
+
if (require_parse.isParserLoading()) return require_parse.loadParser().then(() => rewrite(matching, code, id, parse));
|
|
49
|
+
return rewrite(matching, code, id, parse);
|
|
50
|
+
} };
|
|
51
|
+
}
|
|
52
|
+
function matches(include, code, id) {
|
|
53
|
+
if (include.id && !include.id.test(id)) return false;
|
|
54
|
+
if (include.basename && !include.basename.test((0, node_path.basename)(id))) return false;
|
|
55
|
+
if (include.contains && !include.contains.every((substring) => code.includes(substring))) return false;
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
/** Applies the rules in order, staying synchronous until a rule returns a promise. */
|
|
59
|
+
function applyRules(rules, ctx, from) {
|
|
60
|
+
for (let index = from; index < rules.length; index++) {
|
|
61
|
+
const pending = rules[index]?.apply(ctx);
|
|
62
|
+
if (pending instanceof Promise) return pending.then(() => applyRules(rules, ctx, index + 1));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.createCallSiteRewriter = createCallSiteRewriter;
|
|
66
|
+
exports.createRewriteState = createRewriteState;
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=rewriter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rewriter.js","names":["MagicString","createReport","formatReport","bundlerParser","parseModule","isParserLoading","loadParser","basename"],"sources":["../../../src/rewriter/rewriter.ts"],"sourcesContent":["import { basename } from 'node:path';\nimport MagicString, { type SourceMap } from 'magic-string';\nimport type { ProgramBody } from './ast';\nimport {\n bundlerParser,\n isParserLoading,\n loadParser,\n type ModuleParser,\n parseModule,\n type ParserContext,\n} from './parse';\nimport { createReport, formatReport, type RewriteReport } from './report';\n\n/** The edits a rewrite makes to one module, shared by every rule that runs on it. */\nexport interface RewriteState {\n /** Positions are always relative to the original source. */\n ms: MagicString;\n /** Set once the rewritten code references a binding the caller must import. */\n needsImport: boolean;\n /** Local binding name → the binding that holds its wrapper. */\n wrappedLocals: Map<string, string>;\n /** Import statements already added to this module. */\n imports: Set<string>;\n report: RewriteReport;\n}\n\nexport function createRewriteState(code: string): RewriteState {\n return {\n ms: new MagicString(code),\n needsImport: false,\n wrappedLocals: new Map(),\n imports: new Set(),\n report: createReport(),\n };\n}\n\n/** Which modules a rule runs on. Every given condition must hold. */\nexport interface RuleInclude {\n /** Tested against the full module id. */\n id?: RegExp;\n /** Tested against the last path segment of the module id. */\n basename?: RegExp;\n /** Substrings the source must contain, checked before parsing. */\n contains?: string[];\n}\n\nexport interface RuleContext {\n ast: ProgramBody;\n code: string;\n id: string;\n state: RewriteState;\n}\n\nexport interface RewriteRule {\n name: string;\n include: RuleInclude;\n apply(ctx: RuleContext): void | Promise<void>;\n}\n\nexport interface RewriterOptions {\n /**\n * Defaults to Rolldown's `this.parse` when the transform gets a Rolldown\n * plugin context, and to {@link parseModule} otherwise.\n */\n parse?: ModuleParser;\n /** Receives the warning for sites a rule could not wrap. */\n logger?: { warn(message: string): void };\n}\n\nexport interface RewriteOutput {\n code: string;\n map: SourceMap;\n}\n\nexport interface CallSiteRewriter {\n /**\n * Runs every matching rule on one module. Returns `null` when nothing\n * changed. The result is a promise only while `oxc-parser` is still\n * loading, or when a rule's `apply` returns one.\n *\n * @param context the bundler's plugin context, for its parser\n */\n transform(code: string, id: string, context?: ParserContext): RewriteOutput | null | Promise<RewriteOutput | null>;\n}\n\nexport function createCallSiteRewriter(rules: RewriteRule[], options: RewriterOptions = {}): CallSiteRewriter {\n const rewrite = (\n matching: RewriteRule[],\n code: string,\n id: string,\n parse: ModuleParser,\n ): RewriteOutput | null | Promise<RewriteOutput | null> => {\n let ast: ProgramBody;\n try {\n ast = parse(code, id);\n } catch {\n return null;\n }\n\n const state = createRewriteState(code);\n const ctx: RuleContext = { ast, code, id, state };\n const finish = (): RewriteOutput | null => {\n const warning = formatReport(state.report, id);\n if (warning) options.logger?.warn(warning);\n if (!state.ms.hasChanged()) return null;\n return { code: state.ms.toString(), map: state.ms.generateMap({ hires: true }) };\n };\n\n const pending = applyRules(matching, ctx, 0);\n return pending ? pending.then(finish) : finish();\n };\n\n return {\n transform(code, id, context) {\n const matching = rules.filter(rule => matches(rule.include, code, id));\n if (matching.length === 0) return null;\n\n const parse = options.parse ?? bundlerParser(context) ?? parseModule;\n // Rules may parse other modules with `parseModule`, so it has to be ready\n // whichever parser reads this one.\n if (isParserLoading()) {\n return loadParser().then(() => rewrite(matching, code, id, parse));\n }\n return rewrite(matching, code, id, parse);\n },\n };\n}\n\nfunction matches(include: RuleInclude, code: string, id: string): boolean {\n if (include.id && !include.id.test(id)) return false;\n if (include.basename && !include.basename.test(basename(id))) return false;\n if (include.contains && !include.contains.every(substring => code.includes(substring))) return false;\n return true;\n}\n\n/** Applies the rules in order, staying synchronous until a rule returns a promise. */\nfunction applyRules(rules: RewriteRule[], ctx: RuleContext, from: number): Promise<void> | undefined {\n for (let index = from; index < rules.length; index++) {\n const pending = rules[index]?.apply(ctx);\n if (pending instanceof Promise) {\n return pending.then(() => applyRules(rules, ctx, index + 1));\n }\n }\n return undefined;\n}\n"],"mappings":";;;;;;;AA0BA,SAAgB,mBAAmB,MAA4B;CAC7D,OAAO;EACL,IAAI,IAAIA,aAAAA,QAAY,IAAI;EACxB,aAAa;EACb,+BAAe,IAAI,IAAI;EACvB,yBAAS,IAAI,IAAI;EACjB,QAAQC,eAAAA,aAAa;CACvB;AACF;AAmDA,SAAgB,uBAAuB,OAAsB,UAA2B,CAAC,GAAqB;CAC5G,MAAM,WACJ,UACA,MACA,IACA,UACyD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O;CAC/F,OAAO;AACT;;AAGA,SAAS,WAAW,OAAsB,KAAkB,MAAyC;CACnG,KAAK,IAAI,QAAQ,MAAM,QAAQ,MAAM,QAAQ,SAAS;EACpD,MAAM,UAAU,MAAM,MAAM,EAAE,MAAM,GAAG;EACvC,IAAI,mBAAmB,SACrB,OAAO,QAAQ,WAAW,WAAW,OAAO,KAAK,QAAQ,CAAC,CAAC;CAE/D;AAEF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","names":[],"sources":["../../../src/core/version.ts"],"sourcesContent":["export const LIB_VERSION = \"11.
|
|
1
|
+
{"version":3,"file":"version.js","names":[],"sources":["../../../src/core/version.ts"],"sourcesContent":["export const LIB_VERSION = \"11.20.0\";\n"],"mappings":";AAAA,MAAa,cAAc"}
|
package/build/esm/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"type":"module","version":"11.
|
|
1
|
+
{"type":"module","version":"11.20.0","sideEffects":["./sentry-release-injection-file.js","./sentry-esbuild-debugid-injection-file.js"]}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/rewriter/arrays.ts
|
|
2
|
+
/**
|
|
3
|
+
* Replace an array literal of identifiers with `build(names)`, where `names`
|
|
4
|
+
* holds each identifier once, in source order. An empty array is left alone.
|
|
5
|
+
* An array holding anything other than plain identifiers is left alone and
|
|
6
|
+
* recorded as skipped under `label`.
|
|
7
|
+
*
|
|
8
|
+
* @returns whether the array was replaced
|
|
9
|
+
*/
|
|
10
|
+
function wrapArrayLiteral(state, arrayNode, label, build) {
|
|
11
|
+
const elements = arrayNode.elements ?? [];
|
|
12
|
+
if (elements.length === 0) return false;
|
|
13
|
+
if (!elements.every((element) => element?.type === "Identifier")) {
|
|
14
|
+
state.report.skipped.push(label);
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const names = [...new Set(elements.map((element) => element.name))];
|
|
18
|
+
state.ms.overwrite(arrayNode.start, arrayNode.end, build(names));
|
|
19
|
+
state.needsImport = true;
|
|
20
|
+
state.report.wrapped.push(label);
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
export { wrapArrayLiteral };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=arrays.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arrays.js","names":[],"sources":["../../../src/rewriter/arrays.ts"],"sourcesContent":["import type { ArrayExpressionNode, BaseNode, IdentifierNode } from './ast';\nimport type { RewriteState } from './rewriter';\n\n/**\n * Replace an array literal of identifiers with `build(names)`, where `names`\n * holds each identifier once, in source order. An empty array is left alone.\n * An array holding anything other than plain identifiers is left alone and\n * recorded as skipped under `label`.\n *\n * @returns whether the array was replaced\n */\nexport function wrapArrayLiteral(\n state: RewriteState,\n arrayNode: BaseNode,\n label: string,\n build: (names: string[]) => string,\n): boolean {\n const elements = (arrayNode as ArrayExpressionNode).elements ?? [];\n if (elements.length === 0) return false;\n\n if (!elements.every(element => element?.type === 'Identifier')) {\n state.report.skipped.push(label);\n return false;\n }\n\n const names = [...new Set(elements.map(element => (element as IdentifierNode).name))];\n state.ms.overwrite(arrayNode.start, arrayNode.end, build(names));\n state.needsImport = true;\n state.report.wrapped.push(label);\n return true;\n}\n"],"mappings":";;;;;;;;;AAWA,SAAgB,iBACd,OACA,WACA,OACA,OACS;CACT,MAAM,WAAY,UAAkC,YAAY,CAAC;CACjE,IAAI,SAAS,WAAW,GAAG,OAAO;CAElC,IAAI,CAAC,SAAS,OAAM,YAAW,SAAS,SAAS,YAAY,GAAG;EAC9D,MAAM,OAAO,QAAQ,KAAK,KAAK;EAC/B,OAAO;CACT;CAEA,MAAM,QAAQ,CAAC,GAAG,IAAI,IAAI,SAAS,KAAI,YAAY,QAA2B,IAAI,CAAC,CAAC;CACpF,MAAM,GAAG,UAAU,UAAU,OAAO,UAAU,KAAK,MAAM,KAAK,CAAC;CAC/D,MAAM,cAAc;CACpB,MAAM,OAAO,QAAQ,KAAK,KAAK;CAC/B,OAAO;AACT"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// src/rewriter/ast.ts
|
|
2
|
+
/** The declaration a top-level statement makes, looking through `export` (`export const x = ...`). */
|
|
3
|
+
function declarationOf(node) {
|
|
4
|
+
return node.type === "ExportNamedDeclaration" ? node.declaration : node;
|
|
5
|
+
}
|
|
6
|
+
/** Whether `node` calls `methodName`, either bare (`fn()`) or as a member (`ns.fn()`). */
|
|
7
|
+
function isCallToMethod(node, methodName) {
|
|
8
|
+
if (node.type !== "CallExpression") return false;
|
|
9
|
+
const callee = node.callee;
|
|
10
|
+
if (!callee) return false;
|
|
11
|
+
if (callee.type === "Identifier" && callee.name === methodName) return true;
|
|
12
|
+
return callee.type === "MemberExpression" && callee.property?.type === "Identifier" && callee.property.name === methodName;
|
|
13
|
+
}
|
|
14
|
+
function isAnonymousFunctionOrClass(node) {
|
|
15
|
+
if (node.type === "ArrowFunctionExpression") return true;
|
|
16
|
+
return [
|
|
17
|
+
"FunctionExpression",
|
|
18
|
+
"FunctionDeclaration",
|
|
19
|
+
"ClassExpression",
|
|
20
|
+
"ClassDeclaration"
|
|
21
|
+
].includes(node.type) && !node.id;
|
|
22
|
+
}
|
|
23
|
+
/** `export type { X }` or `export { type X }`, which TypeScript erases. */
|
|
24
|
+
function isTypeOnlyExport(node) {
|
|
25
|
+
return node.exportKind === "type";
|
|
26
|
+
}
|
|
27
|
+
/** The name an export or import specifier refers to, for both `x` and `"x"`. */
|
|
28
|
+
function exportedName(node) {
|
|
29
|
+
if (node?.type === "Identifier") return node.name;
|
|
30
|
+
if (node?.type === "Literal" && typeof node.value === "string") return node.value;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The offset after the hashbang and the directive prologue (`'use server'`),
|
|
34
|
+
* where code may be inserted without changing what the module means.
|
|
35
|
+
*/
|
|
36
|
+
function directivesEnd(ast) {
|
|
37
|
+
let end = ast.hashbang?.end ?? 0;
|
|
38
|
+
for (const node of ast.body) {
|
|
39
|
+
if (node.type !== "ExpressionStatement" || typeof node.directive !== "string") break;
|
|
40
|
+
end = node.end;
|
|
41
|
+
}
|
|
42
|
+
return end;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The binding names a declaration introduces, including names inside
|
|
46
|
+
* destructuring patterns. `undefined` for a declaration that binds no runtime
|
|
47
|
+
* value (`interface`, `type`).
|
|
48
|
+
*/
|
|
49
|
+
function declaredNames(declaration) {
|
|
50
|
+
switch (declaration.type) {
|
|
51
|
+
case "ClassDeclaration":
|
|
52
|
+
case "FunctionDeclaration": {
|
|
53
|
+
const name = declaration.id?.name;
|
|
54
|
+
return name ? [name] : void 0;
|
|
55
|
+
}
|
|
56
|
+
case "VariableDeclaration": {
|
|
57
|
+
const names = [];
|
|
58
|
+
for (const declarator of declaration.declarations ?? []) collectPatternNames(declarator.id, names);
|
|
59
|
+
return names;
|
|
60
|
+
}
|
|
61
|
+
default: return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function collectPatternNames(pattern, names) {
|
|
65
|
+
if (!pattern) return;
|
|
66
|
+
const node = pattern;
|
|
67
|
+
switch (node.type) {
|
|
68
|
+
case "Identifier":
|
|
69
|
+
if (node.name) names.push(node.name);
|
|
70
|
+
break;
|
|
71
|
+
case "ObjectPattern":
|
|
72
|
+
for (const property of node.properties ?? []) collectPatternNames(property.type === "RestElement" ? property.argument : property.value, names);
|
|
73
|
+
break;
|
|
74
|
+
case "ArrayPattern":
|
|
75
|
+
for (const element of node.elements ?? []) collectPatternNames(element, names);
|
|
76
|
+
break;
|
|
77
|
+
case "AssignmentPattern":
|
|
78
|
+
collectPatternNames(node.left, names);
|
|
79
|
+
break;
|
|
80
|
+
case "RestElement": collectPatternNames(node.argument, names);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Visit `node` and every node below it, parents first. */
|
|
84
|
+
function walk(node, visit) {
|
|
85
|
+
if (Array.isArray(node)) {
|
|
86
|
+
for (const child of node) walk(child, visit);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (!node || typeof node !== "object" || typeof node.type !== "string") return;
|
|
90
|
+
visit(node);
|
|
91
|
+
for (const value of Object.values(node)) if (value && typeof value === "object") walk(value, visit);
|
|
92
|
+
}
|
|
93
|
+
export { declarationOf, declaredNames, directivesEnd, exportedName, isAnonymousFunctionOrClass, isCallToMethod, isTypeOnlyExport, walk };
|
|
94
|
+
|
|
95
|
+
//# sourceMappingURL=ast.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.js","names":[],"sources":["../../../src/rewriter/ast.ts"],"sourcesContent":["// Minimal ESTree node shapes. Any parser that puts `start`/`end` offsets on\n// every node fits (oxc, acorn, Rollup's `this.parse`).\n\nexport interface BaseNode {\n type: string;\n start: number;\n end: number;\n}\n\nexport interface ProgramBody {\n body: BaseNode[];\n hashbang?: BaseNode | null;\n}\n\nexport interface IdentifierNode extends BaseNode {\n name: string;\n}\n\n/** An `Identifier` or, for `export { x as \"name\" }`, a string `Literal`. */\nexport interface NameNode {\n type: string;\n name?: string;\n value?: unknown;\n}\n\nexport interface CallExpressionNode extends BaseNode {\n callee?: { type: string; name?: string; property?: { type: string; name?: string } };\n arguments?: BaseNode[];\n}\n\nexport interface MemberExpressionNode extends BaseNode {\n object?: { type: string; name?: string };\n property?: { type: string; name?: string };\n}\n\nexport interface ClassDeclarationNode extends BaseNode {\n id?: IdentifierNode | null;\n superClass?: BaseNode | null;\n decorators?: BaseNode[] | null;\n}\n\nexport interface ExportDefaultNode extends BaseNode {\n declaration: BaseNode;\n}\n\nexport interface ExportSpecifierNode extends BaseNode {\n local?: NameNode;\n exported?: NameNode;\n exportKind?: string;\n}\n\nexport interface ExportNamedNode extends BaseNode {\n declaration?: BaseNode | null;\n source?: (BaseNode & { value?: unknown }) | null;\n specifiers?: ExportSpecifierNode[];\n exportKind?: string;\n}\n\nexport interface ExportAllNode extends BaseNode {\n source?: { value?: unknown };\n exported?: NameNode | null;\n exportKind?: string;\n}\n\nexport interface VariableDeclaratorNode {\n id?: BaseNode | null;\n init?: BaseNode | null;\n}\n\nexport interface VariableDeclarationNode extends BaseNode {\n declarations?: VariableDeclaratorNode[];\n}\n\nexport interface ImportSpecifierNode {\n type: string;\n imported?: NameNode;\n local?: NameNode;\n importKind?: string;\n}\n\nexport interface ImportDeclarationNode extends BaseNode {\n source?: { value?: unknown };\n specifiers?: ImportSpecifierNode[];\n importKind?: string;\n}\n\nexport interface ArrayExpressionNode extends BaseNode {\n elements?: Array<BaseNode | null>;\n}\n\nexport interface PropertyNode extends BaseNode {\n key?: NameNode;\n value?: BaseNode;\n computed?: boolean;\n}\n\n/** The declaration a top-level statement makes, looking through `export` (`export const x = ...`). */\nexport function declarationOf(node: BaseNode): BaseNode | null | undefined {\n return node.type === 'ExportNamedDeclaration' ? (node as ExportNamedNode).declaration : node;\n}\n\n/** Whether `node` calls `methodName`, either bare (`fn()`) or as a member (`ns.fn()`). */\nexport function isCallToMethod(node: BaseNode, methodName: string): boolean {\n if (node.type !== 'CallExpression') return false;\n const callee = (node as CallExpressionNode).callee;\n if (!callee) return false;\n if (callee.type === 'Identifier' && callee.name === methodName) return true;\n return (\n callee.type === 'MemberExpression' && callee.property?.type === 'Identifier' && callee.property.name === methodName\n );\n}\n\nexport function isAnonymousFunctionOrClass(node: BaseNode): boolean {\n if (node.type === 'ArrowFunctionExpression') return true;\n const isFunctionOrClass = [\n 'FunctionExpression',\n 'FunctionDeclaration',\n 'ClassExpression',\n 'ClassDeclaration',\n ].includes(node.type);\n return isFunctionOrClass && !(node as ClassDeclarationNode).id;\n}\n\n/** `export type { X }` or `export { type X }`, which TypeScript erases. */\nexport function isTypeOnlyExport(node: { exportKind?: string }): boolean {\n return node.exportKind === 'type';\n}\n\n/** The name an export or import specifier refers to, for both `x` and `\"x\"`. */\nexport function exportedName(node: NameNode | undefined): string | undefined {\n if (node?.type === 'Identifier') return node.name;\n if (node?.type === 'Literal' && typeof node.value === 'string') return node.value;\n return undefined;\n}\n\n/**\n * The offset after the hashbang and the directive prologue (`'use server'`),\n * where code may be inserted without changing what the module means.\n */\nexport function directivesEnd(ast: ProgramBody): number {\n let end = ast.hashbang?.end ?? 0;\n for (const node of ast.body) {\n if (node.type !== 'ExpressionStatement' || typeof (node as { directive?: unknown }).directive !== 'string') break;\n end = node.end;\n }\n return end;\n}\n\n/**\n * The binding names a declaration introduces, including names inside\n * destructuring patterns. `undefined` for a declaration that binds no runtime\n * value (`interface`, `type`).\n */\nexport function declaredNames(declaration: BaseNode): string[] | undefined {\n switch (declaration.type) {\n case 'ClassDeclaration':\n case 'FunctionDeclaration': {\n const name = (declaration as ClassDeclarationNode).id?.name;\n return name ? [name] : undefined;\n }\n case 'VariableDeclaration': {\n const names: string[] = [];\n for (const declarator of (declaration as VariableDeclarationNode).declarations ?? []) {\n collectPatternNames(declarator.id, names);\n }\n return names;\n }\n default:\n return undefined;\n }\n}\n\nfunction collectPatternNames(pattern: BaseNode | null | undefined, names: string[]): void {\n if (!pattern) return;\n const node = pattern as BaseNode & {\n name?: string;\n properties?: Array<BaseNode & { value?: BaseNode; argument?: BaseNode }>;\n elements?: Array<BaseNode | null>;\n left?: BaseNode;\n argument?: BaseNode;\n };\n switch (node.type) {\n case 'Identifier':\n if (node.name) names.push(node.name);\n break;\n case 'ObjectPattern':\n for (const property of node.properties ?? []) {\n collectPatternNames(property.type === 'RestElement' ? property.argument : property.value, names);\n }\n break;\n case 'ArrayPattern':\n for (const element of node.elements ?? []) collectPatternNames(element, names);\n break;\n case 'AssignmentPattern':\n collectPatternNames(node.left, names);\n break;\n case 'RestElement':\n collectPatternNames(node.argument, names);\n break;\n }\n}\n\n/** Visit `node` and every node below it, parents first. */\nexport function walk(node: unknown, visit: (node: BaseNode) => void): void {\n if (Array.isArray(node)) {\n for (const child of node) walk(child, visit);\n return;\n }\n if (!node || typeof node !== 'object' || typeof (node as BaseNode).type !== 'string') return;\n\n visit(node as BaseNode);\n for (const value of Object.values(node)) {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CAAC,GACjF,oBAAoB,WAAW,IAAI,KAAK;GAE1C,OAAO;EACT;EACA,SACE;CACJ;AACF;AAEA,SAAS,oBAAoB,SAAsC,OAAuB;CACxF,IAAI,CAAC,SAAS;CACd,MAAM,OAAO;CAOb,QAAQ,KAAK,MAAb;EACE,KAAK;GACH,IAAI,KAAK,MAAM,MAAM,KAAK,KAAK,IAAI;GACnC;EACF,KAAK;GACH,KAAK,MAAM,YAAY,KAAK,cAAc,CAAC,GACzC,oBAAoB,SAAS,SAAS,gBAAgB,SAAS,WAAW,SAAS,OAAO,KAAK;GAEjG;EACF,KAAK;GACH,KAAK,MAAM,WAAW,KAAK,YAAY,CAAC,GAAG,oBAAoB,SAAS,KAAK;GAC7E;EACF,KAAK;GACH,oBAAoB,KAAK,MAAM,KAAK;GACpC;EACF,KAAK,eACH,oBAAoB,KAAK,UAAU,KAAK;CAE5C;AACF;;AAGA,SAAgB,KAAK,MAAe,OAAuC;CACzE,IAAI,MAAM,QAAQ,IAAI,GAAG;EACvB,KAAK,MAAM,SAAS,MAAM,KAAK,OAAO,KAAK;EAC3C;CACF;CACA,IAAI,CAAC,QAAQ,OAAO,SAAS,YAAY,OAAQ,KAAkB,SAAS,UAAU;CAEtF,MAAM,IAAgB;CACtB,KAAK,MAAM,SAAS,OAAO,OAAO,IAAI,GACpC,IAAI,SAAS,OAAO,UAAU,UAAU,KAAK,OAAO,KAAK;AAE7D"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { declarationOf } from "./ast.js";
|
|
2
|
+
// src/rewriter/class-extends.ts
|
|
3
|
+
/**
|
|
4
|
+
* Find top-level classes that (transitively, within this module) extend the
|
|
5
|
+
* given base class. A superclass must be a plain identifier (`extends Base`)
|
|
6
|
+
* or member access (`extends ns.Base`).
|
|
7
|
+
*
|
|
8
|
+
* Only same-file base chains are resolvable here; a base class imported from
|
|
9
|
+
* another module under a different name is invisible.
|
|
10
|
+
*/
|
|
11
|
+
function findClassesExtending(ast, base) {
|
|
12
|
+
const bases = collectBaseImports(ast, base);
|
|
13
|
+
if (bases.named.size === 0 && bases.namespaces.size === 0) return /* @__PURE__ */ new Set();
|
|
14
|
+
const classes = /* @__PURE__ */ new Map();
|
|
15
|
+
for (const node of ast.body) {
|
|
16
|
+
const declaration = declarationOf(node);
|
|
17
|
+
if (declaration?.type !== "ClassDeclaration") continue;
|
|
18
|
+
const classNode = declaration;
|
|
19
|
+
if (classNode.id?.name) classes.set(classNode.id.name, classNode);
|
|
20
|
+
}
|
|
21
|
+
const found = /* @__PURE__ */ new Set();
|
|
22
|
+
let changed = true;
|
|
23
|
+
while (changed) {
|
|
24
|
+
changed = false;
|
|
25
|
+
for (const [name, classNode] of classes) {
|
|
26
|
+
if (found.has(name)) continue;
|
|
27
|
+
if (extendsBase(classNode.superClass, base, bases, found)) {
|
|
28
|
+
found.add(name);
|
|
29
|
+
changed = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return found;
|
|
34
|
+
}
|
|
35
|
+
function collectBaseImports(ast, base) {
|
|
36
|
+
const named = /* @__PURE__ */ new Set();
|
|
37
|
+
const namespaces = /* @__PURE__ */ new Set();
|
|
38
|
+
for (const node of ast.body) {
|
|
39
|
+
if (node.type !== "ImportDeclaration") continue;
|
|
40
|
+
const importNode = node;
|
|
41
|
+
if (importNode.source?.value !== base.module || importNode.importKind === "type") continue;
|
|
42
|
+
for (const specifier of importNode.specifiers ?? []) {
|
|
43
|
+
if (specifier.importKind === "type") continue;
|
|
44
|
+
if (specifier.type === "ImportSpecifier" && specifier.imported?.name === base.name && specifier.local?.name) named.add(specifier.local.name);
|
|
45
|
+
else if (specifier.type === "ImportNamespaceSpecifier" && specifier.local?.name) namespaces.add(specifier.local.name);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
named,
|
|
50
|
+
namespaces
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function extendsBase(superClass, base, bases, detected) {
|
|
54
|
+
if (!superClass) return false;
|
|
55
|
+
if (superClass.type === "Identifier") {
|
|
56
|
+
const name = superClass.name;
|
|
57
|
+
return bases.named.has(name) || detected.has(name);
|
|
58
|
+
}
|
|
59
|
+
if (superClass.type === "MemberExpression") {
|
|
60
|
+
const member = superClass;
|
|
61
|
+
return member.object?.type === "Identifier" && !!member.object.name && bases.namespaces.has(member.object.name) && member.property?.name === base.name;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
export { findClassesExtending };
|
|
66
|
+
|
|
67
|
+
//# sourceMappingURL=class-extends.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class-extends.js","names":[],"sources":["../../../src/rewriter/class-extends.ts"],"sourcesContent":["import type {\n BaseNode,\n ClassDeclarationNode,\n IdentifierNode,\n ImportDeclarationNode,\n MemberExpressionNode,\n ProgramBody,\n} from './ast';\nimport { declarationOf } from './ast';\n\n/** A base class, named by the module it is imported from and its export name. */\nexport interface BaseClassRef {\n module: string;\n name: string;\n}\n\ninterface BaseBindings {\n /** Local identifiers bound to the named import of the base class. */\n named: Set<string>;\n /** Local identifiers bound to a `* as ns` import of the base module. */\n namespaces: Set<string>;\n}\n\n/**\n * Find top-level classes that (transitively, within this module) extend the\n * given base class. A superclass must be a plain identifier (`extends Base`)\n * or member access (`extends ns.Base`).\n *\n * Only same-file base chains are resolvable here; a base class imported from\n * another module under a different name is invisible.\n */\nexport function findClassesExtending(ast: ProgramBody, base: BaseClassRef): Set<string> {\n const bases = collectBaseImports(ast, base);\n if (bases.named.size === 0 && bases.namespaces.size === 0) {\n return new Set<string>();\n }\n\n // Every top-level class, including the `export class Foo {}` form.\n const classes = new Map<string, ClassDeclarationNode>();\n for (const node of ast.body) {\n const declaration = declarationOf(node);\n if (declaration?.type !== 'ClassDeclaration') continue;\n const classNode = declaration as ClassDeclarationNode;\n if (classNode.id?.name) classes.set(classNode.id.name, classNode);\n }\n\n const found = new Set<string>();\n // Iterate to a fixed point so an indirect chain (A extends B extends Base) is\n // fully resolved regardless of declaration order.\n let changed = true;\n while (changed) {\n changed = false;\n for (const [name, classNode] of classes) {\n if (found.has(name)) continue;\n if (extendsBase(classNode.superClass, base, bases, found)) {\n found.add(name);\n changed = true;\n }\n }\n }\n return found;\n}\n\nfunction collectBaseImports(ast: ProgramBody, base: BaseClassRef): BaseBindings {\n const named = new Set<string>();\n const namespaces = new Set<string>();\n for (const node of ast.body) {\n if (node.type !== 'ImportDeclaration') continue;\n const importNode = node as ImportDeclarationNode;\n if (importNode.source?.value !== base.module || importNode.importKind === 'type') continue;\n for (const specifier of importNode.specifiers ?? []) {\n if (specifier.importKind === 'type') continue;\n if (specifier.type === 'ImportSpecifier' && specifier.imported?.name === base.name && specifier.local?.name) {\n named.add(specifier.local.name);\n } else if (specifier.type === 'ImportNamespaceSpecifier' && specifier.local?.name) {\n namespaces.add(specifier.local.name);\n }\n }\n }\n return { named, namespaces };\n}\n\nfunction extendsBase(\n superClass: BaseNode | null | undefined,\n base: BaseClassRef,\n bases: BaseBindings,\n detected: Set<string>,\n): boolean {\n if (!superClass) return false;\n if (superClass.type === 'Identifier') {\n const name = (superClass as IdentifierNode).name;\n return bases.named.has(name) || detected.has(name);\n }\n if (superClass.type === 'MemberExpression') {\n const member = superClass as MemberExpressionNode;\n return (\n member.object?.type === 'Identifier' &&\n !!member.object.name &&\n bases.namespaces.has(member.object.name) &&\n member.property?.name === base.name\n );\n }\n return false;\n}\n"],"mappings":";;;;;;;;;;AA+BA,SAAgB,qBAAqB,KAAkB,MAAiC;CACtF,MAAM,QAAQ,mBAAmB,KAAK,IAAI;CAC1C,IAAI,MAAM,MAAM,SAAS,KAAK,MAAM,WAAW,SAAS,GACtD,uBAAO,IAAI,IAAY;CAIzB,MAAM,0BAAU,IAAI,IAAkC;CACtD,KAAK,MAAM,QAAQ,IAAI,MAAM;EAC3B,MAAM,cAAc,cAAc,IAAI;EACtC,IAAI,aAAa,SAAS,oBAAoB;EAC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