Skip to content

Commit

Permalink
chore: Add stylelint
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldowseza committed Aug 1, 2024
1 parent 1b150d9 commit 45f42bc
Show file tree
Hide file tree
Showing 21 changed files with 1,992 additions and 158 deletions.
20 changes: 20 additions & 0 deletions .stylelintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"extends": ["stylelint-config-recommended-scss", "stylelint-prettier/recommended"],
"rules": {
"selector-pseudo-class-no-unknown": [
true,
{
"ignorePseudoClasses": [
"global"
]
}
],
"@cloudscape-design/license-headers": [
true,
{
"header": "\n Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n SPDX-License-Identifier: Apache-2.0\n"
}
]
},
"plugins": ["./build-tools/stylelint"]
}
5 changes: 5 additions & 0 deletions build-tools/stylelint/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import licenseHeaders from "./license-headers.js";

export default [licenseHeaders];
56 changes: 56 additions & 0 deletions build-tools/stylelint/license-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import stylelint from "stylelint";

const ruleName = "@cloudscape-design/license-headers";
const messages = stylelint.utils.ruleMessages(ruleName, {
rejected: "Missing license header",
});

function licenseHeadersPlugin(enabled, { header }, context) {
if (!enabled) {
return;
}

if (!header) {
throw new Error(`stylelint ${ruleName} rule requires a header option.`);
}

const trimmedHeader = header.trim();

return (root, result) => {
let foundComment = false;
let firstComment = null;

root.walkComments(function (comment) {
if (comment.parent.type === "root" && comment === comment.parent.first) {
if (!firstComment) {
firstComment = comment;
}

if (comment.text.trim() === trimmedHeader) {
foundComment = true;
}
}
});

if (!foundComment) {
if (context.fix) {
const newComment = `/*${header}*/\n\n`;
root.prepend(newComment);
} else {
stylelint.utils.report({
message: messages.rejected,
node: firstComment || root,
result,
ruleName,
});
}
}
};
}

licenseHeadersPlugin.ruleName = ruleName;
licenseHeadersPlugin.messages = messages;

export default stylelint.createPlugin(ruleName, licenseHeadersPlugin);
3 changes: 3 additions & 0 deletions build-tools/stylelint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Loading

0 comments on commit 45f42bc

Please sign in to comment.