Skip to content

Commit e54ebff

Browse files
authored
fix(ssr): prohibit variable names in customer code from colliding with SSR-internal variables (#5196)
* fix(ssr): prohibit variable names in customer code from colliding with SSR-internal variables * chore: make generateMarkup inaccessible as a variable
1 parent 0777504 commit e54ebff

File tree

9 files changed

+31
-2
lines changed

9 files changed

+31
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ssrFiles": {
3+
"error": "error-ssr.txt",
4+
"expected": "expected-ssr.html"
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LWCTODO: identifier name '__lwcThrowAnError__' cannot start with '__lwc'

packages/@lwc/engine-server/src/__tests__/fixtures/prohibited-variable-name/error.txt

Whitespace-only changes.

packages/@lwc/engine-server/src/__tests__/fixtures/prohibited-variable-name/expected-ssr.html

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<x-dont-do-it-stupid>
2+
<template shadowrootmode="open">
3+
</template>
4+
</x-dont-do-it-stupid>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const tagName = 'x-dont-do-it-stupid';
2+
export { default } from 'x/dontDoItStupid';
3+
export * from 'x/dontDoItStupid';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { LightningElement } from 'lwc';
2+
3+
export default class TextStatic extends LightningElement {
4+
connectedCallback() {
5+
// This will throw a compile-time error in SSRv2!
6+
const __lwcThrowAnError__ = 'yup';
7+
console.log(__lwcThrowAnError__);
8+
}
9+
}

packages/@lwc/ssr-compiler/src/compile-js/generate-markup.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const bGenerateMarkup = esTemplate`
2020
const __lwcPublicProperties__ = new Set(${/*public properties*/ is.arrayExpression}.concat(__lwcSuperPublicProperties__));
2121
const __lwcPrivateProperties__ = new Set(${/*private properties*/ is.arrayExpression});
2222
23-
async function* generateMarkup(
23+
${/* component class */ 0}[__SYMBOL__GENERATE_MARKUP] = async function* generateMarkup(
2424
tagName,
2525
props,
2626
attrs,
@@ -75,7 +75,6 @@ const bGenerateMarkup = esTemplate`
7575
);
7676
yield \`</\${tagName}>\`;
7777
}
78-
${/* component class */ 0}[__SYMBOL__GENERATE_MARKUP] = generateMarkup;
7978
${/* component class */ 0}.__lwcPublicProperties__ = __lwcPublicProperties__;
8079
`<[Statement]>;
8180

packages/@lwc/ssr-compiler/src/compile-js/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ const visitors: Visitors = {
261261
}
262262
},
263263
},
264+
Identifier(path, _state) {
265+
const { node } = path;
266+
if (node?.name.startsWith('__lwc') && node.name.endsWith('__')) {
267+
// TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
268+
throw new Error(`LWCTODO: identifier name '${node.name}' cannot start with '__lwc'`);
269+
}
270+
},
264271
};
265272

266273
function validateUniqueDecorator(decorators: EsDecorator[]) {

0 commit comments

Comments
 (0)