MCPcopy Create free account
hub / github.com/NativeScript/NativeScript / collect

Function collect

packages/vite/helpers/nativeclass-transform.ts:87–123  ·  view source on GitHub ↗
(node: ts.Node)

Source from the content-addressed store, hash-verified

85 const edits: { start: number; end: number; text: string }[] = [];
86 // Collect all class declarations (top-level or nested) for potential transform
87 const collect = (node: ts.Node) => {
88 if (ts.isClassDeclaration(node)) {
89 const fullStart = (node as any).getFullStart ? (node as any).getFullStart() : node.pos;
90 const preamble = working.slice(fullStart, Math.min(node.getStart(sf) + 64, node.end));
91 if (/\/\*__NativeClass__\*\//.test(preamble)) {
92 const original = working.slice(fullStart, node.end);
93 const stripped = original.replace(/\/\*__NativeClass__\*\/\s*/g, '').replace(/^\s*@NativeClass(?:\([\s\S]*?\))?\s*$/gm, '');
94 const hadExport = /^\s*export\s+class\b/.test(stripped);
95 const down = ts
96 .transpileModule(stripped, {
97 compilerOptions: {
98 module: ts.ModuleKind.ESNext,
99 target: ts.ScriptTarget.ES5,
100 experimentalDecorators: true,
101 emitDecoratorMetadata: false,
102 noEmitHelpers: true,
103 useDefineForClassFields: false,
104 },
105 })
106 .outputText.replace(/Object\.defineProperty\(([^,]+),\s*(["'`][^"'`]+["'`]|[A-Za-z_$][A-Za-z0-9_$]*),\s*{([^}]*)}\)/g, (m, obj, key, body) => {
107 if (/enumerable:\s*false/.test(body)) {
108 body = body.replace(/enumerable:\s*false/, 'enumerable: true');
109 }
110 return `Object.defineProperty(${obj}, ${key}, {${body}})`;
111 });
112 let cleaned = down.replace(/export \{\};?\s*$/m, '');
113 if (hadExport) {
114 const name = (node as ts.ClassDeclaration).name?.text;
115 if (name && !new RegExp(`export\s*{\s*${name}\s*}`, 'm').test(cleaned)) {
116 cleaned += `\nexport { ${name} };\n`;
117 }
118 }
119 edits.push({ start: fullStart, end: node.end, text: cleaned });
120 }
121 }
122 ts.forEachChild(node, collect);
123 };
124 collect(sf);
125 if (!edits.length) return null;
126 // Apply edits sequentially

Callers 1

Calls 4

replaceMethod · 0.80
sliceMethod · 0.45
testMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected