Skip to content

Commit 3c975e9

Browse files
authored
build: enforce that we use comments that won't show up in the generated output (#22309)
Adds a custom stylelint rule that will prevent multi-line comments (`/* */`) from being used in .scss files. The problem with such comments are preserved by Sass and will show up in the user's output, whereas single-line comments (`//`) will not. **Note:** while Stylelint has a `comment-pattern` rule already, we can't use it because the rule works on the comment text which doesn't include the syntax.
1 parent af3e74c commit 3c975e9

File tree

20 files changed

+84
-50
lines changed

20 files changed

+84
-50
lines changed

.stylelintrc.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"./tools/stylelint/no-concrete-rules.ts",
1010
"./tools/stylelint/no-top-level-ampersand-in-mixin.ts",
1111
"./tools/stylelint/theme-mixin-api.ts",
12-
"./tools/stylelint/no-import.ts"
12+
"./tools/stylelint/no-import.ts",
13+
"./tools/stylelint/single-line-comment-only.ts"
1314
],
1415
"rules": {
1516
"material/no-prefixes": [true, {
@@ -19,6 +20,9 @@
1920
"material/theme-mixin-api": true,
2021
"material/selector-no-deep": true,
2122
"material/no-nested-mixin": true,
23+
"material/single-line-comment-only": [true, {
24+
"filePattern": "\\.scss$"
25+
}],
2226
"material/no-import": [true, {
2327
"exclude": "\\.import\\.scss$"
2428
}],

src/dev-app/virtual-scroll/virtual-scroll-demo.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
.demo-item {
1818
-ms-writing-mode: tb-lr;
1919
-webkit-writing-mode: vertical-lr;
20-
/* stylelint-disable-next-line material/no-prefixes */
20+
// stylelint-disable-next-line material/no-prefixes
2121
writing-mode: vertical-lr;
2222
}
2323
}

src/material-experimental/mdc-button/fab.scss

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// ```
2222
// However, Angular Material expects a `mat-icon` instead. The following
2323
// mixin will style the icons appropriately.
24-
/* stylelint-disable-next-line selector-class-pattern */
24+
// stylelint-disable-next-line selector-class-pattern
2525
.mat-icon, .material-icons {
2626
@include mdc-fab.icon_();
2727
}
@@ -30,7 +30,7 @@
3030
.mat-mdc-extended-fab {
3131
@include mdc-fab.extended_();
3232

33-
/* stylelint-disable-next-line selector-class-pattern */
33+
// stylelint-disable-next-line selector-class-pattern
3434
.mat-icon, .material-icons {
3535
@include mdc-fab.extended-icon-padding(
3636
mdc-fab.$extended-icon-padding,
@@ -41,7 +41,7 @@
4141
// For Extended FAB with text label followed by icon.
4242
// We are checking for the a button class because white this is a FAB it
4343
// uses the same template as button.
44-
/* stylelint-disable-next-line selector-class-pattern */
44+
// stylelint-disable-next-line selector-class-pattern
4545
.mdc-button__label + .mat-icon, .mdc-button__label + .material-icons {
4646
@include mdc-fab.extended-icon-padding(
4747
mdc-fab.$extended-icon-padding,
@@ -50,4 +50,3 @@
5050
);
5151
}
5252
}
53-

src/material-experimental/mdc-form-field/_mdc-text-field-structure-overrides.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
// placeholder if the form-field label is set to always float.
6969
// TODO(devversion): consider getting a mixin or variables for this (currently not available).
7070
// Stylelint no-prefixes rule disabled because MDC text-field only uses "::placeholder" too.
71-
/* stylelint-disable-next-line material/no-prefixes */
71+
// stylelint-disable-next-line material/no-prefixes
7272
.mat-mdc-form-field-label-always-float .mdc-text-field__input::placeholder {
7373
transition-delay: 40ms;
7474
transition-duration: 110ms;

src/material-experimental/mdc-helpers/_focus-indicators-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
/// .demo-red-theme {
3131
/// @include mat-mdc-strong-focus-indicators-theme(#f00);
3232
/// }
33-
/* stylelint-disable-next-line material/theme-mixin-api */
33+
// stylelint-disable-next-line material/theme-mixin-api
3434
@mixin theme($theme-or-color) {
3535
@if meta.type-of($theme-or-color) != 'map' {
3636
@include _mat-mdc-strong-focus-indicators-border-color($theme-or-color);

src/material-experimental/mdc-helpers/_mdc-helpers.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ $mat-typography-2018-level-mappings: (
159159

160160
// Configures MDC's global variables to reflect the given theme, applies the given styles,
161161
// then resets the global variables to prevent unintended side effects.
162-
/* stylelint-disable-next-line material/theme-mixin-api */
162+
// stylelint-disable-next-line material/theme-mixin-api
163163
@mixin mat-using-mdc-theme($config) {
164164
$primary: theming.get-color-from-palette(map.get($config, primary));
165165
$accent: theming.get-color-from-palette(map.get($config, accent));
@@ -250,7 +250,7 @@ $mat-typography-2018-level-mappings: (
250250

251251
// Configures MDC's global variables to reflect the given typography config,
252252
// applies the given styles, then resets the global variables to prevent unintended side effects.
253-
/* stylelint-disable-next-line material/theme-mixin-api */
253+
// stylelint-disable-next-line material/theme-mixin-api
254254
@mixin mat-using-mdc-typography($config) {
255255
// Save the original values.
256256
$orig-mdc-typography-styles: mdc-typography.$styles;

src/material/autocomplete/autocomplete.scss

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
@use '../core/style/menu-common';
22
@use '../../cdk/a11y/a11y';
33

4-
/**
5-
* The max-height of the panel, currently matching mat-select value.
6-
*/
4+
// The max-height of the panel, currently matching mat-select value.
75
$panel-max-height: 256px !default;
86
$panel-border-radius: 4px !default;
97

src/material/core/focus-indicators/_focus-indicators-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/// .demo-red-theme {
2424
/// @include mat-strong-focus-indicators-theme(#f00);
2525
/// }
26-
/* stylelint-disable-next-line material/theme-mixin-api */
26+
// stylelint-disable-next-line material/theme-mixin-api
2727
@mixin theme($theme-or-color) {
2828
@if meta.type-of($theme-or-color) != 'map' {
2929
@include _mat-strong-focus-indicators-border-color($theme-or-color);

src/material/core/ripple/_ripple-theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@use 'sass:meta';
33
@use '../theming/theming';
44

5-
/* Colors for the ripple elements.*/
5+
// Colors for the ripple elements.
66
@mixin color($config-or-theme) {
77
$config: theming.get-color-config($config-or-theme);
88
$foreground: map.get($config, foreground);

src/material/core/style/_menu-common.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@use './layout-common';
44
@use './vendor-prefixes';
55

6-
/** The mixins below are shared between mat-menu and mat-select */
6+
// The mixins below are shared between mat-menu and mat-select
77

88
// menu width must be a multiple of 56px
99
$overlay-min-width: 112px !default; // 56 * 2

src/material/core/style/_vendor-prefixes.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* stylelint-disable material/no-prefixes */
1+
// stylelint-disable material/no-prefixes
22
@mixin user-select($value) {
33
-webkit-user-select: $value;
44
-moz-user-select: $value;
@@ -43,4 +43,4 @@
4343
position: -webkit-sticky #{if($important, '!important', '')};
4444
position: sticky #{if($important, '!important', '')};
4545
}
46-
/* stylelint-enable */
46+
// stylelint-enable

src/material/core/typography/_typography.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
}
150150

151151
// Adds the base typography styles, based on a config.
152-
/* stylelint-disable-next-line material/theme-mixin-api */
152+
// stylelint-disable-next-line material/theme-mixin-api
153153
@mixin typography-hierarchy($config-or-theme, $selector: '.mat-typography') {
154154
$config: private-typography-to-2014-config(theming.get-typography-config($config-or-theme));
155155

src/material/expansion/expansion-panel-header.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@
6666
flex-grow: 2;
6767
}
6868

69-
/**
70-
* Creates the expansion indicator arrow. Done using ::after rather than having
71-
* additional nodes in the template.
72-
*/
69+
// Creates the expansion indicator arrow. Done using ::after
70+
// rather than havingadditional nodes in the template.
7371
.mat-expansion-indicator::after {
7472
border-style: solid;
7573
border-width: 0 2px 2px 0;

src/material/progress-spinner/progress-spinner.scss

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,36 +87,34 @@ $_mat-progress-spinner-default-circumference:
8787
$fallback-iterations: 4;
8888

8989
@keyframes mat-progress-spinner-stroke-rotate-100 {
90-
/*
91-
stylelint-disable declaration-block-single-line-max-declarations,
92-
declaration-block-semicolon-space-after
93-
*/
90+
// stylelint-disable declaration-block-single-line-max-declarations
91+
// stylelint-disable declaration-block-semicolon-space-after
9492

9593
// .0001 percentage difference is necessary in order to avoid unwanted animation frames
9694
// for example because the animation duration is 4 seconds, .1% accounts to 4ms
9795
// which are enough to see the flicker described in
9896
// https://github.com/angular/components/issues/8984
9997
//
10098
// NOTE: this is replaced by js for any diameter other that 100
101-
0% { stroke-dashoffset: $start; transform: rotate(0); }
102-
12.5% { stroke-dashoffset: $end; transform: rotate(0); }
99+
0% { stroke-dashoffset: $start; transform: rotate(0); }
100+
12.5% { stroke-dashoffset: $end; transform: rotate(0); }
103101
12.5001% { stroke-dashoffset: $end; transform: rotateX(180deg) rotate(72.5deg); }
104-
25% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(72.5deg); }
102+
25% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(72.5deg); }
105103

106104
25.0001% { stroke-dashoffset: $start; transform: rotate(270deg); }
107-
37.5% { stroke-dashoffset: $end; transform: rotate(270deg); }
108-
37.5001% { stroke-dashoffset: $end; transform: rotateX(180deg) rotate(161.5deg); }
109-
50% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(161.5deg); }
105+
37.5% { stroke-dashoffset: $end; transform: rotate(270deg); }
106+
37.5001% { stroke-dashoffset: $end; transform: rotateX(180deg) rotate(161.5deg); }
107+
50% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(161.5deg); }
110108

111109
50.0001% { stroke-dashoffset: $start; transform: rotate(180deg); }
112-
62.5% { stroke-dashoffset: $end; transform: rotate(180deg); }
110+
62.5% { stroke-dashoffset: $end; transform: rotate(180deg); }
113111
62.5001% { stroke-dashoffset: $end; transform: rotateX(180deg) rotate(251.5deg); }
114-
75% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(251.5deg); }
112+
75% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(251.5deg); }
115113

116114
75.0001% { stroke-dashoffset: $start; transform: rotate(90deg); }
117-
87.5% { stroke-dashoffset: $end; transform: rotate(90deg); }
115+
87.5% { stroke-dashoffset: $end; transform: rotate(90deg); }
118116
87.5001% { stroke-dashoffset: $end; transform: rotateX(180deg) rotate(341.5deg); }
119-
100% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(341.5deg); }
117+
100% { stroke-dashoffset: $start; transform: rotateX(180deg) rotate(341.5deg); }
120118
// stylelint-enable
121119
}
122120

src/material/slide-toggle/slide-toggle.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ $bar-track-width: $bar-width - $thumb-size;
5959
@include list-common.truncate-line();
6060
}
6161

62-
/* If the label should be placed before the thumb then we just change the orders. */
62+
// If the label should be placed before the thumb then we just change the orders.
6363
.mat-slide-toggle-label-before {
6464
.mat-slide-toggle-label { order: 1; }
6565
.mat-slide-toggle-bar { order: 2; }
@@ -216,7 +216,7 @@ $bar-track-width: $bar-width - $thumb-size;
216216
}
217217
}
218218

219-
/** Custom styling to make the slide-toggle usable in high contrast mode. */
219+
// Custom styling to make the slide-toggle usable in high contrast mode.
220220
@include a11y.high-contrast(active, off) {
221221
.mat-slide-toggle-thumb,
222222
.mat-slide-toggle-bar {

src/material/snack-bar/snack-bar-container.scss

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ $spacing-margin-handset: 8px !default;
2424
}
2525
}
2626

27-
/**
28-
* The mat-snack-bar-handset class will be applied to the overlay
29-
* element causing styling to both it and the snack bar container.
30-
*/
27+
// The mat-snack-bar-handset class will be applied to the overlay
28+
// element causing styling to both it and the snack bar container.
3129
.mat-snack-bar-handset {
3230
width: 100%;
3331

src/material/table/_table-flex-styles.scss

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/**
2-
* Flex-based table structure
3-
*/
1+
// Flex-based table structure
42
$header-row-height: 56px;
53
$row-height: 48px;
64
$row-horizontal-padding: 24px;

src/material/table/table.scss

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
@include table-flex-styles.private-table-flex-styles();
55

6-
/**
7-
* Native HTML table structure
8-
*/
6+
// Native HTML table structure
97
table.mat-table {
108
border-spacing: 0;
119
}

src/material/toolbar/toolbar.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$row-padding: 16px !default;
44

5-
/** @deprecated @breaking-change 8.0.0 */
5+
// @deprecated @breaking-change 8.0.0
66
// TODO: Remove once internal g3 apps no longer depend on this variable. Tracked with: COMP-303.
77
$height-mobile-portrait: 56px !default;
88

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {createPlugin, utils} from 'stylelint';
2+
import {basename} from 'path';
3+
4+
const ruleName = 'material/single-line-comment-only';
5+
const messages = utils.ruleMessages(ruleName, {
6+
expected: () => 'Multi-line comments are not allowed (e.g. /* */). ' +
7+
'Use single-line comments instead (//).',
8+
});
9+
10+
/**
11+
* Stylelint plugin that doesn't allow multi-line comments to
12+
* be used, because they'll show up in the user's output.
13+
*/
14+
const plugin = createPlugin(ruleName, (isEnabled: boolean, options?: {filePattern?: string}) => {
15+
return (root, result) => {
16+
if (!isEnabled) {
17+
return;
18+
}
19+
20+
const filePattern = options?.filePattern ? new RegExp(options.filePattern) : null;
21+
22+
if (filePattern && !filePattern?.test(basename(root.source!.input.file!))) {
23+
return;
24+
}
25+
26+
root.walkComments(comment => {
27+
// The `raws.inline` property isn't in the typing so we need to cast to any. Also allow
28+
// comments starting with `!` since they're used to tell minifiers to preserve the comment.
29+
if (!(comment.raws as any).inline && !comment.text.startsWith('!')) {
30+
utils.report({
31+
result,
32+
ruleName,
33+
message: messages.expected(),
34+
node: comment
35+
});
36+
}
37+
});
38+
};
39+
});
40+
41+
plugin.ruleName = ruleName;
42+
plugin.messages = messages;
43+
export default plugin;

0 commit comments

Comments
 (0)