-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow computed properties in @wire if they are constants or pri…
…mitives @W-14785085 (#3955) * refactor(@wire): split parameter validation into individual methods * chore(tests): remove duplicate tests duplicate of decorator-accepts-a-member-expression * test(@wire): add tests for spreads, methods, and numbers * test(@wire): update test for computed identifiers and primitives * feat: allow computed properties that are constants or primitive literals * refactor: less nesting * test(@wire): add test for template literal computed prop * test(@wire): add test for let variable computed prop * test(@wire): add test for expression in computed prop * test(@wire): add test for regexp literal computed prop * test(@wire): add test for bigint literal computed prop * chore: add github issues to TODOs
- Loading branch information
Showing
20 changed files
with
281 additions
and
48 deletions.
There are no files selected for viewing
7 changes: 5 additions & 2 deletions
7
...wire-provider-member-expression/actual.js → ...decorator/ignores-object-method/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { Foo } from "data-service"; | ||
import { getFoo } from "data-service"; | ||
export default class Test extends LightningElement { | ||
@wire(Foo.Bar, {}) wiredProp; | ||
@wire(getFoo, { | ||
method() {} | ||
}) | ||
wiredProp; | ||
} |
8 changes: 5 additions & 3 deletions
8
...re-provider-member-expression/expected.js → ...corator/ignores-object-method/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...l-plugin-component/src/__tests__/fixtures/wire-decorator/ignores-spread-element/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
const spreadMe = { | ||
key1: "$prop2" | ||
} | ||
export default class Test extends LightningElement { | ||
@wire(getFoo, { | ||
...spreadMe, | ||
...({key2: "$prop2"}) | ||
}) | ||
wiredProp; | ||
} |
31 changes: 31 additions & 0 deletions
31
...plugin-component/src/__tests__/fixtures/wire-decorator/ignores-spread-element/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { registerDecorators as _registerDecorators, registerComponent as _registerComponent, LightningElement } from "lwc"; | ||
import _tmpl from "./test.html"; | ||
import { getFoo } from "data-service"; | ||
const spreadMe = { | ||
key1: "$prop2" | ||
}; | ||
class Test extends LightningElement { | ||
wiredProp; | ||
/*LWC compiler vX.X.X*/ | ||
} | ||
_registerDecorators(Test, { | ||
wire: { | ||
wiredProp: { | ||
adapter: getFoo, | ||
dynamic: [], | ||
config: function ($cmp) { | ||
return { | ||
...spreadMe, | ||
...{ | ||
key2: "$prop2" | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
}); | ||
export default _registerComponent(Test, { | ||
tmpl: _tmpl, | ||
sel: "lwc-test", | ||
apiVersion: 9999999 | ||
}); |
8 changes: 8 additions & 0 deletions
8
...t/src/__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-expression/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
const symbol = Symbol.for("key"); | ||
export default class Test extends LightningElement { | ||
// accidentally an array expression = oops! | ||
@wire(getFoo, { [[symbol]]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredFoo; | ||
} |
10 changes: 10 additions & 0 deletions
10
.../src/__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-expression/error.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"message": "LWC1200: Computed property in @wire config must be a constant or primitive literal.", | ||
"loc": { | ||
"line": 6, | ||
"column": 19, | ||
"start": 237, | ||
"length": 8 | ||
}, | ||
"filename": "test.js" | ||
} |
7 changes: 7 additions & 0 deletions
7
...src/__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-let-variable/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
let key1 = 'key1' | ||
export default class Test extends LightningElement { | ||
@wire(getFoo, { [key1]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredFoo; | ||
} |
10 changes: 10 additions & 0 deletions
10
...rc/__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-let-variable/error.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"message": "LWC1200: Computed property in @wire config must be a constant or primitive literal.", | ||
"loc": { | ||
"line": 5, | ||
"column": 19, | ||
"start": 175, | ||
"length": 4 | ||
}, | ||
"filename": "test.js" | ||
} |
6 changes: 6 additions & 0 deletions
6
...c/__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-regexp-literal/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
export default class Test extends LightningElement { | ||
@wire(getFoo, { [/key1/]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredFoo; | ||
} |
10 changes: 10 additions & 0 deletions
10
.../__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-regexp-literal/error.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"message": "LWC1200: Computed property in @wire config must be a constant or primitive literal.", | ||
"loc": { | ||
"line": 4, | ||
"column": 19, | ||
"start": 157, | ||
"length": 6 | ||
}, | ||
"filename": "test.js" | ||
} |
6 changes: 6 additions & 0 deletions
6
...__tests__/fixtures/wire-decorator/throws-when-computed-prop-is-template-literal/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
export default class Test extends LightningElement { | ||
@wire(getFoo, { [`key1`]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredFoo; | ||
} |
10 changes: 10 additions & 0 deletions
10
..._tests__/fixtures/wire-decorator/throws-when-computed-prop-is-template-literal/error.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"message": "LWC1199: Cannot use a template literal as a computed property key. Instead, use a string or extract the value to a constant.", | ||
"loc": { | ||
"line": 4, | ||
"column": 19, | ||
"start": 157, | ||
"length": 6 | ||
}, | ||
"filename": "test.js" | ||
} |
22 changes: 15 additions & 7 deletions
22
...-component/src/__tests__/fixtures/wire-decorator/transforms-computed-properties/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo, getBar } from "data-service"; | ||
const key1 = Symbol.for("key"); | ||
export default class Test extends LightningElement { | ||
@wire(getBar, { [key1]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredBar; | ||
|
||
// eslint-disable-next-line no-useless-computed-key | ||
@wire(getFoo, { ["key1"]: "$prop1", key2: ["fixed", "array"] }) | ||
wiredFoo; | ||
const symbol = Symbol.for("key"); | ||
export default class Test extends LightningElement { | ||
@wire(getFoo, { | ||
[symbol]: '$prop' | ||
}) | ||
wiredIdentifier; | ||
|
||
@wire(getBar, { | ||
['computedStringLiteral']: '$prop', | ||
[123n]: '$prop', | ||
[321]: '$prop', | ||
[null]: '$prop', | ||
[undefined]: '$prop' | ||
}) | ||
wiredPrimitives; | ||
} |
30 changes: 15 additions & 15 deletions
30
...omponent/src/__tests__/fixtures/wire-decorator/transforms-computed-properties/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...plugin-component/src/__tests__/fixtures/wire-decorator/transforms-numeric-props/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { wire, LightningElement } from "lwc"; | ||
import { getFoo } from "data-service"; | ||
export default class Test extends LightningElement { | ||
// Did you know numeric literals can be used as property keys? This becomes "123"! | ||
@wire(getFoo, { 1.2_3e2: "$prop" }) | ||
wiredProp; | ||
} |
26 changes: 26 additions & 0 deletions
26
...ugin-component/src/__tests__/fixtures/wire-decorator/transforms-numeric-props/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { registerDecorators as _registerDecorators, registerComponent as _registerComponent, LightningElement } from "lwc"; | ||
import _tmpl from "./test.html"; | ||
import { getFoo } from "data-service"; | ||
class Test extends LightningElement { | ||
// Did you know numeric literals can be used as property keys? This becomes "123"! | ||
wiredProp; | ||
/*LWC compiler vX.X.X*/ | ||
} | ||
_registerDecorators(Test, { | ||
wire: { | ||
wiredProp: { | ||
adapter: getFoo, | ||
dynamic: ["123"], | ||
config: function ($cmp) { | ||
return { | ||
1.2_3e2: $cmp.prop | ||
}; | ||
} | ||
} | ||
} | ||
}); | ||
export default _registerComponent(Test, { | ||
tmpl: _tmpl, | ||
sel: "lwc-test", | ||
apiVersion: 9999999 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.