Skip to content

Commit a5b21ed

Browse files
committed
fix(webpack): keep a newline before downleveled native classes
ts.transpileModule drops the snippet's leading newline, so a statement without a semicolon before a native class ran into the downleveled code. Closes #11681
1 parent 6b0790f commit a5b21ed

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,23 @@ export class FooImpl extends NSObject {
7272
INPUT_TS,
7373
);
7474
});
75+
76+
it('keeps a statement without semicolon apart from the downleveled class', async () => {
77+
const input = `const a = 1
78+
/*__NativeClass__*/
79+
class Foo extends NSObject {}
80+
`;
81+
const output = await new Promise<string>((resolve) =>
82+
downlevelLoader.call(createDownlevelContext(resolve), input),
83+
);
84+
const { diagnostics } = ts.transpileModule(output, {
85+
fileName: '/check.js',
86+
reportDiagnostics: true,
87+
});
88+
const errors = diagnostics.map((d) =>
89+
ts.flattenDiagnosticMessageText(d.messageText, '\n'),
90+
);
91+
expect(errors).toEqual([]);
92+
expect(output).toMatch(/const a = 1\s*\n\s*var /);
93+
});
7594
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ function downlevelClass(
106106
} catch (e) {
107107
// ignore parsing safety guard failures – fallback to raw transpiled
108108
}
109+
// transpileModule drops the leading newline; keep statements apart.
110+
transpiled = `\n${transpiled}`;
111+
leadingLines += 1;
109112
if (process.env.NATIVECLASS_DEBUG) {
110113
loader.emitWarning(
111114
new Error(

0 commit comments

Comments
 (0)