Skip to content

Commit 8a446a6

Browse files
committed
build: update to stylelint 14
Updates the repo to Stylelint version 14 and resolves the breaking changes. Also cleans up some of the custom rule typings now that Stylelint provides its own types.
1 parent f69cf6c commit 8a446a6

13 files changed

+139
-485
lines changed

.stylelintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"customSyntax": "postcss-scss",
23
"defaultSeverity": "error",
34
"reportNeedlessDisables": true,
45
"reportInvalidScopeDisables": true,

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"cherry-pick-patch": "ts-node --project tools/cherry-pick-patch/tsconfig.json tools/cherry-pick-patch/cherry-pick-patch.ts",
3333
"ownerslint": "ts-node --project scripts/tsconfig.json scripts/ownerslint.ts",
3434
"tslint": "tslint -c tslint.json --project ./tsconfig.json",
35-
"stylelint": "stylelint \"src/**/*.+(css|scss)\" --config .stylelintrc.json --syntax scss",
35+
"stylelint": "stylelint \"src/**/*.+(css|scss)\" --config .stylelintrc.json",
3636
"resync-caretaker-app": "ts-node --project scripts/tsconfig.json scripts/caretaking/resync-caretaker-app-prs.ts",
3737
"ts-circular-deps:check": "yarn -s ng-dev ts-circular-deps check --config ./src/circular-deps-test.conf.js",
3838
"ts-circular-deps:approve": "yarn -s ng-dev ts-circular-deps approve --config ./src/circular-deps-test.conf.js",
@@ -159,7 +159,6 @@
159159
"@types/semver": "^7.3.4",
160160
"@types/send": "^0.14.5",
161161
"@types/shelljs": "^0.8.9",
162-
"@types/stylelint": "^13.13.2",
163162
"@types/yaml": "^1.9.7",
164163
"autoprefixer": "^10.2.5",
165164
"browser-sync": "2.26.13",
@@ -197,6 +196,7 @@
197196
"node-fetch": "^2.6.0",
198197
"parse5": "^6.0.1",
199198
"postcss": "^8.2.1",
199+
"postcss-scss": "^4.0.2",
200200
"protractor": "^7.0.0",
201201
"reflect-metadata": "^0.1.3",
202202
"requirejs": "^2.3.6",
@@ -207,7 +207,7 @@
207207
"semver": "^7.3.4",
208208
"send": "^0.17.1",
209209
"shelljs": "^0.8.3",
210-
"stylelint": "^13.13.1",
210+
"stylelint": "^14.0.1",
211211
"terser": "^5.9.0",
212212
"ts-node": "^10.2.1",
213213
"tsickle": "0.39.1",

