Skip to content

Commit 91f1937

Browse files
chore: Add cloudscape stylelint (#38)
- Upgrade necessary packages - Add custom cloudscape stylelint - Running linting for project
1 parent 50fc488 commit 91f1937

File tree

19 files changed

+438
-553
lines changed

19 files changed

+438
-553
lines changed

.stylelintrc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
"selector-pseudo-class-no-unknown": [true, { "ignorePseudoClasses": ["global"] }],
77
"selector-max-type": [0, { "ignore": ["compounded"] }],
88
"selector-max-universal": [0],
9-
"order/order": ["rules"]
9+
"order/order": ["rules"],
10+
"@cloudscape-design/license-headers": [
11+
true,
12+
{
13+
"header": "\n Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n SPDX-License-Identifier: Apache-2.0\n"
14+
}
15+
]
1016
},
11-
"plugins": ["stylelint-use-logical", "stylelint-order"]
17+
"plugins": ["stylelint-use-logical", "stylelint-order", "./build-tools/stylelint"]
1218
}

build-tools/stylelint/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import licenseHeaders from "./license-headers.js";
4+
5+
export default [licenseHeaders];
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
import stylelint from "stylelint";
4+
5+
const ruleName = "@cloudscape-design/license-headers";
6+
const messages = stylelint.utils.ruleMessages(ruleName, {
7+
rejected: "Missing license header",
8+
});
9+
10+
function licenseHeadersPlugin(enabled, { header }, context) {
11+
if (!enabled) {
12+
return;
13+
}
14+
15+
if (!header) {
16+
throw new Error(`stylelint ${ruleName} rule requires a header option.`);
17+
}
18+
19+
const trimmedHeader = header.trim();
20+
21+
return (root, result) => {
22+
let foundComment = false;
23+
let firstComment = null;
24+
25+
root.walkComments(function (comment) {
26+
if (comment.parent.type === "root" && comment === comment.parent.first) {
27+
if (!firstComment) {
28+
firstComment = comment;
29+
}
30+
31+
if (comment.text.trim() === trimmedHeader) {
32+
foundComment = true;
33+
}
34+
}
35+
});
36+
37+
if (!foundComment) {
38+
if (context.fix) {
39+
const newComment = `/*${header}*/\n\n`;
40+
root.prepend(newComment);
41+
} else {
42+
stylelint.utils.report({
43+
message: messages.rejected,
44+
node: firstComment || root,
45+
result,
46+
ruleName,
47+
});
48+
}
49+
}
50+
};
51+
}
52+
53+
licenseHeadersPlugin.ruleName = ruleName;
54+
licenseHeadersPlugin.messages = messages;
55+
56+
export default stylelint.createPlugin(ruleName, licenseHeadersPlugin);

build-tools/stylelint/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

0 commit comments

Comments
 (0)