MCPcopy Index your code
hub / github.com/NativeScript/NativeScript / emitDownleveledClass

Function emitDownleveledClass

tools/transformers/native-class.js:128–166  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

126}
127
128function emitDownleveledClass(node) {
129 // Preserve leading trivia (including possible preceding import statements separated earlier by bundler) only remove the decorator itself.
130 const stripped = node.getText().replace(/@NativeClass(?:\((?:.|\n)*?\))?\s*/gm, '');
131 const downleveled = ts
132 .transpileModule(stripped, {
133compilerOptions: {
134module: ts.ModuleKind.ESNext,
135target: ts.ScriptTarget.ES5,
136noEmitHelpers: true,
137experimentalDecorators: true,
138emitDecoratorMetadata: false,
139useDefineForClassFields: false,
140},
141})
142 .outputText.replace(/(Object\.defineProperty\(.*?{.*?)(enumerable:\s*false)(.*?}\))/gs, '$1enumerable: true$3');
143
144 const helperSource = ts.createSourceFile(
145 (node.getSourceFile()?.fileName ?? 'NativeClass.ts') + '.helper.js',
146 downleveled,
147 ts.ScriptTarget.ES5,
148 true,
149 ts.ScriptKind.JS,
150 );
151
152 const statements = [];
153 for (const statement of helperSource.statements) {
154 if (statement.kind === ts.SyntaxKind.EndOfFileToken) {
155 continue;
156 }
157 // Explicitly skip import declarations accidentally re-emitted (shouldn't occur but defensive)
158 if (ts.isImportDeclaration(statement)) {
159 continue;
160 }
161 const prepared = prepareSynthesizedNode(statement, node);
162 statements.push(prepared);
163 }
164
165 return statements;
166}
167
168function prepareSynthesizedNode(node, original) {
169 const clone = ts.factory.cloneNode(node);

Callers 1

transformStatementsFunction · 0.70

Calls 3

replaceMethod · 0.80
prepareSynthesizedNodeFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected