stylelint-copyright 0.0.0-4753
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/package.json +20 -0
- package/src/__tests__/index.test.ts +249 -0
- package/src/index.ts +61 -0
- package/tsconfig.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# `stylelint-copyright`
|
|
2
|
+
|
|
3
|
+
Stylelint plugin to check CSS files for a copyright header.
|
|
4
|
+
|
|
5
|
+
```css
|
|
6
|
+
/*
|
|
7
|
+
* Copyright ...
|
|
8
|
+
* ...
|
|
9
|
+
*/
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```json
|
|
15
|
+
// .stylelintrc
|
|
16
|
+
{
|
|
17
|
+
"plugins": ["stylelint-copyright"],
|
|
18
|
+
"rules": {
|
|
19
|
+
"docusaurus/copyright-header": [true, {"header": "\n * Copyright"}]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "stylelint-copyright",
|
|
3
|
+
"version": "0.0.0-4753",
|
|
4
|
+
"description": "Stylelint plugin to check CSS files for a copyright header.",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"watch": "tsc --watch"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/facebook/docusaurus.git",
|
|
14
|
+
"directory": "packages/stylelint-copyright"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"stylelint": "^14.16.1",
|
|
18
|
+
"tslib": "^2.6.0"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
/* eslint-disable jest/no-conditional-expect */
|
|
8
|
+
|
|
9
|
+
import path from 'path';
|
|
10
|
+
import stylelint from 'stylelint';
|
|
11
|
+
import rule from '../index';
|
|
12
|
+
|
|
13
|
+
declare global {
|
|
14
|
+
namespace jest {
|
|
15
|
+
interface Matchers<R> {
|
|
16
|
+
toHaveMessage: () => R;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type TestSuite = {
|
|
22
|
+
ruleName: string;
|
|
23
|
+
fix: boolean;
|
|
24
|
+
accept: TestCase[];
|
|
25
|
+
reject: TestCase[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type TestCase = {
|
|
29
|
+
code: string;
|
|
30
|
+
description?: string;
|
|
31
|
+
fixed?: string;
|
|
32
|
+
message?: string;
|
|
33
|
+
line?: number;
|
|
34
|
+
column?: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
function getOutputCss(output: stylelint.LinterResult) {
|
|
38
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
39
|
+
const result = output.results[0]!._postcssResult!;
|
|
40
|
+
return result.root.toString(result.opts!.syntax);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function testStylelintRule(config: stylelint.Config, tests: TestSuite) {
|
|
44
|
+
describe(`${tests.ruleName}`, () => {
|
|
45
|
+
const checkTestCaseContent = (testCase: TestCase) =>
|
|
46
|
+
testCase.description ?? testCase.code;
|
|
47
|
+
|
|
48
|
+
describe('accept cases', () => {
|
|
49
|
+
tests.accept.forEach((testCase) => {
|
|
50
|
+
it(`${checkTestCaseContent(testCase)}`, async () => {
|
|
51
|
+
const options: stylelint.LinterOptions = {
|
|
52
|
+
code: testCase.code,
|
|
53
|
+
config,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const output = await stylelint.lint(options);
|
|
57
|
+
expect(output.results[0]!.warnings).toEqual([]);
|
|
58
|
+
if (!tests.fix) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const fixedOutput = await stylelint.lint({...options, fix: true});
|
|
62
|
+
const fixedCode = getOutputCss(fixedOutput);
|
|
63
|
+
expect(fixedCode).toBe(testCase.code);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('reject cases', () => {
|
|
69
|
+
tests.reject.forEach((testCase) => {
|
|
70
|
+
it(`${checkTestCaseContent(testCase)}`, async () => {
|
|
71
|
+
const options = {
|
|
72
|
+
code: testCase.code,
|
|
73
|
+
config,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const output = await stylelint.lint(options);
|
|
77
|
+
const {warnings} = output.results[0]!;
|
|
78
|
+
const warning = warnings[0]!;
|
|
79
|
+
expect(warnings.length).toBeGreaterThanOrEqual(1);
|
|
80
|
+
expect(testCase).toHaveMessage();
|
|
81
|
+
if (testCase.message != null) {
|
|
82
|
+
expect(warning.text).toBe(testCase.message);
|
|
83
|
+
}
|
|
84
|
+
if (testCase.line != null) {
|
|
85
|
+
expect(warning.line).toBe(testCase.line);
|
|
86
|
+
}
|
|
87
|
+
if (testCase.column != null) {
|
|
88
|
+
expect(warning.column).toBe(testCase.column);
|
|
89
|
+
}
|
|
90
|
+
if (!tests.fix) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
if (!testCase.fixed) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
'If using { fix: true } in test tests, all reject cases must have { fixed: .. }',
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
const fixedOutput = await stylelint.lint({...options, fix: true});
|
|
99
|
+
const fixedCode = getOutputCss(fixedOutput);
|
|
100
|
+
expect(fixedCode).toBe(testCase.fixed);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
expect.extend({
|
|
106
|
+
toHaveMessage(testCase: TestCase) {
|
|
107
|
+
if (testCase.message == null) {
|
|
108
|
+
return {
|
|
109
|
+
message: () =>
|
|
110
|
+
'Expected "reject" test case to have a "message" property',
|
|
111
|
+
pass: false,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
message: () =>
|
|
117
|
+
'Expected "reject" test case to not have a "message" property',
|
|
118
|
+
pass: true,
|
|
119
|
+
};
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
testStylelintRule(
|
|
126
|
+
{
|
|
127
|
+
plugins: [path.join(__dirname, '../../lib/index.js')],
|
|
128
|
+
rules: {
|
|
129
|
+
[rule.ruleName]: [true, {header: '*\n * Copyright'}],
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
ruleName: rule.ruleName,
|
|
134
|
+
fix: true,
|
|
135
|
+
accept: [
|
|
136
|
+
{
|
|
137
|
+
code: `
|
|
138
|
+
/**
|
|
139
|
+
* Copyright
|
|
140
|
+
*/
|
|
141
|
+
.foo {}`,
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
code: `/**
|
|
145
|
+
* Copyright
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
.foo {}`,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
code: `/**
|
|
152
|
+
* Copyright
|
|
153
|
+
*/
|
|
154
|
+
.foo {}`,
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
reject: [
|
|
158
|
+
{
|
|
159
|
+
code: `.foo {}`,
|
|
160
|
+
fixed: `/**
|
|
161
|
+
* Copyright
|
|
162
|
+
*/
|
|
163
|
+
.foo {}`,
|
|
164
|
+
message:
|
|
165
|
+
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
|
166
|
+
line: 1,
|
|
167
|
+
column: 1,
|
|
168
|
+
},
|
|
169
|
+
{
|
|
170
|
+
code: `
|
|
171
|
+
.foo {}`,
|
|
172
|
+
fixed: `/**
|
|
173
|
+
* Copyright
|
|
174
|
+
*/
|
|
175
|
+
.foo {}`,
|
|
176
|
+
message:
|
|
177
|
+
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
|
178
|
+
line: 1,
|
|
179
|
+
column: 1,
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
code: `/**
|
|
183
|
+
* Copyright
|
|
184
|
+
*/
|
|
185
|
+
|
|
186
|
+
.foo {}`,
|
|
187
|
+
fixed: `/**
|
|
188
|
+
* Copyright
|
|
189
|
+
*/
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Copyright
|
|
193
|
+
*/
|
|
194
|
+
|
|
195
|
+
.foo {}`,
|
|
196
|
+
message:
|
|
197
|
+
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
|
198
|
+
line: 1,
|
|
199
|
+
column: 1,
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
code: `/**
|
|
203
|
+
* Copyleft
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
.foo {}`,
|
|
207
|
+
fixed: `/**
|
|
208
|
+
* Copyright
|
|
209
|
+
*/
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Copyleft
|
|
213
|
+
*/
|
|
214
|
+
|
|
215
|
+
.foo {}`,
|
|
216
|
+
message:
|
|
217
|
+
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
|
218
|
+
line: 1,
|
|
219
|
+
column: 1,
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
code: `/**
|
|
223
|
+
* Copyleft
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Copyright
|
|
228
|
+
*/
|
|
229
|
+
.foo {}`,
|
|
230
|
+
fixed: `/**
|
|
231
|
+
* Copyright
|
|
232
|
+
*/
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Copyleft
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Copyright
|
|
240
|
+
*/
|
|
241
|
+
.foo {}`,
|
|
242
|
+
message:
|
|
243
|
+
'Missing copyright in the header comment (docusaurus/copyright-header)',
|
|
244
|
+
line: 1,
|
|
245
|
+
column: 1,
|
|
246
|
+
},
|
|
247
|
+
],
|
|
248
|
+
},
|
|
249
|
+
);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import stylelint, {type Rule} from 'stylelint';
|
|
9
|
+
|
|
10
|
+
const ruleName = 'docusaurus/copyright-header';
|
|
11
|
+
const messages = stylelint.utils.ruleMessages(ruleName, {
|
|
12
|
+
rejected: 'Missing copyright in the header comment',
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
type SecondaryOption = {header?: string};
|
|
16
|
+
|
|
17
|
+
const rule: Rule<boolean, string> =
|
|
18
|
+
(primaryOption, secondaryOption, context) => (root, result) => {
|
|
19
|
+
stylelint.utils.validateOptions(
|
|
20
|
+
result,
|
|
21
|
+
ruleName,
|
|
22
|
+
{
|
|
23
|
+
actual: primaryOption,
|
|
24
|
+
possible: [true, false],
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
actual: secondaryOption,
|
|
28
|
+
possible: (v) =>
|
|
29
|
+
typeof (v as SecondaryOption | undefined)?.header === 'string',
|
|
30
|
+
},
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
root.first &&
|
|
35
|
+
root.first.type === 'comment' &&
|
|
36
|
+
root.first.source?.start?.column === 1
|
|
37
|
+
) {
|
|
38
|
+
const {text} = root.first;
|
|
39
|
+
if (text === secondaryOption.header) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (context.fix) {
|
|
44
|
+
root.first?.before(`/*${secondaryOption.header!}\n */`);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
stylelint.utils.report({
|
|
49
|
+
message: messages.rejected,
|
|
50
|
+
node: root,
|
|
51
|
+
result,
|
|
52
|
+
ruleName,
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
rule.ruleName = ruleName;
|
|
57
|
+
rule.messages = messages;
|
|
58
|
+
|
|
59
|
+
const plugin = stylelint.createPlugin(ruleName, rule);
|
|
60
|
+
|
|
61
|
+
export default plugin;
|