Skip to content

Commit

Permalink
feat: add support for pattern matching in rule no-unexpected-wire-ada…
Browse files Browse the repository at this point in the history
…pter-usages (#98)
  • Loading branch information
seckardt authored Jun 8, 2022
1 parent 74f3cc4 commit e919d6b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 134 deletions.
116 changes: 12 additions & 104 deletions base.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,120 +16,28 @@ const KNOWN_WIRE_ADAPTERS = [
module: '@salesforce/**',
identifier: '*',
},
// All commerce API adapters
{
module: 'commerce/cartApi',
identifier: 'CartItemsAdapter',
},
{
module: 'commerce/cartApi',
identifier: 'CartSummaryAdapter',
},
{
module: 'commerce/contextApi',
identifier: 'AppContextAdapter',
},
{
module: 'commerce/contextApi',
identifier: 'SessionContextAdapter',
},
{
module: 'commerce/recommendationsApi',
identifier: 'ProductRecommendationsAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryHierarchyAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryPathAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductPricingAdapter',
},
{
module: 'experience/cmsDeliveryApi',
identifier: 'getContent',
},
{
module: 'experience/cmsEditorApi',
identifier: 'getContent',
},
{
module: 'experience/cmsEditorApi',
identifier: 'getContext',
module: 'commerce/*Api',
identifier: '*',
},
// All experience API adapters
{
module: 'experience/navigationMenuApi',
identifier: 'getNavigationMenu',
module: 'experience/*Api',
identifier: '*',
},
];

const WIRE_ADAPTERS_WITH_RESTRICTED_USE = [
// All commerce API adapters
{
module: 'commerce/cartApi',
identifier: 'CartItemsAdapter',
},
{
module: 'commerce/cartApi',
identifier: 'CartSummaryAdapter',
},
{
module: 'commerce/contextApi',
identifier: 'AppContextAdapter',
},
{
module: 'commerce/contextApi',
identifier: 'SessionContextAdapter',
},
{
module: 'commerce/recommendationsApi',
identifier: 'ProductRecommendationsAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryHierarchyAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductCategoryPathAdapter',
},
{
module: 'commerce/productApi',
identifier: 'ProductPricingAdapter',
},
{
module: 'experience/cmsDeliveryApi',
identifier: 'getContent',
},
{
module: 'experience/cmsEditorApi',
identifier: 'getContent',
},
{
module: 'experience/cmsEditorApi',
identifier: 'getContext',
module: 'commerce/*Api',
identifier: '*',
},
// All experience API adapters
{
module: 'experience/navigationMenuApi',
identifier: 'getNavigationMenu',
module: 'experience/*Api',
identifier: '*',
},
{
module: 'lightning/analyticsWaveApi',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"semver": "^7.3.7"
},
"devDependencies": {
"@lwc/eslint-plugin-lwc": "^1.2.1",
"@lwc/eslint-plugin-lwc": "^1.3.0",
"@salesforce/eslint-plugin-lightning": "^1.0.0",
"eslint": "^8.14.0",
"eslint-plugin-import": "^2.26.0",
Expand Down
80 changes: 55 additions & 25 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,41 @@ describe('base config', () => {
},
});

const results = await cli.lintText(`
import { wire } from 'lwc';
import { Baz } from 'c/cmp';
class Foo {
@wire(Baz)
foo;
}
`);

const { messages } = results[0];
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unknown-wire-adapters');
const expectedFailures = [
'c/cmp',
'commerce/cmp',
'commerce/cmpApiInternal',
'experience/cmp',
];
for (const bundleName of expectedFailures) {
const results = await cli.lintText(`
import { wire } from 'lwc';
import { getAdapter } from '${bundleName}';
class Foo {
@wire(getAdapter)
wiredProp;
}
`);

const { messages } = results[0];
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unknown-wire-adapters');
}

const expectedSuccesses = ['commerce/cmpApi', 'experience/cmpApi'];
for (const bundleName of expectedSuccesses) {
const results = await cli.lintText(`
import { wire } from 'lwc';
import { getAdapter } from '${bundleName}';
class Foo {
@wire(getAdapter)
wiredProp;
}
`);

const { messages } = results[0];
assert.equal(messages.length, 0);
}
});

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

const results = await cli.lintText(`
import { wire } from 'lwc';
import { CurrentPageReference } from 'lightning/navigation';
const reference = CurrentPageReference;
class Foo {
@wire(CurrentPageReference)
foo;
}
`);

const { messages } = results[0];
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unexpected-wire-adapter-usages');
const expectedFailures = [
['lightning/navigation', 'CurrentPageReference'],
['commerce/cmpApi', 'getAdapter'],
['experience/cmpApi', 'getAdapter'],
];
for (const [bundleName, cmpName] of expectedFailures) {
const results = await cli.lintText(`
import { wire } from 'lwc';
import { ${cmpName} } from '${bundleName}';
const reference = ${cmpName};
class Foo {
@wire(${cmpName})
wiredProp;
}
`);

const { messages } = results[0];
assert.equal(messages.length, 1);
assert.equal(messages[0].ruleId, '@lwc/lwc/no-unexpected-wire-adapter-usages');
}
});

it('should include @lwc/lwc/no-disallowed-lwc-imports', async () => {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@
"@jridgewell/resolve-uri" "^3.0.3"
"@jridgewell/sourcemap-codec" "^1.4.10"

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

Expand Down

0 comments on commit e919d6b

Please sign in to comment.