tools/stylelint/no-ampersand-beyond-selector-start.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,14 @@ interface RuleOptions {
2121
* Based off the `selector-nested-pattern` Stylelint rule.
2222
* Source: https://github.com/stylelint/stylelint/blob/master/lib/rules/selector-nested-pattern/
2323
*/
24-
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
24+
const plugin = createPlugin(ruleName, (isEnabled: boolean, options: RuleOptions) => {
2525
return (root, result) => {
2626
if (!isEnabled) {
2727
return;
2828
}
2929

30-
const options = _options as RuleOptions;
3130
const filePattern = new RegExp(options.filePattern);
32-
const fileName = basename(root.source!.input.file!);
31+
const fileName = basename(root.source.input.file);
3332

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

60-
plugin.ruleName = ruleName;
61-
plugin.messages = messages;
6259
export default plugin;

tools/stylelint/no-concrete-rules.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,14 @@ interface RuleOptions {
1515
* Stylelint plugin that will log a warning for all top-level CSS rules.
1616
* Can be used in theme files to ensure that everything is inside a mixin.
1717
*/
18-
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options) => {
18+
const plugin = createPlugin(ruleName, (isEnabled: boolean, options: RuleOptions) => {
1919
return (root, result) => {
2020
if (!isEnabled) {
2121
return;
2222
}
2323

24-
const options = _options as RuleOptions;
2524
const filePattern = new RegExp(options.filePattern);
26-
const fileName = basename(root.source!.input.file!);
25+
const fileName = basename(root.source.input.file);
2726

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

48-
plugin.ruleName = ruleName;
49-
plugin.messages = messages;
5047
export default plugin;

tools/stylelint/no-import.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {exclude?:
1515

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

18-
if (excludePattern?.test(basename(root.source!.input.file!))) {
18+
if (excludePattern?.test(basename(root.source.input.file))) {
1919
return;
2020
}
2121

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

35-
plugin.ruleName = ruleName;
36-
plugin.messages = messages;
3735
export default plugin;

tools/stylelint/no-nested-mixin.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean) => {
3535
};
3636
});
3737

38-
plugin.ruleName = ruleName;
39-
plugin.messages = messages;
4038
export default plugin;

tools/stylelint/no-prefixes/index.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,17 @@ const messages = utils.ruleMessages(ruleName, {
1515
});
1616

1717
/** Config options for the rule. */
18-
interface RuleOptions {
18+
interface Options {
1919
browsers: string[];
2020
filePattern: string;
2121
}
2222

2323
/**
2424
* Stylelint plugin that warns for unprefixed CSS.
2525
*/
26-
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
26+
const plugin = createPlugin(ruleName, (isEnabled: boolean, {filePattern, browsers}: Options) => {
2727
return (root, result) => {
28-
if (!isEnabled) {
29-
return;
30-
}
31-
32-
const options = _options as RuleOptions;
33-
const {browsers, filePattern} = options;
34-
35-
if (filePattern && !minimatch(root.source!.input.file!, filePattern)) {
28+
if (!isEnabled || (filePattern && !minimatch(root.source.input.file, filePattern))) {
3629
return;
3730
}
3831

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

104-
plugin.ruleName = ruleName;
105-
plugin.messages = messages;
10697
export default plugin;

tools/stylelint/no-top-level-ampersand-in-mixin.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
2424

2525
const options = _options as RuleOptions;
2626
const filePattern = new RegExp(options.filePattern);
27-
const fileName = basename(root.source!.input.file!);
27+
const fileName = basename(root.source.input.file);
2828

2929
if (!filePattern.test(fileName)) {
3030
return;
@@ -55,6 +55,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, _options?) => {
5555
};
5656
});
5757

58-
plugin.ruleName = ruleName;
59-
plugin.messages = messages;
6058
export default plugin;

tools/stylelint/no-unused-import.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import {createPlugin, Plugin, utils} from 'stylelint';
1+
import {createPlugin, utils} from 'stylelint';
22
import {basename, join} from 'path';
3-
import {Result, Root} from 'postcss';
43

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

1312
/** Stylelint plugin that flags unused `@use` statements. */
14-
const factory = (isEnabled: boolean, _options: never, context: {fix: boolean}) => {
15-
return (root: Root, result: Result) => {
13+
const plugin = createPlugin(ruleName, (isEnabled: boolean, _options, context) => {
14+
return (root, result) => {
1615
if (!isEnabled) {
1716
return;
1817
}
@@ -26,29 +25,27 @@ const factory = (isEnabled: boolean, _options: never, context: {fix: boolean}) =
2625
// Flag namespaces we didn't manage to parse so that we can fix the parsing logic.
2726
if (!namespace) {
2827
utils.report({
29-
// We need these `as any` casts, because Stylelint uses an older version
30-
// of the postcss typings that don't match up with our anymore.
31-
result: result as any,
28+
result,
3229
ruleName,
3330
message: messages.invalid(rule.params),
34-
node: rule as any,
31+
node: rule,
3532
});
3633
} else if (!fileContent.includes(namespace + '.')) {
3734
if (context.fix) {
3835
rule.remove();
3936
} else {
4037
utils.report({
41-
result: result as any,
38+
result,
4239
ruleName,
4340
message: messages.expected(namespace),
44-
node: rule as any,
41+
node: rule,
4542
});
4643
}
4744
}
4845
}
4946
});
5047
};
51-
};
48+
});
5249

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

91-
// Note: We need to cast the value explicitly to `Plugin` because the stylelint types
92-
// do not type the context parameter. https://stylelint.io/developer-guide/rules#add-autofix
93-
const plugin = createPlugin(ruleName, factory as unknown as Plugin);
94-
plugin.ruleName = ruleName;
95-
plugin.messages = messages;
96-
module.exports = plugin;
88+
export default plugin;

tools/stylelint/selector-no-deep.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,4 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean) => {
3535
};
3636
});
3737

38-
plugin.ruleName = ruleName;
39-
plugin.messages = messages;
4038
export default plugin;

tools/stylelint/single-line-comment-only.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {filePatter
1919

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

22-
if (filePattern && !filePattern?.test(basename(root.source!.input.file!))) {
22+
if (filePattern && !filePattern?.test(basename(root.source.input.file))) {
2323
return;
2424
}
2525

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

41-
plugin.ruleName = ruleName;
42-
plugin.messages = messages;
4341
export default plugin;

tools/stylelint/theme-mixin-api.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {createPlugin, Plugin, utils} from 'stylelint';
1+
import {createPlugin, utils} from 'stylelint';
22
import {basename} from 'path';
3-
import {AtRule, atRule, decl, Declaration, Node, Result, Root} from 'postcss';
3+
import {AtRule, atRule, decl, Declaration, Node} from 'postcss';
44

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

2828
if (!componentName || !isEnabled) {
2929
return;
@@ -209,7 +209,7 @@ const plugin = (isEnabled: boolean, _options: never, context: {fix: boolean}) =>
209209
utils.report({result: result as any, ruleName, node: node as any, message});
210210
}
211211
};
212-
};
212+
});
213213

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

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

0 commit comments

Comments
 (0)