Skip to content

Commit e919d6b

Browse files
authored
feat: add support for pattern matching in rule no-unexpected-wire-adapter-usages (#98)
1 parent 74f3cc4 commit e919d6b

File tree

4 files changed

+72
-134
lines changed

4 files changed

+72
-134
lines changed

base.js

Lines changed: 12 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -16,120 +16,28 @@ const KNOWN_WIRE_ADAPTERS = [
1616
module: '@salesforce/**',
1717
identifier: '*',
1818
},
19+
// All commerce API adapters
1920
{
20-
module: 'commerce/cartApi',
21-
identifier: 'CartItemsAdapter',
22-
},
23-
{
24-
module: 'commerce/cartApi',
25-
identifier: 'CartSummaryAdapter',
26-
},
27-
{
28-
module: 'commerce/contextApi',
29-
identifier: 'AppContextAdapter',
30-
},
31-
{
32-
module: 'commerce/contextApi',
33-
identifier: 'SessionContextAdapter',
34-
},
35-
{
36-
module: 'commerce/recommendationsApi',
37-
identifier: 'ProductRecommendationsAdapter',
38-
},
39-
{
40-
module: 'commerce/productApi',
41-
identifier: 'ProductAdapter',
42-
},
43-
{
44-
module: 'commerce/productApi',
45-
identifier: 'ProductCategoryAdapter',
46-
},
47-
{
48-
module: 'commerce/productApi',
49-
identifier: 'ProductCategoryHierarchyAdapter',
50-
},
51-
{
52-
module: 'commerce/productApi',
53-
identifier: 'ProductCategoryPathAdapter',
54-
},
55-
{
56-
module: 'commerce/productApi',
57-
identifier: 'ProductPricingAdapter',
58-
},
59-
{
60-
module: 'experience/cmsDeliveryApi',
61-
identifier: 'getContent',
62-
},
63-
{
64-
module: 'experience/cmsEditorApi',
65-
identifier: 'getContent',
66-
},
67-
{
68-
module: 'experience/cmsEditorApi',
69-
identifier: 'getContext',
21+
module: 'commerce/*Api',
22+
identifier: '*',
7023
},
24+
// All experience API adapters
7125
{
72-
module: 'experience/navigationMenuApi',
73-
identifier: 'getNavigationMenu',
26+
module: 'experience/*Api',
27+
identifier: '*',
7428
},
7529
];
7630

7731
const WIRE_ADAPTERS_WITH_RESTRICTED_USE = [
32+
// All commerce API adapters
7833
{
79-
module: 'commerce/cartApi',
80-
identifier: 'CartItemsAdapter',
81-
},
82-
{
83-
module: 'commerce/cartApi',
84-
identifier: 'CartSummaryAdapter',
85-
},
86-
{
87-
module: 'commerce/contextApi',
88-
identifier: 'AppContextAdapter',
89-
},
90-
{
91-
module: 'commerce/contextApi',
92-
identifier: 'SessionContextAdapter',
93-
},
94-
{
95-
module: 'commerce/recommendationsApi',
96-
identifier: 'ProductRecommendationsAdapter',
97-
},
98-
{
99-
module: 'commerce/productApi',
100-
identifier: 'ProductAdapter',
101-
},
102-
{
103-
module: 'commerce/productApi',
104-
identifier: 'ProductCategoryAdapter',
105-
},
106-
{
107-
module: 'commerce/productApi',
108-
identifier: 'ProductCategoryHierarchyAdapter',
109-
},
110-
{
111-
module: 'commerce/productApi',
112-
identifier: 'ProductCategoryPathAdapter',
113-
},
114-
{
115-
module: 'commerce/productApi',
116-
identifier: 'ProductPricingAdapter',
117-
},
118-
{
119-
module: 'experience/cmsDeliveryApi',
120-
identifier: 'getContent',
121-
},
122-
{
123-
module: 'experience/cmsEditorApi',
124-
identifier: 'getContent',
125-
},
126-
{
127-
module: 'experience/cmsEditorApi',
128-
identifier: 'getContext',
34+
module: 'commerce/*Api',
35+
identifier: '*',
12936
},
37+
// All experience API adapters
13038
{
131-
module: 'experience/navigationMenuApi',
132-
identifier: 'getNavigationMenu',
39+
module: 'experience/*Api',
40+
identifier: '*',
13341
},
13442
{
13543
module: 'lightning/analyticsWaveApi',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"semver": "^7.3.7"
2727
},
2828
"devDependencies": {
29-
"@lwc/eslint-plugin-lwc": "^1.2.1",
29+
"@lwc/eslint-plugin-lwc": "^1.3.0",
3030
"@salesforce/eslint-plugin-lightning": "^1.0.0",
3131
"eslint": "^8.14.0",
3232
"eslint-plugin-import": "^2.26.0",

test/base.js

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,41 @@ describe('base config', () => {
4949
},
5050
});
5151

52-
const results = await cli.lintText(`
53-
import { wire } from 'lwc';
54-
import { Baz } from 'c/cmp';
55-
class Foo {
56-
@wire(Baz)
57-
foo;
58-
}
59-
`);
60-
61-
const { messages } = results[0];
62-
assert.equal(messages.length, 1);
63-
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unknown-wire-adapters');
52+
const expectedFailures = [
53+
'c/cmp',
54+
'commerce/cmp',
55+
'commerce/cmpApiInternal',
56+
'experience/cmp',
57+
];
58+
for (const bundleName of expectedFailures) {
59+
const results = await cli.lintText(`
60+
import { wire } from 'lwc';
61+
import { getAdapter } from '${bundleName}';
62+
class Foo {
63+
@wire(getAdapter)
64+
wiredProp;
65+
}
66+
`);
67+
68+
const { messages } = results[0];
69+
assert.equal(messages.length, 1);
70+
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unknown-wire-adapters');
71+
}
72+
73+
const expectedSuccesses = ['commerce/cmpApi', 'experience/cmpApi'];
74+
for (const bundleName of expectedSuccesses) {
75+
const results = await cli.lintText(`
76+
import { wire } from 'lwc';
77+
import { getAdapter } from '${bundleName}';
78+
class Foo {
79+
@wire(getAdapter)
80+
wiredProp;
81+
}
82+
`);
83+
84+
const { messages } = results[0];
85+
assert.equal(messages.length, 0);
86+
}
6487
});
6588

