@csstools/stylelint-formatter-github 1.0.0

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.md ADDED
@@ -0,0 +1,18 @@
1
+ MIT No Attribution (MIT-0)
2
+
3
+ Copyright 2023 Romain Menke, Antonio Laguna <antonio@laguna.es>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the “Software”), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so.
11
+
12
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # [@csstools/stylelint-formatter-github](https://www.npmjs.com/package/@csstools/stylelint-formatter-github)
2
+
3
+ [![version](https://img.shields.io/npm/v/@csstools/stylelint-formatter-github.svg)](https://www.npmjs.com/package/@csstools/stylelint-formatter-github)
4
+
5
+ Format Stylelint output as GitHub Actions annotations.
6
+
7
+ ## Usage
8
+
9
+ ```sh
10
+ npm install --save-dev @csstools/stylelint-formatter-github
11
+ ```
12
+
13
+ ```sh
14
+ stylelint --custom-formatter @csstools/stylelint-formatter-github
15
+ ```
16
+
17
+ ## `--formatter github`
18
+
19
+ The build-in `github` formatter was deprecated.
20
+ This package provides a drop-in replacement.
21
+
package/index.mjs ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @see https://github.com/stylelint/stylelint/blob/8fefd145f86b74f182286550675881c47b164fba/lib/formatters/githubFormatter.mjs
3
+ *
4
+ * @license MIT https://github.com/stylelint/stylelint/blob/main/LICENSE
5
+ */
6
+
7
+ import preprocessWarnings from './preprocess-warnings.mjs';
8
+
9
+ /**
10
+ * @see https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
11
+ *
12
+ * @type {import('stylelint').Formatter}
13
+ */
14
+ export default function githubFormatter(results, returnValue) {
15
+ const title = 'Stylelint problem';
16
+ const metadata = returnValue.ruleMetadata;
17
+
18
+ const lines = results.flatMap((result) => {
19
+ const { source, warnings } = preprocessWarnings(result);
20
+
21
+ return warnings.map(({ line, column, endLine, endColumn, text, severity, rule }) => {
22
+ const msg = buildMessage(text, metadata[rule]);
23
+
24
+ return endLine === undefined
25
+ ? `::${severity} file=${source},line=${line},col=${column},title=${title}::${msg}`
26
+ : `::${severity} file=${source},line=${line},col=${column},endLine=${endLine},endColumn=${endColumn},title=${title}::${msg}`;
27
+ });
28
+ });
29
+
30
+ lines.push('');
31
+
32
+ return lines.join('\n');
33
+ }
34
+
35
+ /**
36
+ * @param {string} msg
37
+ * @param {Partial<import('stylelint').RuleMeta> | undefined} metadata
38
+ * @returns {string}
39
+ */
40
+ function buildMessage(msg, metadata) {
41
+ if (!metadata) {
42
+ return msg;
43
+ }
44
+
45
+ const url = metadata.url ? ` - ${metadata.url}` : '';
46
+
47
+ let additional = [
48
+ metadata.fixable ? 'maybe fixable' : '',
49
+ metadata.deprecated ? 'deprecated' : '',
50
+ ]
51
+ .filter(Boolean)
52
+ .join(', ');
53
+
54
+ additional = additional ? ` [${additional}]` : '';
55
+
56
+ return `${msg}${additional}${url}`;
57
+ }
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@csstools/stylelint-formatter-github",
3
+ "description": "Format Stylelint errors as GitHub annotations",
4
+ "version": "1.0.0",
5
+ "contributors": [
6
+ {
7
+ "name": "Antonio Laguna",
8
+ "email": "antonio@laguna.es",
9
+ "url": "https://antonio.laguna.es"
10
+ },
11
+ {
12
+ "name": "Romain Menke",
13
+ "email": "romainmenke@gmail.com"
14
+ }
15
+ ],
16
+ "license": "MIT-0",
17
+ "funding": [
18
+ {
19
+ "type": "github",
20
+ "url": "https://github.com/sponsors/csstools"
21
+ },
22
+ {
23
+ "type": "opencollective",
24
+ "url": "https://opencollective.com/csstools"
25
+ }
26
+ ],
27
+ "engines": {
28
+ "node": ">=18"
29
+ },
30
+ "type": "module",
31
+ "main": "index.mjs",
32
+ "files": [
33
+ "LICENSE.md",
34
+ "README.md",
35
+ "index.mjs",
36
+ "preprocess-warnings.mjs"
37
+ ],
38
+ "peerDependencies": {
39
+ "stylelint": "^16.6.0"
40
+ },
41
+ "scripts": {},
42
+ "homepage": "https://github.com/csstools/postcss-plugins/blob/main/plugins-stylelint/formatter-github#readme",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/csstools/postcss-plugins.git",
46
+ "directory": "plugins-stylelint/formatter-github"
47
+ },
48
+ "bugs": "https://github.com/csstools/postcss-plugins/issues",
49
+ "keywords": [
50
+ "github",
51
+ "stylelint",
52
+ "stylelint-formatter"
53
+ ]
54
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @see https://github.com/stylelint/stylelint/blob/cfe2bcff69a4c6ffb2839552a1fec36cf4971895/lib/formatters/preprocessWarnings.mjs
3
+ *
4
+ * @license MIT https://github.com/stylelint/stylelint/blob/main/LICENSE
5
+ */
6
+
7
+ /** @import {LintResult} from 'stylelint' */
8
+ /** @typedef {LintResult['parseErrors'][0]} ParseError */
9
+ /** @typedef {LintResult['warnings'][0]} Warning */
10
+ /** @typedef {Warning['severity']} Severity */
11
+
12
+ /**
13
+ * Preprocess warnings in a given lint result.
14
+ * Note that this function has a side-effect.
15
+ *
16
+ * @param {LintResult} result
17
+ * @returns {LintResult}
18
+ */
19
+ export default function preprocessWarnings(result) {
20
+ for (const error of result.parseErrors || []) {
21
+ result.warnings.push(parseErrorToWarning(error));
22
+ }
23
+
24
+ for (const warning of result.warnings) {
25
+ warning.severity = normalizeSeverity(warning);
26
+ }
27
+
28
+ result.warnings.sort(byLocationOrder);
29
+
30
+ return result;
31
+ }
32
+
33
+ /**
34
+ * @param {ParseError} error
35
+ * @returns {Warning}
36
+ */
37
+ function parseErrorToWarning(error) {
38
+ return {
39
+ line: error.line,
40
+ column: error.column,
41
+ rule: error.stylelintType,
42
+ severity: 'error',
43
+ text: `${error.text} (${error.stylelintType})`,
44
+ };
45
+ }
46
+
47
+ /**
48
+ * @param {Warning} warning
49
+ * @returns {Severity}
50
+ */
51
+ function normalizeSeverity(warning) {
52
+ // NOTE: Plugins may add a warning without severity, for example,
53
+ // by directly using the PostCSS `Result#warn()` API.
54
+ return warning.severity || 'error';
55
+ }
56
+
57
+ /**
58
+ * @param {Warning} a
59
+ * @param {Warning} b
60
+ * @returns {number}
61
+ */
62
+ function byLocationOrder(a, b) {
63
+ // positionless first
64
+ if (!a.line && b.line) {
65
+ return -1;
66
+ }
67
+
68
+ // positionless first
69
+ if (a.line && !b.line) {
70
+ return 1;
71
+ }
72
+
73
+ if (a.line < b.line) {
74
+ return -1;
75
+ }
76
+
77
+ if (a.line > b.line) {
78
+ return 1;
79
+ }
80
+
81
+ if (a.column < b.column) {
82
+ return -1;
83
+ }
84
+
85
+ if (a.column > b.column) {
86
+ return 1;
87
+ }
88
+
89
+ return 0;
90
+ }