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

feat: add no-disallowed-lwc-imports rule #85

Merged
merged 9 commits into from
Apr 6, 2022

Conversation

nolanlawson
Copy link
Contributor

Fixes: salesforce/lwc#1284 and W-10151021

Adds a rule to disallow restricted APIs from the lwc package.


```js
export * from 'lwc';
```
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incidentally, our current Babel validator allows export * from 'lwc' and export * as foo from 'lwc'. This rule fixes that.


// From "@lwc/engine-server"
'renderComponent',
]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is based on our current list.

import { LightningElement, wire, api } from 'lwc';
```

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we make this rule configurable to allow importing unstable API? Maybe it's something we can include in the future if needed.

{
  rules: {
    'no-disallowed-lwc-imports': ["error", { allowUnstable: true }]
  }
}

or:

{
  rules: {
    'no-disallowed-lwc-imports': ["error", { allow: ['swapComponent'] }]
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary to me. People who want to use unstable imports can disable the rule entirely.

The value-add of the other things that the rule checks is low IMO. Everything else that it checks for (e.g. import nonexistentDefault from 'lwc') will also throw an error either by Rollup or at runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

lib/rules/no-disallowed-lwc-imports.js Outdated Show resolved Hide resolved
lib/rules/no-disallowed-lwc-imports.js Outdated Show resolved Hide resolved
lib/rules/no-disallowed-lwc-imports.js Outdated Show resolved Hide resolved
lib/rules/no-disallowed-lwc-imports.js Show resolved Hide resolved
@nolanlawson nolanlawson requested a review from pmdartus March 3, 2022 19:02
lib/rules/no-disallowed-lwc-imports.js Outdated Show resolved Hide resolved
lib/rules/no-disallowed-lwc-imports.js Outdated Show resolved Hide resolved
import { LightningElement, wire, api } from 'lwc';
```

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

@nolanlawson
Copy link
Contributor Author

Delaying until CLCO

{
code: `export {} from "some-other-package"`,
},
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a testcase for export { LightningElement as default } from "lwc";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

@ravijayaramappa ravijayaramappa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one suggestion for an additional test case

@nolanlawson
Copy link
Contributor Author

@jodarove Are we good to merge this PR and salesforce/lwc#1284 to master?

@jodarove
Copy link
Contributor

jodarove commented Apr 6, 2022

yes @nolanlawson

@nolanlawson nolanlawson merged commit 31cf008 into master Apr 6, 2022
@nolanlawson nolanlawson deleted the nolan/import-rule-squashed branch April 6, 2022 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move import specifier validation for lwc module from compiler to linter
4 participants