Skip to content

Commit

Permalink
feature: add stylelint transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
MeilCli committed May 10, 2021
1 parent 41b736b commit cdf8bb7
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.md
*.md
data/stylelint/test.css
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Current supporting lint file format:
- eslint(JSON)
- textlint(JSON)
- markdownlint
- stylelint(JSON)
- checkstyle
- junit
- compatibility: eslint, textlint, cpplint
Expand Down Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions __test__/transformer/stylelint.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
32 changes: 32 additions & 0 deletions data/stylelint.json
Original file line number Diff line number Diff line change
@@ -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)"
}
]
}
]
3 changes: 3 additions & 0 deletions data/stylelint/.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "stylelint-config-standard"
}
3 changes: 3 additions & 0 deletions data/stylelint/command.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions data/stylelint/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test{
background-color: blue;
}
20 changes: 20 additions & 0 deletions documents/transformer/stylelint.md
Original file line number Diff line number Diff line change
@@ -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`
53 changes: 53 additions & 0 deletions src/transformer/stylelint.ts
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 19 additions & 0 deletions transformer/stylelint/action.yml
Original file line number Diff line number Diff line change
@@ -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'
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cdf8bb7

Please sign in to comment.