Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: v2 non-field api support #5178

Merged
merged 5 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<x-parent>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this test for symmetry (I know it is covered elsewhere too)

<template shadowrootmode="open">
<x-child>
<template shadowrootmode="open">
const field api value
</template>
</x-child>
<x-child>
<template shadowrootmode="open">
field api value
</template>
</x-child>
</template>
</x-parent>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const tagName = 'x-parent';
export { default } from 'x/parent';
export * from 'x/parent';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
{fieldApi}
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Child extends LightningElement {
@api fieldApi;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<x-child field-api="const field api value"></x-child>
<x-child field-api={fieldApiValue}></x-child>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement } from 'lwc';

export default class Parent extends LightningElement {
fieldApiValue = 'field api value';
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LightningElement } from 'lwc';

const privateFields = undefined;
const publicFields = undefined;
const privateProperties = undefined;
const publicProperties = undefined;
const stylesheetScopeToken = undefined;
const hasScopedStylesheets = undefined;
const defaultScopedStylesheets = undefined;
Expand All @@ -12,8 +12,8 @@ export default class extends LightningElement {
Object.assign(
{},
{
privateFields,
publicFields,
privateProperties,
publicProperties,
stylesheetScopeToken,
hasScopedStylesheets,
defaultScopedStylesheets,
Expand Down
14 changes: 7 additions & 7 deletions packages/@lwc/ssr-compiler/src/compile-js/generate-markup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type { ComponentMetaState } from './types';

const bGenerateMarkup = esTemplate`
// These variables may mix with component-authored variables, so should be reasonably unique
const __lwcPublicFields__ = new Set(${/*public fields*/ is.arrayExpression});
const __lwcPrivateFields__ = new Set(${/*private fields*/ is.arrayExpression});
const __lwcPublicProperties__ = new Set(${/*api*/ is.arrayExpression});
const __lwcPrivateProperties__ = new Set(${/*private fields*/ is.arrayExpression});

async function* generateMarkup(
tagName,
Expand All @@ -43,8 +43,8 @@ const bGenerateMarkup = esTemplate`
instance[__SYMBOL__SET_INTERNALS](
props,
attrs,
__lwcPublicFields__,
__lwcPrivateFields__,
__lwcPublicProperties__,
__lwcPrivateProperties__,
);
instance.isConnected = true;
if (instance.connectedCallback) {
Expand Down Expand Up @@ -101,7 +101,7 @@ export function addGenerateMarkupFunction(
tagName: string,
filename: string
) {
const { privateFields, publicFields, tmplExplicitImports } = state;
const { privateProperties, publicProperties, tmplExplicitImports } = state;

// The default tag name represents the component name that's passed to the transformer.
// This is needed to generate markup for dynamic components which are invoked through
Expand Down Expand Up @@ -141,8 +141,8 @@ export function addGenerateMarkupFunction(
);
program.body.push(
...bGenerateMarkup(
b.arrayExpression(publicFields.map(b.literal)),
b.arrayExpression(privateFields.map(b.literal)),
b.arrayExpression(publicProperties.map(b.literal)),
b.arrayExpression(privateProperties.map(b.literal)),
defaultTagName,
classIdentifier,
connectWireAdapterCode
Expand Down
18 changes: 13 additions & 5 deletions packages/@lwc/ssr-compiler/src/compile-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ const visitors: Visitors = {
validateUniqueDecorator(decorators);
const decoratedExpression = decorators?.[0]?.expression;
if (is.identifier(decoratedExpression) && decoratedExpression.name === 'api') {
state.publicFields.push(node.key.name);
state.publicProperties.push(node.key.name);
} else if (
is.callExpression(decoratedExpression) &&
is.identifier(decoratedExpression.callee) &&
decoratedExpression.callee.name === 'wire'
) {
catalogWireAdapters(path, state);
state.privateFields.push(node.key.name);
state.privateProperties.push(node.key.name);
} else {
state.privateFields.push(node.key.name);
state.privateProperties.push(node.key.name);
}

if (
Expand Down Expand Up @@ -164,6 +164,14 @@ const visitors: Visitors = {
} else {
catalogWireAdapters(path, state);
}
} else if (is.identifier(decoratedExpression) && decoratedExpression.name === 'api') {
if (state.publicProperties.includes(node.key.name)) {
// TODO [#5032]: Harmonize errors thrown in `@lwc/ssr-compiler`
throw new Error(
`LWC1112: @api get ${node.key.name} and @api set ${node.key.name} detected in class declaration. Only one of the two needs to be decorated with @api.`
);
}
state.publicProperties.push(node.key.name);
}

switch (node.key.name) {
Expand Down Expand Up @@ -269,8 +277,8 @@ export default function compileJS(
tmplExplicitImports: null,
cssExplicitImports: null,
staticStylesheetIds: null,
publicFields: [],
privateFields: [],
publicProperties: [],
privateProperties: [],
wireAdapters: [],
experimentalDynamicComponent: options.experimentalDynamicComponent,
importManager: new ImportManager(),
Expand Down
8 changes: 4 additions & 4 deletions packages/@lwc/ssr-compiler/src/compile-js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export interface ComponentMetaState {
cssExplicitImports: Map<string, string> | null;
// the set of variable names associated with explicitly imported CSS files
staticStylesheetIds: Set<string> | null;
// the public (`@api`-annotated) fields of the component class
publicFields: Array<string>;
// the private fields of the component class
privateFields: Array<string>;
// the public (`@api`-annotated) properties of the component class
publicProperties: Array<string>;
// the private properties of the component class
privateProperties: Array<string>;
// indicates whether the LightningElement has any wired props
wireAdapters: WireAdapter[];
// dynamic imports configuration
Expand Down
8 changes: 4 additions & 4 deletions packages/@lwc/ssr-runtime/src/lightning-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export class LightningElement implements PropsAvailableAtConstruction {
[SYMBOL__SET_INTERNALS](
props: Properties,
attrs: Attributes,
publicFields: Set<string>,
privateFields: Set<string>
publicProperties: Set<string>,
privateProperties: Set<string>
) {
this.#props = props;
this.#attrs = attrs;
Expand All @@ -91,9 +91,9 @@ export class LightningElement implements PropsAvailableAtConstruction {
for (const propName of keys(props)) {
const attrName = htmlPropertyToAttribute(propName);
if (
publicFields.has(propName) ||
publicProperties.has(propName) ||
((REFLECTIVE_GLOBAL_PROPERTY_SET.has(propName) || isAriaAttribute(attrName)) &&
!privateFields.has(propName))
!privateProperties.has(propName))
) {
// For props passed from parents to children, they are intended to be read-only
// to avoid a child mutating its parent's state
Expand Down