Skip to content

Commit e60891b

Browse files
wjhsfjmsjtunolanlawsonekashida
authored
fix(ssr): update wire adapter validation @ W-17274788 (#4910)
* 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>
1 parent fc811b7 commit e60891b

File tree

11 files changed

+194
-143
lines changed

11 files changed

+194
-143
lines changed
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
<x-wire>
22
<template shadowrootmode="open">
33
Wire adapter value: {
4+
: ,
45
0: 0,
5-
variable: 4404,
6-
prop: not magic,
7-
array: [
8-
value
9-
],
10-
fakeMagic: [
11-
$cmpProp,
12-
$ string in arrays aren&#x27;t magic
13-
],
14-
true: true,
6+
array: [value],
7+
fakeMagic: [$cmpProp,$ string in arrays aren&#x27;t magic],
158
false: false,
16-
null: null,
179
Infinity: null,
10+
magic: 123,
1811
NaN: null,
19-
: ,
20-
magic: 123
12+
null: null,
13+
prop: not magic,
14+
true: true,
15+
undefined: undefined,
16+
variable: 4404,
17+
why: undefined,
2118
}
2219
</template>
2320
</x-wire>

packages/@lwc/engine-server/src/__tests__/fixtures/wire/config/modules/x/wire/adapter.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ export class adapter {
66
connect() {}
77

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

1320
disconnect() {}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<x-wire>
22
<template shadowrootmode="open">
33
wired field value: {
4-
key2: [
5-
fixed,
6-
array
7-
],
8-
key1: foo
4+
key1: foo,
5+
key2: [fixed,array],
96
}
107
</template>
118
</x-wire>

packages/@lwc/engine-server/src/__tests__/fixtures/wire/field/modules/x/adapter/adapter.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ export default class adapter {
66
connect() {}
77

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

1320
disconnect() {}
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<x-wire>
22
<template shadowrootmode="open">
33
wired field value: {
4-
key2: [
5-
fixed,
6-
array
7-
],
8-
key1: foo
4+
key1: foo,
5+
key2: [fixed,array],
96
}
107
</template>
118
</x-wire>

packages/@lwc/engine-server/src/__tests__/fixtures/wire/method/modules/x/adapter/adapter.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ export default class adapter {
66
connect() {}
77

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

1320
disconnect() {}

packages/@lwc/ssr-compiler/src/__tests__/utils/expected-failures.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,4 @@ export const expectedFailures = new Set([
5151
'superclass/render-in-superclass/unused-default-in-subclass/index.js',
5252
'superclass/render-in-superclass/unused-default-in-superclass/index.js',
5353
'svgs/index.js',
54-
'wire/config/index.js',
55-
'wire/deep-reference/index.js',
56-
'wire/field/index.js',
57-
'wire/imported-member/index.js',
58-
'wire/method/index.js',
5954
]);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const visitors: Visitors = {
7474
is.identifier(decoratedExpression.callee) &&
7575
decoratedExpression.callee.name === 'wire'
7676
) {
77-
catalogWireAdapters(state, node);
77+
catalogWireAdapters(path, state);
7878
state.privateFields.push(node.key.name);
7979
} else {
8080
state.privateFields.push(node.key.name);
@@ -111,7 +111,7 @@ const visitors: Visitors = {
111111
is.identifier(decoratedExpression.callee) &&
112112
decoratedExpression.callee.name === 'wire'
113113
) {
114-
catalogWireAdapters(state, node);
114+
catalogWireAdapters(path, state);
115115
}
116116

117117
switch (node.key.name) {

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
*/
77

88
import type { traverse } from 'estree-toolkit';
9-
import type { Node } from 'estree';
9+
import type {
10+
Identifier,
11+
MemberExpression,
12+
MethodDefinition,
13+
Node,
14+
ObjectExpression,
15+
PropertyDefinition,
16+
} from 'estree';
1017

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

1320
export interface WireAdapter {
14-
fieldName: string;
15-
adapterConstructorId: string;
16-
config: Array<{
17-
configKey: string;
18-
referencedField: string;
19-
}>;
20-
fieldType: 'property' | 'method';
21+
adapterId: Identifier | MemberExpression;
22+
config: ObjectExpression;
23+
field: MethodDefinition | PropertyDefinition;
2124
}
2225

2326
export interface ComponentMetaState {

0 commit comments

Comments
 (0)