Skip to content

Commit

Permalink
fix(ssr): update wire adapter validation @ W-17274788 (#4910)
Browse files Browse the repository at this point in the history
* test(ssr): add test for wire config object

* feat: add wire adapter param validation

* test: add some test fixtures for wire

* test: add test fixtures for wire

* test: refactor tests

* feat: validate that the adapter is an imported module

* feat: add config validation

* feat(ssr): fix wire adapter assumptions

* chore(fixtures): standardize output order

* chore(ssr): unexpect failures

* chore: linter fixes

---------

Co-authored-by: James Tu <j.tu@salesforce.com>
Co-authored-by: Nolan Lawson <nlawson@salesforce.com>
Co-authored-by: Eugene Kashida <ekashida@gmail.com>
  • Loading branch information
4 people authored Nov 21, 2024
1 parent fc811b7 commit e60891b
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<x-wire>
<template shadowrootmode="open">
Wire adapter value: {
: ,
0: 0,
variable: 4404,
prop: not magic,
array: [
value
],
fakeMagic: [
$cmpProp,
$ string in arrays aren&#x27;t magic
],
true: true,
array: [value],
fakeMagic: [$cmpProp,$ string in arrays aren&#x27;t magic],
false: false,
null: null,
Infinity: null,
magic: 123,
NaN: null,
: ,
magic: 123
null: null,
prop: not magic,
true: true,
undefined: undefined,
variable: 4404,
why: undefined,
}
</template>
</x-wire>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ export class adapter {
connect() {}

update(config) {
// Quotes are encoded in the output, which is ugly
this.dc(JSON.stringify(config, null, 4).replace(/"/g, ''));
// JSON.stringify serializes differently for engine-server/ssr-compiler, so we do it manually
const output = Object.entries(config)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, value]) => ` ${key}: ${JSON.stringify(value)},`)
.join('\n')
// Quotes are encoded as &quot; in the output, which makes it harder to read...
.replace(/"/g, '');

this.dc(`{\n${output}\n}`);
}

disconnect() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<x-wire>
<template shadowrootmode="open">
wired field value: {
key2: [
fixed,
array
],
key1: foo
key1: foo,
key2: [fixed,array],
}
</template>
</x-wire>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ export default class adapter {
connect() {}

update(config) {
// Quotes are encoded in the output, which is ugly
this.dc(JSON.stringify(config, null, 4).replace(/"/g, ''));
// JSON.stringify serializes differently for engine-server/ssr-compiler, so we do it manually
const output = Object.entries(config)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, value]) => ` ${key}: ${JSON.stringify(value)},`)
.join('\n')
// Quotes are encoded as &quot; in the output, which makes it harder to read...
.replace(/"/g, '');

this.dc(`{\n${output}\n}`);
}

disconnect() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<x-wire>
<template shadowrootmode="open">
wired field value: {
key2: [
fixed,
array
],
key1: foo
key1: foo,
key2: [fixed,array],
}
</template>
</x-wire>
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ export default class adapter {
connect() {}

update(config) {
// Quotes are encoded in the output, which is ugly
this.dc(JSON.stringify(config, null, 4).replace(/"/g, ''));
// JSON.stringify serializes differently for engine-server/ssr-compiler, so we do it manually
const output = Object.entries(config)
.sort(([a], [b]) => a.localeCompare(b))
.map(([key, value]) => ` ${key}: ${JSON.stringify(value)},`)
.join('\n')
// Quotes are encoded as &quot; in the output, which makes it harder to read...
.replace(/"/g, '');

this.dc(`{\n${output}\n}`);
}

disconnect() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,4 @@ export const expectedFailures = new Set([
'superclass/render-in-superclass/unused-default-in-subclass/index.js',
'superclass/render-in-superclass/unused-default-in-superclass/index.js',
'svgs/index.js',
'wire/config/index.js',
'wire/deep-reference/index.js',
'wire/field/index.js',
'wire/imported-member/index.js',
'wire/method/index.js',
]);
4 changes: 2 additions & 2 deletions packages/@lwc/ssr-compiler/src/compile-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const visitors: Visitors = {
is.identifier(decoratedExpression.callee) &&
decoratedExpression.callee.name === 'wire'
) {
catalogWireAdapters(state, node);
catalogWireAdapters(path, state);
state.privateFields.push(node.key.name);
} else {
state.privateFields.push(node.key.name);
Expand Down Expand Up @@ -111,7 +111,7 @@ const visitors: Visitors = {
is.identifier(decoratedExpression.callee) &&
decoratedExpression.callee.name === 'wire'
) {
catalogWireAdapters(state, node);
catalogWireAdapters(path, state);
}

switch (node.key.name) {
Expand Down
19 changes: 11 additions & 8 deletions packages/@lwc/ssr-compiler/src/compile-js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
*/

import type { traverse } from 'estree-toolkit';
import type { Node } from 'estree';
import type {
Identifier,
MemberExpression,
MethodDefinition,
Node,
ObjectExpression,
PropertyDefinition,
} from 'estree';

export type Visitors = Parameters<typeof traverse<Node, ComponentMetaState>>[1];

export interface WireAdapter {
fieldName: string;
adapterConstructorId: string;
config: Array<{
configKey: string;
referencedField: string;
}>;
fieldType: 'property' | 'method';
adapterId: Identifier | MemberExpression;
config: ObjectExpression;
field: MethodDefinition | PropertyDefinition;
}

export interface ComponentMetaState {
Expand Down
Loading

0 comments on commit e60891b

Please sign in to comment.