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

build: update to stylelint 14 #23861

Merged
merged 1 commit into from
Nov 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
1 change: 1 addition & 0 deletions .stylelintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"customSyntax": "postcss-scss",
"defaultSeverity": "error",
"reportNeedlessDisables": true,
"reportInvalidScopeDisables": true,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/tsconfig.json tools/cherry-pick-patch/cherry-pick-patch.ts",
"ownerslint": "ts-node --project scripts/tsconfig.json scripts/ownerslint.ts",
"tslint": "tslint -c tslint.json --project ./tsconfig.json",
"stylelint": "stylelint \"src/**/*.+(css|scss)\" --config .stylelintrc.json --syntax scss",
"stylelint": "stylelint \"src/**/*.+(css|scss)\" --config .stylelintrc.json",
"resync-caretaker-app": "ts-node --project scripts/tsconfig.json scripts/caretaking/resync-caretaker-app-prs.ts",
"ts-circular-deps:check": "yarn -s ng-dev ts-circular-deps check --config ./src/circular-deps-test.conf.js",
"ts-circular-deps:approve": "yarn -s ng-dev ts-circular-deps approve --config ./src/circular-deps-test.conf.js",
Expand Down Expand Up @@ -159,7 +159,6 @@
"@types/semver": "^7.3.4",
"@types/send": "^0.14.5",
"@types/shelljs": "^0.8.9",
"@types/stylelint": "^13.13.2",
"@types/yaml": "^1.9.7",
"autoprefixer": "^10.2.5",
"browser-sync": "2.26.13",
Expand Down Expand Up @@ -197,6 +196,7 @@
"node-fetch": "^2.6.0",
"parse5": "^6.0.1",
"postcss": "^8.2.1",
"postcss-scss": "^4.0.2",
"protractor": "^7.0.0",
"reflect-metadata": "^0.1.3",
"requirejs": "^2.3.6",
Expand All @@ -207,7 +207,7 @@
"semver": "^7.3.4",
"send": "^0.17.1",
"shelljs": "^0.8.3",
"stylelint": "^13.13.1",
"stylelint": "^14.0.1",
"terser": "^5.9.0",
"ts-node": "^10.2.1",
"tsickle": "0.39.1",
Expand Down
7 changes: 2 additions & 5 deletions tools/stylelint/no-ampersand-beyond-selector-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ interface RuleOptions {
* Based off the `selector-nested-pattern` Stylelint rule.
* Source: https://github.com/stylelint/stylelint/blob/master/lib/rules/selector-nested-pattern/
*/
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
const plugin = createPlugin(ruleName, (isEnabled: boolean, options: RuleOptions) => {
return (root, result) => {
if (!isEnabled) {
return;
}

const options = _options as RuleOptions;
const filePattern = new RegExp(options.filePattern);
const fileName = basename(root.source!.input.file!);
const fileName = basename(root.source.input.file);

if (!filePattern.test(fileName)) {
return;
Expand Down Expand Up @@ -57,6 +56,4 @@ function hasInvalidAmpersandUsage(selector: string): boolean {
return selector.split(',').some(part => part.trim().indexOf('&', 1) > -1);
}

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
7 changes: 2 additions & 5 deletions tools/stylelint/no-concrete-rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ interface RuleOptions {
* Stylelint plugin that will log a warning for all top-level CSS rules.
* Can be used in theme files to ensure that everything is inside a mixin.
*/
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options) => {
const plugin = createPlugin(ruleName, (isEnabled: boolean, options: RuleOptions) => {
return (root, result) => {
if (!isEnabled) {
return;
}

const options = _options as RuleOptions;
const filePattern = new RegExp(options.filePattern);
const fileName = basename(root.source!.input.file!);
const fileName = basename(root.source.input.file);

if (!filePattern.test(fileName) || !root.nodes) {
return;
Expand All @@ -45,6 +44,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options) => {
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
4 changes: 1 addition & 3 deletions tools/stylelint/no-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {exclude?:

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

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

Expand All @@ -32,6 +32,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {exclude?:
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
2 changes: 0 additions & 2 deletions tools/stylelint/no-nested-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean) => {
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
15 changes: 3 additions & 12 deletions tools/stylelint/no-prefixes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ const messages = utils.ruleMessages(ruleName, {
});

/** Config options for the rule. */
interface RuleOptions {
interface Options {
browsers: string[];
filePattern: string;
}

/**
* Stylelint plugin that warns for unprefixed CSS.
*/
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
const plugin = createPlugin(ruleName, (isEnabled: boolean, {filePattern, browsers}: Options) => {
return (root, result) => {
if (!isEnabled) {
return;
}

const options = _options as RuleOptions;
const {browsers, filePattern} = options;

if (filePattern && !minimatch(root.source!.input.file!, filePattern)) {
if (!isEnabled || (filePattern && !minimatch(root.source.input.file, filePattern))) {
return;
}

Expand Down Expand Up @@ -101,6 +94,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
4 changes: 1 addition & 3 deletions tools/stylelint/no-top-level-ampersand-in-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {

const options = _options as RuleOptions;
const filePattern = new RegExp(options.filePattern);
const fileName = basename(root.source!.input.file!);
const fileName = basename(root.source.input.file);

if (!filePattern.test(fileName)) {
return;
Expand Down Expand Up @@ -55,6 +55,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
26 changes: 9 additions & 17 deletions tools/stylelint/no-unused-import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {createPlugin, Plugin, utils} from 'stylelint';
import {createPlugin, utils} from 'stylelint';
import {basename, join} from 'path';
import {Result, Root} from 'postcss';

const ruleName = 'material/no-unused-import';
const messages = utils.ruleMessages(ruleName, {
Expand All @@ -11,8 +10,8 @@ const messages = utils.ruleMessages(ruleName, {
});

/** Stylelint plugin that flags unused `@use` statements. */
const factory = (isEnabled: boolean, _options: never, context: {fix: boolean}) => {
return (root: Root, result: Result) => {
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options, context) => {
return (root, result) => {
if (!isEnabled) {
return;
}
Expand All @@ -26,29 +25,27 @@ const factory = (isEnabled: boolean, _options: never, context: {fix: boolean}) =
// Flag namespaces we didn't manage to parse so that we can fix the parsing logic.
if (!namespace) {
utils.report({
// We need these `as any` casts, because Stylelint uses an older version
// of the postcss typings that don't match up with our anymore.
result: result as any,
result,
ruleName,
message: messages.invalid(rule.params),
node: rule as any,
node: rule,
});
} else if (!fileContent.includes(namespace + '.')) {
if (context.fix) {
rule.remove();
} else {
utils.report({
result: result as any,
result,
ruleName,
message: messages.expected(namespace),
node: rule as any,
node: rule,
});
}
}
}
});
};
};
});

/** Extracts the namespace of an `@use` rule from its parameters. */
function extractNamespaceFromUseStatement(params: string): string | null {
Expand Down Expand Up @@ -88,9 +85,4 @@ function extractNamespaceFromUseStatement(params: string): string | null {
return null;
}

// Note: We need to cast the value explicitly to `Plugin` because the stylelint types
// do not type the context parameter. https://stylelint.io/developer-guide/rules#add-autofix
const plugin = createPlugin(ruleName, factory as unknown as Plugin);
plugin.ruleName = ruleName;
plugin.messages = messages;
module.exports = plugin;
export default plugin;
2 changes: 0 additions & 2 deletions tools/stylelint/selector-no-deep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean) => {
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
4 changes: 1 addition & 3 deletions tools/stylelint/single-line-comment-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {filePatter

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

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

Expand All @@ -38,6 +38,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {filePatter
};
});

plugin.ruleName = ruleName;
plugin.messages = messages;
export default plugin;
16 changes: 7 additions & 9 deletions tools/stylelint/theme-mixin-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {createPlugin, Plugin, utils} from 'stylelint';
import {createPlugin, utils} from 'stylelint';
import {basename} from 'path';
import {AtRule, atRule, decl, Declaration, Node, Result, Root} from 'postcss';
import {AtRule, atRule, decl, Declaration, Node} from 'postcss';

/** Name of this stylelint rule. */
const ruleName = 'material/theme-mixin-api';
Expand All @@ -21,9 +21,9 @@ const themeMixinRegex = /^(density|color|typography|theme)\((.*)\)$/;
* consistently check for duplicative theme styles so that we can warn consumers. The
* plugin ensures that style-generating statements are nested inside the duplication check.
*/
const plugin = (isEnabled: boolean, _options: never, context: {fix: boolean}) => {
return (root: Root, result: Result) => {
const componentName = getComponentNameFromPath(root.source!.input.file!);
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options, context) => {
return (root, result) => {
const componentName = getComponentNameFromPath(root.source.input.file);

if (!componentName || !isEnabled) {
return;
Expand Down Expand Up @@ -209,7 +209,7 @@ const plugin = (isEnabled: boolean, _options: never, context: {fix: boolean}) =>
utils.report({result: result as any, ruleName, node: node as any, message});
}
};
};
});

/** Figures out the name of the component from a file path. */
function getComponentNameFromPath(filePath: string): string | null {
Expand All @@ -235,6 +235,4 @@ function stripNewlinesAndIndentation(value: string): string {
return value.replace(/(\r|\n)\s+/g, '');
}

// Note: We need to cast the value explicitly to `Plugin` because the stylelint types
// do not type the context parameter. https://stylelint.io/developer-guide/rules#add-autofix
module.exports = createPlugin(ruleName, plugin as unknown as Plugin);
export default plugin;
Loading