stylelint-copyright 0.0.0-4754
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/LICENSE +21 -0
- package/README.md +22 -0
- package/__tests__/index.test.js +226 -0
- package/index.js +57 -0
- package/package.json +16 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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
|
+
```
|
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
const stylelint = require('stylelint');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const rule = require('..');
|
|
12
|
+
|
|
13
|
+
const {ruleName, messages} = rule;
|
|
14
|
+
|
|
15
|
+
function getOutputCss(output) {
|
|
16
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
17
|
+
const result = output.results[0]._postcssResult;
|
|
18
|
+
return result.root.toString(result.opts.syntax);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function testStylelintRule(config, tests) {
|
|
22
|
+
describe(`${tests.ruleName}`, () => {
|
|
23
|
+
const checkTestCaseContent = (testCase) =>
|
|
24
|
+
testCase.description || testCase.code || 'no description';
|
|
25
|
+
|
|
26
|
+
if (tests.accept?.length) {
|
|
27
|
+
describe('accept cases', () => {
|
|
28
|
+
tests.accept.forEach((testCase) => {
|
|
29
|
+
it(`${checkTestCaseContent(testCase)}`, async () => {
|
|
30
|
+
const options = {
|
|
31
|
+
code: testCase.code,
|
|
32
|
+
config,
|
|
33
|
+
syntax: tests.syntax,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const output = await stylelint.lint(options);
|
|
37
|
+
expect(output.results[0].warnings).toEqual([]);
|
|
38
|
+
if (!tests.fix) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const fixedOutput = await stylelint.lint({...options, fix: true});
|
|
42
|
+
const fixedCode = getOutputCss(fixedOutput);
|
|
43
|
+
expect(fixedCode).toBe(testCase.code);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (tests.reject?.length) {
|
|
50
|
+
describe('reject cases', () => {
|
|
51
|
+
tests.reject.forEach((testCase) => {
|
|
52
|
+
it(`${checkTestCaseContent(testCase)}`, async () => {
|
|
53
|
+
const options = {
|
|
54
|
+
code: testCase.code,
|
|
55
|
+
config,
|
|
56
|
+
syntax: tests.syntax,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const output = await stylelint.lint(options);
|
|
60
|
+
const {warnings} = output.results[0];
|
|
61
|
+
const warning = warnings[0];
|
|
62
|
+
expect(warnings.length).toBeGreaterThanOrEqual(1);
|
|
63
|
+
expect(testCase).toHaveMessage();
|
|
64
|
+
if (testCase.message != null) {
|
|
65
|
+
expect(warning.text).toBe(testCase.message);
|
|
66
|
+
}
|
|
67
|
+
if (testCase.line != null) {
|
|
68
|
+
expect(warning.line).toBe(testCase.line);
|
|
69
|
+
}
|
|
70
|
+
if (testCase.column != null) {
|
|
71
|
+
expect(warning.column).toBe(testCase.column);
|
|
72
|
+
}
|
|
73
|
+
if (!tests.fix) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (!testCase.fixed) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
'If using { fix: true } in test tests, all reject cases must have { fixed: .. }',
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const fixedOutput = await stylelint.lint({...options, fix: true});
|
|
82
|
+
const fixedCode = getOutputCss(fixedOutput);
|
|
83
|
+
expect(fixedCode).toBe(testCase.fixed);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
expect.extend({
|
|
90
|
+
toHaveMessage(testCase) {
|
|
91
|
+
if (testCase.message == null) {
|
|
92
|
+
return {
|
|
93
|
+
message: () =>
|
|
94
|
+
'Expected "reject" test case to have a "message" property',
|
|
95
|
+
pass: false,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
pass: true,
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
testStylelintRule(
|
|
108
|
+
{
|
|
109
|
+
plugins: [path.join(__dirname, '..')],
|
|
110
|
+
rules: {
|
|
111
|
+
[ruleName]: [true, {header: '*\n * Copyright'}],
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
ruleName,
|
|
116
|
+
fix: true,
|
|
117
|
+
accept: [
|
|
118
|
+
{
|
|
119
|
+
code: `
|
|
120
|
+
/**
|
|
121
|
+
* Copyright
|
|
122
|
+
*/
|
|
123
|
+
.foo {}`,
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
code: `/**
|
|
127
|
+
* Copyright
|
|
128
|
+
*/
|
|
129
|
+
|
|
130
|
+
.foo {}`,
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
code: `/**
|
|
134
|
+
* Copyright
|
|
135
|
+
*/
|
|
136
|
+
.foo {}`,
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
reject: [
|
|
140
|
+
{
|
|
141
|
+
code: `.foo {}`,
|
|
142
|
+
fixed: `/**
|
|
143
|
+
* Copyright
|
|
144
|
+
*/
|
|
145
|
+
.foo {}`,
|
|
146
|
+
message: messages.rejected,
|
|
147
|
+
line: 1,
|
|
148
|
+
column: 1,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
code: `
|
|
152
|
+
.foo {}`,
|
|
153
|
+
fixed: `/**
|
|
154
|
+
* Copyright
|
|
155
|
+
*/
|
|
156
|
+
.foo {}`,
|
|
157
|
+
message: messages.rejected,
|
|
158
|
+
line: 1,
|
|
159
|
+
column: 1,
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
code: `/**
|
|
163
|
+
* Copyright
|
|
164
|
+
*/
|
|
165
|
+
|
|
166
|
+
.foo {}`,
|
|
167
|
+
fixed: `/**
|
|
168
|
+
* Copyright
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Copyright
|
|
173
|
+
*/
|
|
174
|
+
|
|
175
|
+
.foo {}`,
|
|
176
|
+
message: messages.rejected,
|
|
177
|
+
line: 1,
|
|
178
|
+
column: 1,
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
code: `/**
|
|
182
|
+
* Copyleft
|
|
183
|
+
*/
|
|
184
|
+
|
|
185
|
+
.foo {}`,
|
|
186
|
+
fixed: `/**
|
|
187
|
+
* Copyright
|
|
188
|
+
*/
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Copyleft
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
.foo {}`,
|
|
195
|
+
message: messages.rejected,
|
|
196
|
+
line: 1,
|
|
197
|
+
column: 1,
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
code: `/**
|
|
201
|
+
* Copyleft
|
|
202
|
+
*/
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Copyright
|
|
206
|
+
*/
|
|
207
|
+
.foo {}`,
|
|
208
|
+
fixed: `/**
|
|
209
|
+
* Copyright
|
|
210
|
+
*/
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Copyleft
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Copyright
|
|
218
|
+
*/
|
|
219
|
+
.foo {}`,
|
|
220
|
+
message: messages.rejected,
|
|
221
|
+
line: 1,
|
|
222
|
+
column: 1,
|
|
223
|
+
},
|
|
224
|
+
],
|
|
225
|
+
},
|
|
226
|
+
);
|
package/index.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
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
|
+
const stylelint = require('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
|
+
const plugin = stylelint.createPlugin(
|
|
16
|
+
ruleName,
|
|
17
|
+
(primaryOption, secondaryOption, context) => (root, result) => {
|
|
18
|
+
stylelint.utils.validateOptions(
|
|
19
|
+
result,
|
|
20
|
+
ruleName,
|
|
21
|
+
{
|
|
22
|
+
actual: primaryOption,
|
|
23
|
+
possible: [true, false],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
actual: secondaryOption,
|
|
27
|
+
possible: (v) => typeof v.header === 'string',
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
if (
|
|
32
|
+
root.first &&
|
|
33
|
+
root.first.type === 'comment' &&
|
|
34
|
+
root.first.source.start.column === 1
|
|
35
|
+
) {
|
|
36
|
+
const {text} = root.first;
|
|
37
|
+
if (text === secondaryOption.header) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (context.fix) {
|
|
42
|
+
root.first?.before(`/*${secondaryOption.header}\n */`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
stylelint.utils.report({
|
|
47
|
+
message: messages.rejected,
|
|
48
|
+
node: root,
|
|
49
|
+
result,
|
|
50
|
+
ruleName,
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
module.exports = plugin;
|
|
56
|
+
module.exports.ruleName = ruleName;
|
|
57
|
+
module.exports.messages = messages;
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "stylelint-copyright",
|
|
3
|
+
"version": "0.0.0-4754",
|
|
4
|
+
"description": "Stylelint plugin to check CSS files for a copyright header.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/facebook/docusaurus.git",
|
|
10
|
+
"directory": "packages/stylelint-copyright"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"stylelint": "^14.6.0"
|
|
14
|
+
},
|
|
15
|
+
"gitHead": "6c1c0d9bb0878c738732b8678af70a8dc04ee0a4"
|
|
16
|
+
}
|