Skip to content

Commit d64da45

Browse files
fix(webpack): keep a newline before downleveled native classes (#11683)
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 [skip-ci] Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
1 parent 0b353ba commit d64da45

2 files changed

Lines changed: 23 additions & 1 deletion

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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ interface Candidate {
2222
interface DownleveledClass {
2323
code: string;
2424
map: any;
25-
// Lines added in front of the transpiled class (the IIFE wrapper).
25+
// Lines added in front of the transpiled class (separator newline and IIFE wrapper).
2626
leadingLines: number;
2727
}
2828

@@ -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)