Skip to content

Commit 553a490

Browse files
committed
fix(webpack): downlevel native classes nested in a native class
The nested path spliced the inner class first and then the outer class at its original end offset, which no longer matched the edited text, leaving fragments of the inner class behind and an unparseable module. Only the outermost marked classes are collected now: transpiling the outer class to ES5 already downlevels every class inside it. All markers in the snippet are stripped, and nested files get the same source map path as everything else.
1 parent d64da45 commit 553a490

2 files changed

Lines changed: 35 additions & 17 deletions

File tree

‎packages/webpack5/__tests__/loaders/native-class-loaders.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,36 @@ class Foo extends NSObject {}
9191
expect(errors).toEqual([]);
9292
expect(output).toMatch(/const a = 1\s*\n\s*var /);
9393
});
94+
95+
it('downlevels a native class nested inside another native class', async () => {
96+
const input = `/*__NativeClass__*/
97+
class Outer extends NSObject {
98+
make() {
99+
/*__NativeClass__*/
100+
class Inner extends NSObject {
101+
get value() {
102+
return 1;
103+
}
104+
}
105+
return Inner;
106+
}
107+
}
108+
`;
109+
const output = await new Promise<string>((resolve) =>
110+
downlevelLoader.call(createDownlevelContext(resolve), input),
111+
);
112+
const { diagnostics } = ts.transpileModule(output, {
113+
fileName: '/check.js',
114+
reportDiagnostics: true,
115+
});
116+
const errors = diagnostics.map((d) =>
117+
ts.flattenDiagnosticMessageText(d.messageText, '\n'),
118+
);
119+
expect(errors).toEqual([]);
120+
expect(output).toMatch(/var Outer = /);
121+
expect(output).toMatch(/var Inner = /);
122+
expect(output).not.toMatch(/^\s*class /m);
123+
expect(output).not.toContain('/*__NativeClass__*/');
124+
expect(output).toMatch(/"value"[\s\S]*?enumerable: true/);
125+
});
94126
});

‎packages/webpack5/src/loaders/native-class-downlevel-loader/index.ts‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function downlevelClass(
3838
snippet: string,
3939
): DownleveledClass {
4040
// strip marker ONLY, preserve surrounding newline to avoid commenting out the next line
41-
const stripped = snippet.replace(/\/\*__NativeClass__\*\//m, '');
41+
const stripped = snippet.replace(/\/\*__NativeClass__\*\//g, '');
4242
const transpileResult = ts.transpileModule(stripped, {
4343
compilerOptions: {
4444
module: ts.ModuleKind.ESNext,
@@ -307,6 +307,8 @@ export default function nativeClassDownlevelLoader(
307307
const leading = content.slice(fullStart, start);
308308
if (/\/\*__NativeClass__\*\//.test(leading)) {
309309
candidates.push({ start, fullStart, end: node.end });
310+
// Downleveling the outer class also downlevels any marked class inside it.
311+
return;
310312
}
311313
}
312314
ts.forEachChild(node, checkNode);
@@ -320,22 +322,6 @@ export default function nativeClassDownlevelLoader(
320322
}
321323

322324
candidates.sort((a, b) => a.fullStart - b.fullStart);
323-
const nested = candidates.some(
324-
(candidate, i) => i > 0 && candidate.fullStart < candidates[i - 1].end,
325-
);
326-
if (nested) {
327-
// Replace from last to first to keep indices valid; no map for nested classes.
328-
let output = content;
329-
for (const candidate of [...candidates].reverse()) {
330-
const { code } = downlevelClass(this, snippetFor(output, candidate));
331-
output =
332-
output.slice(0, candidate.fullStart) +
333-
code +
334-
output.slice(candidate.end);
335-
}
336-
this.callback(null, output, map);
337-
return;
338-
}
339325

340326
const callback = this.async();
341327
const generator = new SourceMapGenerator({ file: this.resourcePath });

0 commit comments

Comments
 (0)