From cdf8bb758a2c9b4b5824a9f85697f17528dd0625 Mon Sep 17 00:00:00 2001 From: MeilCli Date: Mon, 10 May 2021 19:56:24 +0900 Subject: [PATCH] feature: add stylelint transformer --- .prettierignore | 3 +- README.md | 2 + __test__/transformer/stylelint.test.ts | 41 ++++++++++++++++++++ data/stylelint.json | 32 ++++++++++++++++ data/stylelint/.stylelintrc.json | 3 ++ data/stylelint/command.md | 3 ++ data/stylelint/test.css | 3 ++ documents/transformer/stylelint.md | 20 ++++++++++ src/transformer/stylelint.ts | 53 ++++++++++++++++++++++++++ transformer/stylelint/action.yml | 19 +++++++++ webpack.config.js | 1 + 11 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 __test__/transformer/stylelint.test.ts create mode 100644 data/stylelint.json create mode 100644 data/stylelint/.stylelintrc.json create mode 100644 data/stylelint/command.md create mode 100644 data/stylelint/test.css create mode 100644 documents/transformer/stylelint.md create mode 100644 src/transformer/stylelint.ts create mode 100644 transformer/stylelint/action.yml diff --git a/.prettierignore b/.prettierignore index 2e1fa2d5..207baeb3 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,2 @@ -*.md \ No newline at end of file +*.md +data/stylelint/test.css \ No newline at end of file diff --git a/README.md b/README.md index eb8b9dcd..a13dfd17 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Current supporting lint file format: - eslint(JSON) - textlint(JSON) - markdownlint +- stylelint(JSON) - checkstyle - junit - compatibility: eslint, textlint, cpplint @@ -65,6 +66,7 @@ jobs: - [eslint](documents/transformer/eslint.md) - [textlint](documents/transformer/textlint.md) - [markdownlint](documents/transformer/markdownlint.md) + - [stylelint](documents/transformer/stylelint.md) - [checkstyle](documents/transformer/checkstyle.md) - [junit](documents/transformer/junit.md) - Operator diff --git a/__test__/transformer/stylelint.test.ts b/__test__/transformer/stylelint.test.ts new file mode 100644 index 00000000..74cabab1 --- /dev/null +++ b/__test__/transformer/stylelint.test.ts @@ -0,0 +1,41 @@ +import { StyleLintTransformer } from "../../src/transformer/stylelint"; +import { LintResult } from "../../src/lint-result"; +import * as fs from "fs"; + +test("transform", async () => { + const text = fs.readFileSync("data/stylelint.json", "utf-8"); + const transformer = new StyleLintTransformer(); + const result = transformer.parse(text); + + expect(result.length).toBe(3); + expect(result[0]).toMatchObject({ + path: "test/test.css", + rule: "block-opening-brace-space-before", + message: 'Expected single space before "{"', + startLine: 1, + endLine: undefined, + startColumn: undefined, + endColumn: undefined, + level: "failure", + } as LintResult); + expect(result[1]).toMatchObject({ + path: "test/test.css", + rule: "no-missing-end-of-source-newline", + message: "Unexpected missing end-of-source newline", + startLine: 3, + endLine: undefined, + startColumn: undefined, + endColumn: undefined, + level: "failure", + } as LintResult); + expect(result[2]).toMatchObject({ + path: "test/test.css", + rule: "indentation", + message: "Expected indentation of 2 spaces", + startLine: 2, + endLine: undefined, + startColumn: undefined, + endColumn: undefined, + level: "failure", + } as LintResult); +}); diff --git a/data/stylelint.json b/data/stylelint.json new file mode 100644 index 00000000..56ef3b45 --- /dev/null +++ b/data/stylelint.json @@ -0,0 +1,32 @@ +[ + { + "source": "test/test.css", + "deprecations": [], + "invalidOptionWarnings": [], + "parseErrors": [], + "errored": true, + "warnings": [ + { + "line": 1, + "column": 5, + "rule": "block-opening-brace-space-before", + "severity": "error", + "text": "Expected single space before \"{\" (block-opening-brace-space-before)" + }, + { + "line": 3, + "column": 1, + "rule": "no-missing-end-of-source-newline", + "severity": "error", + "text": "Unexpected missing end-of-source newline (no-missing-end-of-source-newline)" + }, + { + "line": 2, + "column": 5, + "rule": "indentation", + "severity": "error", + "text": "Expected indentation of 2 spaces (indentation)" + } + ] + } +] diff --git a/data/stylelint/.stylelintrc.json b/data/stylelint/.stylelintrc.json new file mode 100644 index 00000000..7cad7a12 --- /dev/null +++ b/data/stylelint/.stylelintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "stylelint-config-standard" +} diff --git a/data/stylelint/command.md b/data/stylelint/command.md new file mode 100644 index 00000000..7c9b036d --- /dev/null +++ b/data/stylelint/command.md @@ -0,0 +1,3 @@ +1. `npm install -g stylelint stylelint-config-standard` +1. `stylelint data/stylelint/test.css --config data/stylelint/.stylelintrc.json --output-file data/stylelint.json --formatter json` +1. replace filepath \ No newline at end of file diff --git a/data/stylelint/test.css b/data/stylelint/test.css new file mode 100644 index 00000000..c94feced --- /dev/null +++ b/data/stylelint/test.css @@ -0,0 +1,3 @@ +.test{ + background-color: blue; +} \ No newline at end of file diff --git a/documents/transformer/stylelint.md b/documents/transformer/stylelint.md new file mode 100644 index 00000000..58309305 --- /dev/null +++ b/documents/transformer/stylelint.md @@ -0,0 +1,20 @@ +# Transformer of stylelint +```yml +- uses: MeilCli/common-lint-reporter/transformer/stylelint@v0 + with: + report_files: | + stylelint_report.json +``` + +## Option +### Input +- `report_files`: + - report file glob pattern + - required +- `report_files_follow_symbolic_links`: + - report file glob pattern option + - value: `true` or `false` +- `output_path`: + - output path + - required + - default: `common_lint.json` \ No newline at end of file diff --git a/src/transformer/stylelint.ts b/src/transformer/stylelint.ts new file mode 100644 index 00000000..8b239b0d --- /dev/null +++ b/src/transformer/stylelint.ts @@ -0,0 +1,53 @@ +import * as core from "@actions/core"; +import { getOption } from "./option"; +import { LintResult } from "../lint-result"; +import { Transformer } from "./transformer"; + +interface StyleLintReport { + source: string; + warnings: StyleLintReportMessage[]; +} + +interface StyleLintReportMessage { + rule: string; + severity: string; + text: string; + line: number; +} + +export class StyleLintTransformer extends Transformer { + parse(body: string): LintResult[] { + const lintResults: LintResult[] = []; + const styleLintReports = JSON.parse(body) as StyleLintReport[]; + for (const styleLintReport of styleLintReports) { + for (const message of styleLintReport.warnings) { + const level = message.severity == "error" ? "failure" : "warning"; + lintResults.push({ + path: styleLintReport.source, + rule: message.rule, + message: message.text.replace(/^(.+)\s(\(.+?\))$/, "$1"), + startLine: message.line, + endLine: undefined, + startColumn: undefined, + endColumn: undefined, + level: level, + }); + } + } + return lintResults; + } +} + +async function run() { + try { + const option = getOption(); + const transformer = new StyleLintTransformer(); + await transformer.transform(option); + } catch (error) { + core.setFailed(error.message); + } +} + +if (process.env.NODE_ENV != "test") { + run(); +} diff --git a/transformer/stylelint/action.yml b/transformer/stylelint/action.yml new file mode 100644 index 00000000..ca0a5aee --- /dev/null +++ b/transformer/stylelint/action.yml @@ -0,0 +1,19 @@ +name: 'stylelint transformer of common lint reporter' +description: 'transformer of stylelint report file to common lint report' +author: 'MeilCli' +branding: + icon: layers + color: orange +inputs: + report_files: + description: 'report file glob pattern' + required: true + report_files_follow_symbolic_links: + description: 'report file glob pattern option' + output_path: + description: 'output path' + required: true + default: 'common_lint.json' +runs: + using: 'node12' + main: '../../dist/transformer-stylelint.js' diff --git a/webpack.config.js b/webpack.config.js index 1e103148..d285b522 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -9,6 +9,7 @@ module.exports = { "transformer-eslint": "./src/transformer/eslint.ts", "transformer-textlint": "./src/transformer/textlint.ts", "transformer-markdownlint": "./src/transformer/markdownlint.ts", + "transformer-stylelint": "./src/transformer/stylelint.ts", "transformer-junit": "./src/transformer/junit.ts", "operator-filter": "./src/operator/filter.ts", "operator-filter-by-file-changed": "./src/operator/filter-by-file-changed.ts",