-
Notifications
You must be signed in to change notification settings - Fork 405
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
feat: allow computed properties in @wire if they are constants or primitives @W-14785085 #3955
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
dc1fe80
refactor(@wire): split parameter validation into individual methods
wjhsf 63eb2e5
chore(tests): remove duplicate tests
wjhsf d509cb6
test(@wire): add tests for spreads, methods, and numbers
wjhsf bae05e1
test(@wire): update test for computed identifiers and primitives
wjhsf 813982f
feat: allow computed properties that are constants or primitive literals
wjhsf 2731f9a
refactor: less nesting
wjhsf 81ddb0b
test(@wire): add test for template literal computed prop
wjhsf 5152552
test(@wire): add test for let variable computed prop
wjhsf 1f8255f
test(@wire): add test for expression in computed prop
wjhsf 676694d
test(@wire): add test for regexp literal computed prop
wjhsf 0c1e1a3
test(@wire): add test for bigint literal computed prop
wjhsf 186d426
chore: add github issues to TODOs
wjhsf 5bed7b5
Merge branch 'master' into wjh/helpful-wire-error
wjhsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fall through will probably never occur, because it should already be covered by the validation, but we should still play it safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add this as a comment here? You can also add
/* istanbul ignore next */
to drive home the point that this code should never get hit.