6689
it('should include @lwc/lwc/no-unexpected-wire-adapter-usages', async () => {
@@ -71,19 +94,26 @@ describe('base config', () => {
7194
},
7295
});
7396

74-
const results = await cli.lintText(`
75-
import { wire } from 'lwc';
76-
import { CurrentPageReference } from 'lightning/navigation';
77-
const reference = CurrentPageReference;
78-
class Foo {
79-
@wire(CurrentPageReference)
80-
foo;
81-
}
82-
`);
83-
84-
const { messages } = results[0];
85-
assert.equal(messages.length, 1);
86-
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unexpected-wire-adapter-usages');
97+
const expectedFailures = [
98+
['lightning/navigation', 'CurrentPageReference'],
99+
['commerce/cmpApi', 'getAdapter'],
100+
['experience/cmpApi', 'getAdapter'],
101+
];
102+
for (const [bundleName, cmpName] of expectedFailures) {
103+
const results = await cli.lintText(`
104+
import { wire } from 'lwc';
105+
import { ${cmpName} } from '${bundleName}';
106+
const reference = ${cmpName};
107+
class Foo {
108+
@wire(${cmpName})
109+
wiredProp;
110+
}
111+
`);
112+
113+
const { messages } = results[0];
114+
assert.equal(messages.length, 1);
115+
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unexpected-wire-adapter-usages');
116+
}
87117
});
88118

89119
it('should include @lwc/lwc/no-disallowed-lwc-imports', async () => {

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@
320320
"@jridgewell/resolve-uri" "^3.0.3"
321321
"@jridgewell/sourcemap-codec" "^1.4.10"
322322

323-
"@lwc/eslint-plugin-lwc@^1.2.1":
324-
version "1.2.1"
325-
resolved "https://registry.yarnpkg.com/@lwc/eslint-plugin-lwc/-/eslint-plugin-lwc-1.2.1.tgz#4953dff14d2dabad22c967d4fd4b666ef45dbcd9"
326-
integrity sha512-13a5XOultegAc4i4SyFk9367KOvvRy40zbRSKkApcNa9uMI6n9/C/yFMvwve6Mp6PaIsGXlKFWNkzwj+nADEzw==
323+
"@lwc/eslint-plugin-lwc@^1.3.0":
324+
version "1.3.0"
325+
resolved "https://registry.yarnpkg.com/@lwc/eslint-plugin-lwc/-/eslint-plugin-lwc-1.3.0.tgz#eb47e3bb58228438f5ac869c78923848326d5ef8"
326+
integrity sha512-WkHhIW+C23QTzuUTs3htjp/klZiKCMS/wUroXs7WqtQdZxhBmDLbdR+1WOp/liL27L80KVjdCq9pvLtS1UQKjg==
327327
dependencies:
328328
minimatch "^5.0.1"
329329

0 commit comments

Comments
 (0)