Skip to content
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

Add allowList option to no-disallowed-lwc-imports #97

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/rules/no-disallowed-lwc-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,21 @@ import { LightningElement, wire, api } from 'lwc';
```

If you disable this rule, then you may import unstable or otherwise undesirable APIs from `lwc`.

### `allowlist`

The `allowlist` property overrides the default list of APIs that could be imported from the `lwc` package. It accepts an array of strings.

Examples of **incorrect** code:

```js
/* eslint lwc/valid-api: ["error", { "allowlist": ["LightningElement"] }] */
import { wire } from 'lwc';
```

Examples of **correct** code:

```js
/* eslint lwc/valid-api: ["error", { "allowlist": ["LightningElement"] }] */
import { LightningElement } from 'lwc';
```
30 changes: 25 additions & 5 deletions lib/rules/no-disallowed-lwc-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,30 @@ module.exports = {
category: 'LWC',
url: docUrl('no-disallowed-lwc-imports'),
},
schema: [],
schema: [
{
type: 'object',
properties: {
allowlist: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
],
},

create(context) {
// Use the API allow list provided by option to the ESLint rule, otherwise fallback to the
// default one.
let lwcAllowedApis = LWC_SUPPORTED_APIS;
if (context.options.length > 0 && context.options[0].allowlist) {
lwcAllowedApis = new Set(context.options[0].allowlist);
}

return {
ImportDeclaration(node) {
if (isLwcImport(node)) {
Expand Down Expand Up @@ -75,11 +95,11 @@ module.exports = {
});
} else if (
type === 'ImportSpecifier' &&
!LWC_SUPPORTED_APIS.has(imported.name)
!lwcAllowedApis.has(imported.name)
) {
// import { fake } from 'lwc'
context.report({
message: `Invalid import. "${imported.name}" is not a known and stable API.`,
message: `Invalid import. "${imported.name}" can't be imported from "lwc".`,
node: specifier,
});
}
Expand All @@ -98,10 +118,10 @@ module.exports = {
}
for (const specifier of specifiers) {
const { type, local } = specifier;
if (type === 'ExportSpecifier' && !LWC_SUPPORTED_APIS.has(local.name)) {
if (type === 'ExportSpecifier' && !lwcAllowedApis.has(local.name)) {
// export { fake } from 'lwc'
context.report({
message: `Invalid export. "${local.name}" is not a known and stable API.`,
message: `Invalid export. "${local.name}" can't be imported from "lwc".`,
node: specifier,
});
}
Expand Down
156 changes: 140 additions & 16 deletions test/lib/rules/no-disallowed-lwc-imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -29,7 +29,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -39,7 +39,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -49,7 +49,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -59,11 +59,11 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
{
message: new RegExp(`Invalid import. "AlsoBanned" is not a known and stable API.`),
message: new RegExp(`Invalid import. "AlsoBanned" can't be imported from "lwc".`),
},
],
},
Expand All @@ -72,11 +72,11 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid import. "ShouldNotImport" is not a known and stable API.`,
`Invalid import. "ShouldNotImport" can't be imported from "lwc".`,
),
},
{
message: new RegExp(`Invalid import. "AlsoBanned" is not a known and stable API.`),
message: new RegExp(`Invalid import. "AlsoBanned" can't be imported from "lwc".`),
},
],
},
Expand Down Expand Up @@ -139,7 +139,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -149,7 +149,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -159,7 +159,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -169,7 +169,7 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
],
Expand All @@ -179,11 +179,11 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
{
message: new RegExp(`Invalid export. "AlsoBanned" is not a known and stable API.`),
message: new RegExp(`Invalid export. "AlsoBanned" can't be imported from "lwc".`),
},
],
},
Expand All @@ -192,11 +192,11 @@ const invalidCases = [
errors: [
{
message: new RegExp(
`Invalid export. "ShouldNotImport" is not a known and stable API.`,
`Invalid export. "ShouldNotImport" can't be imported from "lwc".`,
),
},
{
message: new RegExp(`Invalid export. "AlsoBanned" is not a known and stable API.`),
message: new RegExp(`Invalid export. "AlsoBanned" can't be imported from "lwc".`),
},
],
},
Expand All @@ -210,6 +210,89 @@ const invalidCases = [
},
],
},

{
code: `import { LightningElement } from "lwc"`,
options: [
{
allowlist: [],
},
],
errors: [
{
message: new RegExp(
`Invalid import. "LightningElement" can't be imported from "lwc".`,
),
},
],
},
{
code: `export { LightningElement } from "lwc"`,
options: [
{
allowlist: [],
},
],
errors: [
{
message: new RegExp(
`Invalid export. "LightningElement" can't be imported from "lwc".`,
),
},
],
},
{
code: `import { api } from "lwc"`,
options: [
{
allowlist: ['LightningElement'],
},
],
errors: [
{
message: new RegExp(`Invalid import. "api" can't be imported from "lwc".`),
},
],
},
{
code: `import { Unknown as LightningElement } from "lwc"`,
options: [
{
allowlist: ['LightningElement'],
},
],
errors: [
{
message: new RegExp(`Invalid import. "Unknown" can't be imported from "lwc".`),
},
],
},
{
code: `export { api } from "lwc"`,
options: [
{
allowlist: ['LightningElement'],
},
],
errors: [
{
message: new RegExp(`Invalid export. "api" can't be imported from "lwc".`),
},
],
},
{
code: `export { Unknown as LightningElement } from "lwc"`,
options: [
{
allowlist: ['LightningElement'],
},
],
errors: [
{
message: new RegExp(`Invalid export. "Unknown" can't be imported from "lwc".`),
},
],
},
];

const validCases = [
Expand Down Expand Up @@ -276,6 +359,47 @@ const validCases = [
{
code: `export default 'foo'`,
},

{
code: `import { LightningElement } from "lwc"`,
options: [
{
allowlist: ['LightningElement', 'api'],
},
],
},
{
code: `import { LightningElement as LWCElement } from "lwc"`,
options: [
{
allowlist: ['LightningElement', 'api'],
},
],
},
{
code: `import { LightningElement, api } from "lwc"`,
options: [
{
allowlist: ['LightningElement', 'api'],
},
],
},
{
code: `export { LightningElement } from "lwc"`,
options: [
{
allowlist: ['LightningElement', 'api'],
},
],
},
{
code: `export { LightningElement, api } from "lwc"`,
options: [
{
allowlist: ['LightningElement', 'api'],
},
],
},
];

ruleTester.run('no-disallowed-lwc-imports', rule, {
Expand Down