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

chore: restrict @salesforce/lds imports #111

Merged
merged 6 commits into from
Feb 8, 2023

Conversation

scarrawaySF
Copy link
Contributor

@scarrawaySF scarrawaySF commented Feb 2, 2023

restricting access to @salesforce/lds modules as they shouldn't be accessed directly in consuming code.

@scarrawaySF scarrawaySF changed the title chore: restrict @salesforce/graphql imports chore: restrict @salesforce/lds imports Feb 2, 2023
@scarrawaySF scarrawaySF marked this pull request as ready for review February 2, 2023 20:33
@scarrawaySF
Copy link
Contributor Author

@nolanlawson are you the right person to ask for a review here? Let me know if there's another process I should follow

@@ -289,5 +289,17 @@ module.exports = {

// LWC import validation
'@lwc/lwc/no-disallowed-lwc-imports': 'error',

// Disable any direct importing of LDS artifacts generated by the LWC compiler
'no-restricted-imports': [

Choose a reason for hiding this comment

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

Should we be more explicit with this rule name? I guess I could see this being a good place to group multiple patterns for restricted imports

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 just a built-in rule for eslint, so I didn't want to reinvent the wheel. We can use a custom error message if we really want to though

Copy link

Choose a reason for hiding this comment

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

A custom error message sounds better

base.js Outdated
{
patterns: [
{
group: ['@salesforce/lds/*'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this pattern work when doing import foo from '@salesforce/lds'? It looks like the extra / would prevent that from being covered.

Copy link
Contributor

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

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

See above comment

base.js Outdated
@@ -296,7 +296,7 @@ module.exports = {
{
patterns: [
{
group: ['@salesforce/lds/*'],
group: ['@salesforce/lds**'],
Copy link
Contributor

Choose a reason for hiding this comment

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

AIUI this also captures @salesforce/ldsfoobar, which could be owned by some other team. (Unlikely, I admit, but possible.) I think this might work instead?

Suggested change
group: ['@salesforce/lds**'],
group: ['@salesforce/lds', '@salesforce/lds/**'],

Copy link
Contributor

Choose a reason for hiding this comment

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

Note the **, I believe that covers deep imports like @salesforce/lds/foo/bar/baz?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah good catch 👍 Should be updated now. Let me know if you have any other concerns


describe('should include no-restricted-imports', () => {
describe('prevents imports from @salesforce/lds', () => {
it('should prevent nested imports', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: The two(or 3) test cases share a lot of code. Suggestion: Create a function that accepts the source to lint and the messages to expect. Have the three tests call that function.

@ravijayaramappa
Copy link
Contributor

@scarrawaySF Is your team planning to start using import foo from '@salesforce/lds'; statements in generated internal modules?

lwc-platform-public(the metadata collector for LWCs) currently validates allowed usages for @salesforce/xxx imports. I do not see @salesforce/lds as an allowed pattern, so importing this should already be dis-allowed in LWCs.

* @param {string} expectedMessages[].ruleId - The lint rule id that should fail.
* @param {string} [expectedMessages[].message] - The message that the lint rule should throw.
*/
async function setupBaseListConfigAndAssertMessages(text, expectedMessages = []) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ravijayaramappa - I've added this function directly in this test for now. I think a good followup might be to extend it to allow passing in the eslint config, then it can be used across all the unit tests? Let me know if you want me to try and tackle that here or not.

@scarrawaySF
Copy link
Contributor Author

@scarrawaySF Is your team planning to start using import foo from '@salesforce/lds'; statements in generated internal modules?

lwc-platform-public(the metadata collector for LWCs) currently validates allowed usages for @salesforce/xxx imports. I do not see @salesforce/lds as an allowed pattern, so importing this should already be dis-allowed in LWCs.

Yes that's the idea. We're externalizing GraphQL documents that we've parsed to their own modules, then they'll be imported into the components for use by the adapter.

@scarrawaySF scarrawaySF merged commit e452677 into salesforce:master Feb 8, 2023
@scarrawaySF scarrawaySF deleted the add-restricted-import branch February 8, 2023 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants