-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·138 lines (117 loc) · 3.75 KB
/
Copy pathindex.js
File metadata and controls
executable file
·138 lines (117 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { existsSync, readFileSync } from "fs";
import { join } from "path";
import stylelint from "stylelint";
import { compareTwoStrings } from "string-similarity";
import { template } from "lodash-es";
const {
createPlugin,
utils: { report, ruleMessages, validateOptions },
} = stylelint;
const ruleName = "header/header";
const messages = ruleMessages(ruleName, {
rejected: `Header not found.`,
});
const meta = {
url: "https://github.com/castastrophe/stylelint-header/blob/main/README.md",
};
/**
* @typedef {object} Options
* @property {number} [nonMatchingTolerance=0.98] -- percentage of allowed difference between a found comment in the file and the provided header
* @property {{ [string]: any }} [templateVariables={}] -- used to replace variables in the header template
* @property {boolean} [isRemovable=false] -- whether the comment starts with `/*!`, a special syntax that is often retained even when other comments are stripped by minifiers such as cssnano
*/
/** @type {import('stylelint').Rule<string, Options>} */
const ruleFunction =
(pathOrString, options = {}, context = {}) =>
(root, result) => {
const validOptions = validateOptions(
result,
ruleName,
{
actual: pathOrString,
possible: [
null,
(x) => typeof x === "string",
(x) => typeof x === "string" && existsSync(x),
(x) => typeof x === "string" && existsSync(join(process.cwd(), x)),
],
},
{
optional: true,
actual: options,
possible: {
nonMatchingTolerance: [
(val) => typeof val === "number" && val >= 0 && val <= 1,
],
templateVariables: [Object, null],
isRemovable: [Boolean, null],
},
},
);
if (!validOptions) return;
let headerTemplate = pathOrString;
if (existsSync(pathOrString)) {
headerTemplate = readFileSync(pathOrString, "utf8");
} else if (existsSync(join(process.cwd(), pathOrString))) {
headerTemplate = readFileSync(join(process.cwd(), pathOrString), "utf8");
}
if (!headerTemplate || headerTemplate === "") return;
// Trim any comment tags from the string and remove whitespace
headerTemplate = headerTemplate.replace(/(\/\*|\*\/|(\s*\*))/g, "").trim();
const getHeader = template(headerTemplate);
const header = getHeader({
YEAR: new Date().getFullYear(),
FILE_NAME: context.file?.basename,
FILE_PATH: context.file?.dirname,
...(options.templateVariables ?? {}),
});
const nonMatchingTolerance = options?.nonMatchingTolerance || 0.98;
const isRemovable = options?.isRemovable || false;
// Walk comments on root to find if header exists
let found = false;
root.walkComments((comment, _idx) => {
// Remove any asterisks and whitespace from the texts before comparing
const clean = (text) =>
text
.replace(/(\*|\n|\s)/g, "")
.replace(/^!/g, "")
.trim();
// If the two strings are at least 98% alike, it's a match
if (
compareTwoStrings(clean(comment.text), clean(header)) >=
nonMatchingTolerance
) {
found = true;
}
// This escapes the loop if found, continues if not found
return !found;
});
if (found) return;
if (context.fix) {
// Add the provided header to the top of the file
root.prepend({
text: header
.split("\n")
.map((line) => ` * ${line}`)
.join("\n"),
raws: {
left: isRemovable ? "\n" : "!\n",
right: "\n ",
},
});
// Put a few newlines between the comment and the first property
root.nodes[1].raws.before = context.newline + context.newline;
} else {
// Just report the issue
report({
ruleName: ruleName,
result: result,
message: messages.rejected,
node: root,
});
}
};
ruleFunction.ruleName = ruleName;
ruleFunction.messages = messages;
ruleFunction.meta = meta;
export default createPlugin(ruleName, ruleFunction);