stylelint-copyright 2.0.0-alpha.69 → 2.0.0-beta.18

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 CHANGED
@@ -1,6 +1,6 @@
1
- # stylelint-copyright
1
+ # `stylelint-copyright`
2
2
 
3
- stylelint plugin to check CSS files for a copyright header.
3
+ Stylelint plugin to check CSS files for a copyright header.
4
4
 
5
5
  ```css
6
6
  /*
@@ -16,7 +16,7 @@ stylelint plugin to check CSS files for a copyright header.
16
16
  {
17
17
  "plugins": ["stylelint-copyright"],
18
18
  "rules": {
19
- "docusaurus/copyright-header": true
19
+ "docusaurus/copyright-header": [true, {"header": "\n * Copyright"}]
20
20
  }
21
21
  }
22
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 CHANGED
@@ -12,44 +12,46 @@ const messages = stylelint.utils.ruleMessages(ruleName, {
12
12
  rejected: 'Missing copyright in the header comment',
13
13
  });
14
14
 
15
- module.exports = stylelint.createPlugin(ruleName, (actual) => {
16
- return (root, result) => {
17
- const validOptions = stylelint.utils.validateOptions(result, ruleName, {
18
- actual,
19
- });
20
-
21
- if (!validOptions) {
22
- return;
23
- }
24
-
25
- root.walkComments((comment) => {
26
- // Ignore root comments with copyright text.
27
- if (
28
- comment === comment.parent.first &&
29
- /[Cc]opyright/.test(comment.text)
30
- ) {
31
- return;
32
- }
33
-
34
- // Ignore non-root comments.
35
- if (comment.type !== 'root' && comment !== comment.parent.first) {
36
- return;
37
- }
38
-
39
- // Ignore indented comments.
40
- if (comment.source.start.column > 1) {
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) {
41
38
  return;
42
39
  }
40
+ }
41
+ if (context.fix) {
42
+ root.first?.before(`/*${secondaryOption.header}\n */`);
43
+ return;
44
+ }
43
45
 
44
- stylelint.utils.report({
45
- message: messages.rejected,
46
- node: comment,
47
- result,
48
- ruleName,
49
- });
46
+ stylelint.utils.report({
47
+ message: messages.rejected,
48
+ node: root,
49
+ result,
50
+ ruleName,
50
51
  });
51
- };
52
- });
52
+ },
53
+ );
53
54
 
55
+ module.exports = plugin;
54
56
  module.exports.ruleName = ruleName;
55
57
  module.exports.messages = messages;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "stylelint-copyright",
3
- "version": "2.0.0-alpha.69",
4
- "description": "stylelint plugin to check css files for a copyright header",
3
+ "version": "2.0.0-beta.18",
4
+ "description": "Stylelint plugin to check CSS files for a copyright header.",
5
5
  "main": "index.js",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -10,7 +10,7 @@
10
10
  "directory": "packages/stylelint-copyright"
11
11
  },
12
12
  "dependencies": {
13
- "stylelint": "^13.2.1"
13
+ "stylelint": "^14.6.0"
14
14
  },
15
- "gitHead": "4410a9eb01b87c2df664986ba37e748ed34a3c78"
15
+ "gitHead": "1a945d06993d53376e61bed2c942799fe07dc336"
16
16
  }
@@ -1,69 +0,0 @@
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 rule = require('..');
9
-
10
- const {ruleName, messages} = rule;
11
-
12
- testStylelintRule(
13
- {
14
- // Relative to repo root.
15
- plugins: ['./packages/stylelint-copyright'],
16
- rules: {
17
- [ruleName]: true,
18
- },
19
- },
20
- {
21
- ruleName,
22
- fix: false,
23
- accept: [
24
- {
25
- code: `
26
- /**
27
- * Copyright
28
- */
29
-
30
- .foo {}`,
31
- },
32
- {
33
- code: `
34
- /**
35
- * copyright
36
- */
37
-
38
- .foo {}`,
39
- },
40
- ],
41
- reject: [
42
- {
43
- code: `
44
- /**
45
- * Copyleft
46
- */
47
-
48
- .foo {}`,
49
- message: messages.rejected,
50
- line: 2,
51
- column: 1,
52
- },
53
- {
54
- code: `
55
- /**
56
- * Copyleft
57
- */
58
-
59
- /**
60
- * Copyright
61
- */
62
- .foo {}`,
63
- message: messages.rejected,
64
- line: 2,
65
- column: 1,
66
- },
67
- ],
68
- },
69
- );