Skip to content

build: add lint rule to prevent @import usage #22222

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

Merged
merged 1 commit into from
Mar 15, 2021
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
10 changes: 7 additions & 3 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"./tools/stylelint/no-nested-mixin.ts",
"./tools/stylelint/no-concrete-rules.ts",
"./tools/stylelint/no-top-level-ampersand-in-mixin.ts",
"./tools/stylelint/theme-mixin-api.ts"
"./tools/stylelint/theme-mixin-api.ts",
"./tools/stylelint/no-import.ts"
],
"rules": {
"material/no-prefixes": [true, {
Expand All @@ -18,6 +19,9 @@
"material/theme-mixin-api": true,
"material/selector-no-deep": true,
"material/no-nested-mixin": true,
"material/no-import": [true, {
"exclude": "\\.import\\.scss$"
}],
"material/no-ampersand-beyond-selector-start": [true, {
"filePattern": "-theme\\.scss$"
}],
Expand Down Expand Up @@ -69,8 +73,8 @@
"declaration-block-semicolon-newline-after": "always-multi-line",
"declaration-colon-space-after": "always-single-line",
"declaration-property-value-disallowed-list": [
{"/.*/": ["initial"]},
{"message": "The `initial` value is not supported in IE."}
{"/.*/": ["initial"]},
{"message": "The `initial` value is not supported in IE."}
],

"block-closing-brace-newline-after": "always",
Expand Down
1 change: 1 addition & 0 deletions src/material/core/theming/tests/test-theming-bundle.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Imports the theming bundle. Needs to be an absolute bin-dir import path as on Windows,
// runfiles are not symlinked, so the Sass compilation happens in the workspace directory
// with the include paths being set to the `bazel-bin` directory.
// stylelint-disable-next-line material/no-import
@import 'src/material/theming';

// Disable theme style duplication warnings. This test intentionally generates
Expand Down
37 changes: 37 additions & 0 deletions tools/stylelint/no-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {createPlugin, utils} from 'stylelint';
import {basename} from 'path';

const ruleName = 'material/no-import';
const messages = utils.ruleMessages(ruleName, {
expected: () => '@import is not allowed. Use @use instead.',
});

/** Stylelint plugin that doesn't allow `@import` to be used. */
const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {exclude?: string}) => {
return (root, result) => {
if (!isEnabled) {
return;
}

const excludePattern = options?.exclude ? new RegExp(options.exclude) : null;

if (excludePattern?.test(basename(root.source!.input.file!))) {
return;
}

root.walkAtRules(rule => {
if (rule.name === 'import') {
utils.report({
result,
ruleName,
message: messages.expected(),
node: rule
});
}
});
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;