Skip to content

Commit e452677

Browse files
authored
chore: restrict @salesforce/lds imports (#111)
* chore: restrict @salesforce/graphql imports * chore: rename to lds * chore: custom error * chore: comments * fix: dont prevent imports from not lds * refactor: reduce code duplication for tests
1 parent ed61265 commit e452677

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

base.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,5 +289,19 @@ module.exports = {
289289

290290
// LWC import validation
291291
'@lwc/lwc/no-disallowed-lwc-imports': 'error',
292+
293+
// Disable any direct importing of LDS artifacts generated by the LWC compiler
294+
'no-restricted-imports': [
295+
'error',
296+
{
297+
patterns: [
298+
{
299+
group: ['@salesforce/lds', '@salesforce/lds/**'],
300+
message:
301+
'Please do not import from @salesforce/lds, these modules are ephemeral and could change at any time.',
302+
},
303+
],
304+
},
305+
],
292306
},
293307
};

test/base.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,72 @@ describe('base config', () => {
132132
assert.equal(messages.length, 1);
133133
assert.equal(messages[0].ruleId, '@lwc/lwc/no-disallowed-lwc-imports');
134134
});
135+
136+
describe('should include no-restricted-imports', () => {
137+
describe('prevents imports from @salesforce/lds', () => {
138+
it('should prevent nested imports', async () => {
139+
setupBaseListConfigAndAssertMessages(
140+
`
141+
import { abc } from '@salesforce/lds/test';
142+
`,
143+
[
144+
{
145+
ruleId: 'no-restricted-imports',
146+
message:
147+
"'@salesforce/lds/test' import is restricted from being used by a pattern. Please do not import from @salesforce/lds, these modules are ephemeral and could change at any time.",
148+
},
149+
],
150+
);
151+
});
152+
153+
it('should prevent imports from restricted modules', async () => {
154+
setupBaseListConfigAndAssertMessages(
155+
`
156+
import foo from '@salesforce/lds';
157+
`,
158+
[
159+
{
160+
ruleId: 'no-restricted-imports',
161+
message:
162+
"'@salesforce/lds' import is restricted from being used by a pattern. Please do not import from @salesforce/lds, these modules are ephemeral and could change at any time.",
163+
},
164+
],
165+
);
166+
});
167+
168+
it('does not prevent imports from similarly named modules', async () => {
169+
setupBaseListConfigAndAssertMessages(
170+
`
171+
import foo from '@salesforce/ldsnotlds';
172+
`,
173+
);
174+
});
175+
});
176+
});
135177
});
178+
179+
/**
180+
* Sets up the linter and runs it against the given text.
181+
* @constructor
182+
* @param {string} text - The text to lint
183+
* @param {Object[]} expectedMessages - The employees who are responsible for the project.
184+
* @param {string} expectedMessages[].ruleId - The lint rule id that should fail.
185+
* @param {string} [expectedMessages[].message] - The message that the lint rule should throw.
186+
*/
187+
async function setupBaseListConfigAndAssertMessages(text, expectedMessages = []) {
188+
const cli = new eslint.ESLint({
189+
useEslintrc: false,
190+
baseConfig: {
191+
extends: '@salesforce/eslint-config-lwc/base',
192+
},
193+
});
194+
const results = await cli.lintText(text);
195+
const { messages } = results[0];
196+
assert.equal(messages.length, expectedMessages.length);
197+
for (var i = 0; i < expectedMessages.length; i++) {
198+
assert.equal(messages[0].ruleId, expectedMessages[i].ruleId);
199+
if (expectedMessages[i].message !== undefined) {
200+
assert.equal(messages[0].message, expectedMessages[i].message);
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)