Skip to content

Commit b2622b4

Browse files
authored
build: add lint rule to prevent @import usage (#22222)
Adds a custom lint rule that doesn't allow for `@import` to be used.
1 parent 168611d commit b2622b4

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

.stylelintrc.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"./tools/stylelint/no-nested-mixin.ts",
99
"./tools/stylelint/no-concrete-rules.ts",
1010
"./tools/stylelint/no-top-level-ampersand-in-mixin.ts",
11-
"./tools/stylelint/theme-mixin-api.ts"
11+
"./tools/stylelint/theme-mixin-api.ts",
12+
"./tools/stylelint/no-import.ts"
1213
],
1314
"rules": {
1415
"material/no-prefixes": [true, {
@@ -18,6 +19,9 @@
1819
"material/theme-mixin-api": true,
1920
"material/selector-no-deep": true,
2021
"material/no-nested-mixin": true,
22+
"material/no-import": [true, {
23+
"exclude": "\\.import\\.scss$"
24+
}],
2125
"material/no-ampersand-beyond-selector-start": [true, {
2226
"filePattern": "-theme\\.scss$"
2327
}],
@@ -69,8 +73,8 @@
6973
"declaration-block-semicolon-newline-after": "always-multi-line",
7074
"declaration-colon-space-after": "always-single-line",
7175
"declaration-property-value-disallowed-list": [
72-
{"/.*/": ["initial"]},
73-
{"message": "The `initial` value is not supported in IE."}
76+
{"/.*/": ["initial"]},
77+
{"message": "The `initial` value is not supported in IE."}
7478
],
7579

7680
"block-closing-brace-newline-after": "always",

src/material/core/theming/tests/test-theming-bundle.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Imports the theming bundle. Needs to be an absolute bin-dir import path as on Windows,
22
// runfiles are not symlinked, so the Sass compilation happens in the workspace directory
33
// with the include paths being set to the `bazel-bin` directory.
4+
// stylelint-disable-next-line material/no-import
45
@import 'src/material/theming';
56

67
// Disable theme style duplication warnings. This test intentionally generates

tools/stylelint/no-import.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {createPlugin, utils} from 'stylelint';
2+
import {basename} from 'path';
3+
4+
const ruleName = 'material/no-import';
5+
const messages = utils.ruleMessages(ruleName, {
6+
expected: () => '@import is not allowed. Use @use instead.',
7+
});
8+
9+
/** Stylelint plugin that doesn't allow `@import` to be used. */
10+
const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {exclude?: string}) => {
11+
return (root, result) => {
12+
if (!isEnabled) {
13+
return;
14+
}
15+
16+
const excludePattern = options?.exclude ? new RegExp(options.exclude) : null;
17+
18+
if (excludePattern?.test(basename(root.source!.input.file!))) {
19+
return;
20+
}
21+
22+
root.walkAtRules(rule => {
23+
if (rule.name === 'import') {
24+
utils.report({
25+
result,
26+
ruleName,
27+
message: messages.expected(),
28+
node: rule
29+
});
30+
}
31+
});
32+
};
33+
});
34+
35+
plugin.ruleName = ruleName;
36+
plugin.messages = messages;
37+
export default plugin;

0 commit comments

Comments
 (0)