From 3f54025b0534bc9689c278e1a59b34e8031d7c09 Mon Sep 17 00:00:00 2001 From: Brian Surowiec Date: Thu, 28 Apr 2022 21:22:36 -0400 Subject: [PATCH] Match errors and warnings --- CHANGELOG.md | 2 + __tests__/problemMatcher.test.ts | 222 ++++++++++++++++++++++--------- src/main.ts | 18 +-- src/problem-matcher.json | 21 ++- 4 files changed, 193 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d38dd4b..bff19fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ - Updated to run on Node 16 - Converted to es modules +- Updated problem matcher to support errors and warnings + - The problem matcher owner changed from `stylelint` to `stylelint-error` and the new one is `stylelint-warning` ## [1.3.0](https://github.com/xt0rted/stylelint-problem-matcher/compare/v1.2.0...v1.3.0) - 2022-04-28 diff --git a/__tests__/problemMatcher.test.ts b/__tests__/problemMatcher.test.ts index a6f21c7..d611cf6 100644 --- a/__tests__/problemMatcher.test.ts +++ b/__tests__/problemMatcher.test.ts @@ -10,94 +10,194 @@ import type { } from "github-actions-problem-matcher-typings"; describe("problemMatcher", () => { - let problemMatcher: ProblemMatcher; + let problemMatcherDocument: ProblemMatcherDocument; beforeAll(async () => { const matcherFile = fileURLToPath(new URL("../src/problem-matcher.json", import.meta.url)); const fileContents = await readFile(matcherFile, { encoding: "utf8" }); - const problemMatcherDocument = JSON.parse(fileContents) as ProblemMatcherDocument; - problemMatcher = problemMatcherDocument.problemMatcher[0]; + problemMatcherDocument = JSON.parse(fileContents); }); - it("has the correct owner", () => { - expect(problemMatcher.owner).toEqual("stylelint"); + it("has two matchers", () => { + expect(problemMatcherDocument.problemMatcher.length).toEqual(2); }); - it("has two patterns", () => { - expect(problemMatcher.pattern.length).toEqual(2); - }); - - describe("file pattern", () => { - let pattern: ProblemPattern; - let regexp: RegExp; + describe("error matcher", () => { + let problemMatcher: ProblemMatcher; - beforeEach(() => { - pattern = problemMatcher.pattern[0]; - regexp = new RegExp(pattern.regexp); + beforeAll(() => { + problemMatcher = problemMatcherDocument.problemMatcher[0]; }); - it("matches file path", () => { - const reportOutput = [ - "scss/_test.scss", - " 11:16 × Unexpected unit length-zero-no-unit", - " 11:28 × Unexpected unit length-zero-no-unit", - ]; - - const results = matchResults(reportOutput, regexp); + it("has the correct owner", () => { + expect(problemMatcher.owner).toEqual("stylelint-error"); + }); - expect(results.length).toEqual(1); - expect(results[0][pattern.file!]).toEqual("scss/_test.scss"); + it("has two patterns", () => { + expect(problemMatcher.pattern.length).toEqual(2); }); + + describe("file pattern", () => { + let pattern: ProblemPattern; + let regexp: RegExp; + + beforeEach(() => { + pattern = problemMatcher.pattern[0]; + regexp = new RegExp(pattern.regexp); + }); + + it("matches file path", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ✖ Unexpected unit length-zero-no-unit", + " 11:28 ✖ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(1); + expect(results[0][pattern.file!]).toEqual("scss/_test.scss"); + }); + }); + + describe("violation pattern", () => { + let pattern: ProblemPattern; + let regexp: RegExp; + + beforeEach(() => { + pattern = problemMatcher.pattern[1]; + regexp = new RegExp(pattern.regexp); + }); + + it("matches violations", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ✖ Unexpected unit length-zero-no-unit", + " 11:28 ✖ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(2); + }); + + it("matches violations without line numbers", () => { + const reportOutput = [ + "scss/_test.scss", + " ✖ Unexpected Unicode BOM unicode-bom", + " 11:16 ✖ Unexpected unit length-zero-no-unit", + " 11:28 ✖ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(3); + }); + + it("matches violation details", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ✖ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(1); + expect(results[0][pattern.line!]).toEqual("11"); + expect(results[0][pattern.column!]).toEqual("16"); + expect(results[0][pattern.message!]).toEqual("Unexpected unit "); + expect(results[0][pattern.code!]).toEqual("length-zero-no-unit"); + }); + }); }); - describe("violation pattern", () => { - let pattern: ProblemPattern; - let regexp: RegExp; + describe("warning matcher", () => { + let problemMatcher: ProblemMatcher; - beforeEach(() => { - pattern = problemMatcher.pattern[1]; - regexp = new RegExp(pattern.regexp); + beforeAll(() => { + problemMatcher = problemMatcherDocument.problemMatcher[1]; }); - it("matches violations", () => { - const reportOutput = [ - "scss/_test.scss", - " 11:16 × Unexpected unit length-zero-no-unit", - " 11:28 × Unexpected unit length-zero-no-unit", - ]; - - const results = matchResults(reportOutput, regexp); + it("has the correct owner", () => { + expect(problemMatcher.owner).toEqual("stylelint-warning"); + }); - expect(results.length).toEqual(2); + it("has two patterns", () => { + expect(problemMatcher.pattern.length).toEqual(2); }); - it("matches violations without line numbers", () => { - const reportOutput = [ - "scss/_test.scss", - " × Unexpected Unicode BOM unicode-bom", - " 11:16 × Unexpected unit length-zero-no-unit", - " 11:28 × Unexpected unit length-zero-no-unit", - ]; + describe("file pattern", () => { + let pattern: ProblemPattern; + let regexp: RegExp; - const results = matchResults(reportOutput, regexp); + beforeEach(() => { + pattern = problemMatcher.pattern[0]; + regexp = new RegExp(pattern.regexp); + }); - expect(results.length).toEqual(3); - }); + it("matches file path", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ⚠ Unexpected unit length-zero-no-unit", + " 11:28 ⚠ Unexpected unit length-zero-no-unit", + ]; - it("matches violation details", () => { - const reportOutput = [ - "scss/_test.scss", - " 11:16 × Unexpected unit length-zero-no-unit", - ]; + const results = matchResults(reportOutput, regexp); - const results = matchResults(reportOutput, regexp); + expect(results.length).toEqual(1); + expect(results[0][pattern.file!]).toEqual("scss/_test.scss"); + }); + }); - expect(results.length).toEqual(1); - expect(results[0][pattern.line!]).toEqual("11"); - expect(results[0][pattern.column!]).toEqual("16"); - expect(results[0][pattern.message!]).toEqual("Unexpected unit "); - expect(results[0][pattern.code!]).toEqual("length-zero-no-unit"); + describe("violation pattern", () => { + let pattern: ProblemPattern; + let regexp: RegExp; + + beforeEach(() => { + pattern = problemMatcher.pattern[1]; + regexp = new RegExp(pattern.regexp); + }); + + it("matches violations", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ⚠ Unexpected unit length-zero-no-unit", + " 11:28 ⚠ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(2); + }); + + it("matches violations without line numbers", () => { + const reportOutput = [ + "scss/_test.scss", + " ⚠ Unexpected Unicode BOM unicode-bom", + " 11:16 ⚠ Unexpected unit length-zero-no-unit", + " 11:28 ⚠ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(3); + }); + + it("matches violation details", () => { + const reportOutput = [ + "scss/_test.scss", + " 11:16 ⚠ Unexpected unit length-zero-no-unit", + ]; + + const results = matchResults(reportOutput, regexp); + + expect(results.length).toEqual(1); + expect(results[0][pattern.line!]).toEqual("11"); + expect(results[0][pattern.column!]).toEqual("16"); + expect(results[0][pattern.message!]).toEqual("Unexpected unit "); + expect(results[0][pattern.code!]).toEqual("length-zero-no-unit"); + }); }); }); }); diff --git a/src/main.ts b/src/main.ts index 6ae395f..730ac9c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,15 +27,17 @@ export async function run(): Promise { case "remove": const fileContents = await readFile(matcherFile, { encoding: "utf8" }); const problemMatcherDocument: ProblemMatcherDocument = JSON.parse(fileContents); - const problemMatcher = problemMatcherDocument.problemMatcher[0]; - issueCommand( - "remove-matcher", - { - owner: problemMatcher.owner, - }, - "", - ); + for (const problemMatcher of problemMatcherDocument.problemMatcher) { + issueCommand( + "remove-matcher", + { + owner: problemMatcher.owner, + }, + "", + ); + } + break; default: diff --git a/src/problem-matcher.json b/src/problem-matcher.json index b0cf76b..54e05d6 100644 --- a/src/problem-matcher.json +++ b/src/problem-matcher.json @@ -1,7 +1,8 @@ { "problemMatcher": [ { - "owner": "stylelint", + "owner": "stylelint-error", + "severity": "error", "pattern": [ { "regexp": "^([^\\s].*)$", @@ -16,6 +17,24 @@ "loop": true } ] + }, + { + "owner": "stylelint-warning", + "severity": "warning", + "pattern": [ + { + "regexp": "^([^\\s].*)$", + "file": 1 + }, + { + "regexp": "^\\s+((\\d+):(\\d+))?\\s+(⚠|‼)\\s+(.*)\\s{3,}(.*)$", + "line": 2, + "column": 3, + "message": 5, + "code": 6, + "loop": true + } + ] } ] }