Skip to content

Commit

Permalink
fix(ssr): prohibit variable names in customer code from colliding wit…
Browse files Browse the repository at this point in the history
…h 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
  • Loading branch information
divmain authored Feb 5, 2025
1 parent 0777504 commit e54ebff
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ssrFiles": {
"error": "error-ssr.txt",
"expected": "expected-ssr.html"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LWCTODO: identifier name '__lwcThrowAnError__' cannot start with '__lwc'
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x-dont-do-it-stupid>
<template shadowrootmode="open">
</template>
</x-dont-do-it-stupid>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-dont-do-it-stupid';
export { default } from 'x/dontDoItStupid';
export * from 'x/dontDoItStupid';
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { LightningElement } from 'lwc';

export default class TextStatic extends LightningElement {
connectedCallback() {
// This will throw a compile-time error in SSRv2!
const __lwcThrowAnError__ = 'yup';
console.log(__lwcThrowAnError__);
}
}
3 changes: 1 addition & 2 deletions packages/@lwc/ssr-compiler/src/compile-js/generate-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const bGenerateMarkup = esTemplate`
const __lwcPublicProperties__ = new Set(${/*public properties*/ is.arrayExpression}.concat(__lwcSuperPublicProperties__));
const __lwcPrivateProperties__ = new Set(${/*private properties*/ is.arrayExpression});
async function* generateMarkup(
${/* component class */ 0}[__SYMBOL__GENERATE_MARKUP] = async function* generateMarkup(
tagName,
props,
attrs,
Expand Down Expand Up @@ -75,7 +75,6 @@ const bGenerateMarkup = esTemplate`
);
yield \`</\${tagName}>\`;
}
${/* component class */ 0}[__SYMBOL__GENERATE_MARKUP] = generateMarkup;
${/* component class */ 0}.__lwcPublicProperties__ = __lwcPublicProperties__;
`<[Statement]>;

Expand Down
7 changes: 7 additions & 0 deletions packages/@lwc/ssr-compiler/src/compile-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ const visitors: Visitors = {
}
},
},
Identifier(path, _state) {
const { node } = path;
if (node?.name.startsWith('__lwc') && node.name.endsWith('__')) {
// TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
throw new Error(`LWCTODO: identifier name '${node.name}' cannot start with '__lwc'`);
}
},
};

function validateUniqueDecorator(decorators: EsDecorator[]) {
Expand Down

0 comments on commit e54ebff

Please sign in to comment.