From 50bdc0a93db57359438688b6ddb88336b283becb Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Mon, 21 Aug 2023 20:12:33 +0900 Subject: [PATCH] chore: fix test for postcss-scss v4.0.7 --- package.json | 2 +- .../empty-style-element-output.json | 4 +- .../style-context/one-line-css-output.json | 20 ++-- .../style-context/parse-error-output.json | 4 +- .../style-context/simple-css-output.json | 36 +++---- .../style-context/simple-scss-output.json | 52 ++++----- .../unrelated-style-attr-output.json | 36 +++---- tests/src/parser/style-context.ts | 18 +--- tests/src/parser/test-utils.ts | 100 ++++++++++++++++++ tools/update-fixtures.ts | 14 +-- 10 files changed, 183 insertions(+), 103 deletions(-) diff --git a/package.json b/package.json index eca561d1..e9d372d8 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "eslint-visitor-keys": "^3.0.0", "espree": "^9.0.0", "postcss": "^8.4.28", - "postcss-scss": "^4.0.6" + "postcss-scss": "^4.0.7" }, "devDependencies": { "@changesets/changelog-github": "^0.4.8", diff --git a/tests/fixtures/parser/style-context/empty-style-element-output.json b/tests/fixtures/parser/style-context/empty-style-element-output.json index e03a6244..8f5db192 100644 --- a/tests/fixtures/parser/style-context/empty-style-element-output.json +++ b/tests/fixtures/parser/style-context/empty-style-element-output.json @@ -9,14 +9,14 @@ "nodes": [], "source": { "end": { - "column": 8, "line": 7, + "column": 8, "offset": 52 }, "inputId": 0, "start": { - "column": 8, "line": 7, + "column": 8, "offset": 52 } }, diff --git a/tests/fixtures/parser/style-context/one-line-css-output.json b/tests/fixtures/parser/style-context/one-line-css-output.json index 052d8177..a53d7791 100644 --- a/tests/fixtures/parser/style-context/one-line-css-output.json +++ b/tests/fixtures/parser/style-context/one-line-css-output.json @@ -23,51 +23,51 @@ "between": ": " }, "type": "decl", + "prop": "color", + "value": "red", "source": { "end": { - "column": 30, "line": 7, + "column": 30, "offset": 99 }, "inputId": 0, "start": { - "column": 20, "line": 7, + "column": 20, "offset": 89 } - }, - "prop": "color", - "value": "red" + } } ], + "selector": ".myClass", "source": { "end": { - "column": 32, "line": 7, + "column": 32, "offset": 101 }, "inputId": 0, "start": { - "column": 9, "line": 7, + "column": 9, "offset": 78 } }, - "selector": ".myClass", "lastEach": 1, "indexes": {} } ], "source": { "end": { - "column": 34, "line": 7, + "column": 34, "offset": 103 }, "inputId": 0, "start": { - "column": 8, "line": 7, + "column": 8, "offset": 77 } }, diff --git a/tests/fixtures/parser/style-context/parse-error-output.json b/tests/fixtures/parser/style-context/parse-error-output.json index 965e96b6..694cc9e0 100644 --- a/tests/fixtures/parser/style-context/parse-error-output.json +++ b/tests/fixtures/parser/style-context/parse-error-output.json @@ -10,10 +10,10 @@ "endLine": 4, "endColumn": 24, "input": { + "line": 4, "column": 11, - "endColumn": 24, "endLine": 4, - "line": 4, + "endColumn": 24, "source": "\n // This syntax is intentionally invalid CSS - this is to be used to test resiliency against invalid input\n .container {\n class .div-class/35\n # Weird comment\n color: red;\n\n .span-class begin\n font-weight: bold;\n end\n }\n" } } diff --git a/tests/fixtures/parser/style-context/simple-css-output.json b/tests/fixtures/parser/style-context/simple-css-output.json index 26b4888c..5ee1a9bb 100644 --- a/tests/fixtures/parser/style-context/simple-css-output.json +++ b/tests/fixtures/parser/style-context/simple-css-output.json @@ -23,37 +23,37 @@ "between": ": " }, "type": "decl", + "prop": "color", + "value": "red", "source": { "end": { - "column": 15, "line": 11, + "column": 15, "offset": 117 }, "inputId": 0, "start": { - "column": 5, "line": 11, + "column": 5, "offset": 107 } - }, - "prop": "color", - "value": "red" + } } ], + "selector": ".myClass", "source": { "end": { - "column": 3, "line": 12, + "column": 3, "offset": 121 }, "inputId": 0, "start": { - "column": 3, "line": 10, + "column": 3, "offset": 92 } }, - "selector": ".myClass", "lastEach": 1, "indexes": {} }, @@ -72,51 +72,51 @@ "between": ": " }, "type": "decl", + "prop": "font-size", + "value": "xx-large", "source": { "end": { - "column": 24, "line": 15, + "column": 24, "offset": 153 }, "inputId": 0, "start": { - "column": 5, "line": 15, + "column": 5, "offset": 134 } - }, - "prop": "font-size", - "value": "xx-large" + } } ], + "selector": "b", "source": { "end": { - "column": 3, "line": 16, + "column": 3, "offset": 157 }, "inputId": 0, "start": { - "column": 3, "line": 14, + "column": 3, "offset": 126 } }, - "selector": "b", "lastEach": 1, "indexes": {} } ], "source": { "end": { - "column": 1, "line": 17, + "column": 1, "offset": 159 }, "inputId": 0, "start": { - "column": 8, "line": 9, + "column": 8, "offset": 89 } }, diff --git a/tests/fixtures/parser/style-context/simple-scss-output.json b/tests/fixtures/parser/style-context/simple-scss-output.json index 42a09d6e..06a7bcc2 100644 --- a/tests/fixtures/parser/style-context/simple-scss-output.json +++ b/tests/fixtures/parser/style-context/simple-scss-output.json @@ -35,20 +35,20 @@ "text": "This is an inline comment" }, "type": "comment", + "text": "This is an inline comment", "source": { "end": { - "offset": 196, "line": 10, - "column": 34 + "column": 34, + "offset": 196 }, "inputId": 0, "start": { - "column": 7, "line": 10, + "column": 7, "offset": 169 } - }, - "text": "This is an inline comment" + } }, { "raws": { @@ -56,37 +56,37 @@ "between": ": " }, "type": "decl", + "prop": "color", + "value": "red", "source": { "end": { - "column": 17, "line": 11, + "column": 17, "offset": 214 }, "inputId": 0, "start": { - "column": 7, "line": 11, + "column": 7, "offset": 204 } - }, - "prop": "color", - "value": "red" + } } ], + "selector": ".div-class", "source": { "end": { - "column": 5, "line": 12, + "column": 5, "offset": 220 }, "inputId": 0, "start": { - "column": 5, "line": 9, + "column": 5, "offset": 150 } }, - "selector": ".div-class", "lastEach": 1, "indexes": {} }, @@ -105,69 +105,69 @@ "between": ": " }, "type": "decl", + "prop": "font-weight", + "value": "bold", "source": { "end": { - "column": 24, "line": 15, + "column": 24, "offset": 264 }, "inputId": 0, "start": { - "column": 7, "line": 15, + "column": 7, "offset": 247 } - }, - "prop": "font-weight", - "value": "bold" + } } ], + "selector": ".span-class", "source": { "end": { - "column": 5, "line": 16, + "column": 5, "offset": 270 }, "inputId": 0, "start": { - "column": 5, "line": 14, + "column": 5, "offset": 227 } }, - "selector": ".span-class", "lastEach": 1, "indexes": {} } ], + "selector": ".container", "source": { "end": { - "column": 3, "line": 17, + "column": 3, "offset": 274 }, "inputId": 0, "start": { - "column": 3, "line": 8, + "column": 3, "offset": 133 } }, - "selector": ".container", "lastEach": 1, "indexes": {} } ], "source": { "end": { - "column": 1, "line": 18, + "column": 1, "offset": 276 }, "inputId": 0, "start": { - "column": 20, "line": 7, + "column": 20, "offset": 130 } }, diff --git a/tests/fixtures/parser/style-context/unrelated-style-attr-output.json b/tests/fixtures/parser/style-context/unrelated-style-attr-output.json index c5744ce8..584799ef 100644 --- a/tests/fixtures/parser/style-context/unrelated-style-attr-output.json +++ b/tests/fixtures/parser/style-context/unrelated-style-attr-output.json @@ -23,37 +23,37 @@ "between": ": " }, "type": "decl", + "prop": "color", + "value": "red", "source": { "end": { - "column": 15, "line": 11, + "column": 15, "offset": 134 }, "inputId": 0, "start": { - "column": 5, "line": 11, + "column": 5, "offset": 124 } - }, - "prop": "color", - "value": "red" + } } ], + "selector": ".myClass", "source": { "end": { - "column": 3, "line": 12, + "column": 3, "offset": 138 }, "inputId": 0, "start": { - "column": 3, "line": 10, + "column": 3, "offset": 109 } }, - "selector": ".myClass", "lastEach": 1, "indexes": {} }, @@ -72,51 +72,51 @@ "between": ": " }, "type": "decl", + "prop": "font-size", + "value": "xx-large", "source": { "end": { - "column": 24, "line": 15, + "column": 24, "offset": 170 }, "inputId": 0, "start": { - "column": 5, "line": 15, + "column": 5, "offset": 151 } - }, - "prop": "font-size", - "value": "xx-large" + } } ], + "selector": "b", "source": { "end": { - "column": 3, "line": 16, + "column": 3, "offset": 174 }, "inputId": 0, "start": { - "column": 3, "line": 14, + "column": 3, "offset": 143 } }, - "selector": "b", "lastEach": 1, "indexes": {} } ], "source": { "end": { - "column": 1, "line": 17, + "column": 1, "offset": 176 }, "inputId": 0, "start": { - "column": 25, "line": 9, + "column": 25, "offset": 106 } }, diff --git a/tests/src/parser/style-context.ts b/tests/src/parser/style-context.ts index 69a2750f..87aee31a 100644 --- a/tests/src/parser/style-context.ts +++ b/tests/src/parser/style-context.ts @@ -3,8 +3,11 @@ import fs from "fs"; import path from "path"; import { parseForESLint } from "../../../src"; -import type { StyleContext } from "../../../src/parser"; -import { generateParserOptions, listupFixtures } from "./test-utils"; +import { + generateParserOptions, + listupFixtures, + styleContextToJson, +} from "./test-utils"; const STYLE_CONTEXT_FIXTURE_ROOT = path.resolve( __dirname, @@ -38,14 +41,3 @@ describe("Check for AST.", () => { }); } }); - -function styleContextToJson(styleContext: StyleContext): string { - return JSON.stringify(styleContext, nodeReplacer, 2); - - function nodeReplacer(key: string, value: any): any { - if (key === "file" || key === "url") { - return undefined; - } - return value; - } -} diff --git a/tests/src/parser/test-utils.ts b/tests/src/parser/test-utils.ts index 57dc7306..fe994b5d 100644 --- a/tests/src/parser/test-utils.ts +++ b/tests/src/parser/test-utils.ts @@ -7,6 +7,7 @@ import { LinesAndColumns } from "../../../src/context"; import type { Reference, Scope, ScopeManager, Variable } from "eslint-scope"; import type * as TSESScopes from "@typescript-eslint/scope-manager"; import type { SvelteNode } from "../../../src/ast"; +import type { StyleContext } from "../../../src"; const AST_FIXTURE_ROOT = path.resolve(__dirname, "../../fixtures/parser/ast"); const BASIC_PARSER_OPTIONS: Linter.ParserOptions = { @@ -189,6 +190,105 @@ export function scopeToJSON( return JSON.stringify(scopeData, nodeReplacer, 2); } +export function styleContextToJson(styleContext: StyleContext): string { + const normalized = new Set(); + return JSON.stringify(styleContext, nodeReplacer, 2); + + function nodeReplacer(key: string, value: any): any { + if (key === "file" || key === "url") { + return undefined; + } + return normalizePostcssObject(value); + } + + function normalizePostcssObject( + value: any, + defaultCompare = (_a: string, _b: string) => 0, + ) { + if ( + !value || + typeof value !== "object" || + Array.isArray(value) || + normalized.has(value) + ) { + return value; + } + let compare = defaultCompare; + if (value.sourceLang && typeof value.sourceLang === "string") { + // It is context + const order = ["status", "sourceLang", "sourceAst", "error"]; + compare = (a, b) => compareOrder(a, b, order); + } else if (typeof value.type === "string") { + // It is node + compare = (a, b) => + nodeFirsts(a, value.type) - nodeFirsts(b, value.type) || + nodeLasts(a, value.type) - nodeLasts(b, value.type); + if (value.raws && typeof value.raws === "object") { + const order = ["before", "between", "semicolon", "after"]; + value.raws = normalizePostcssObject(value.raws, (a, b) => + compareOrder(a, b, order), + ); + } + } else if ( + typeof value.reason === "string" && + typeof value.name === "string" + ) { + // It is error + const order = [ + "name", + "reason", + "source", + "line", + "column", + "endLine", + "endColumn", + "input", + ]; + compare = (a, b) => order.indexOf(a) - order.indexOf(b); + } else if ( + typeof value.line === "number" && + typeof value.column === "number" + ) { + // It is location + const order = ["line", "column", "offset", "endLine", "endColumn"]; + compare = (a, b) => compareOrder(a, b, order); + } else if (typeof value.hasBOM === "boolean") { + // It is inputs + const order = ["hasBOM", "css"]; + compare = (a, b) => compareOrder(a, b, order); + } + + function nodeFirsts(k: string, _nodeType: string | null) { + const o = ["raws", "type"].indexOf(k); + return o === -1 ? Infinity : o; + } + + function nodeLasts(k: string, _nodeType: string | null) { + return ["source", "lastEach", "indexes", "inputs"].indexOf(k); + } + + function compareOrder(a: string, b: string, order: string[]) { + const oA = order.includes(a) ? order.indexOf(a) : Infinity; + const oB = order.includes(b) ? order.indexOf(b) : Infinity; + return oA - oB; + } + + const entries = Object.entries(value); + + const result = Object.fromEntries( + entries.sort(([a], [b]) => { + const c = compare(a, b); + if (c) { + return c; + } + return a < b ? -1 : a > b ? 1 : 0; + }), + ); + normalized.add(result); + return result; + } +} + function normalizeScope(scope: Scope | TSESScopes.Scope): any { return { type: scope.type, diff --git a/tools/update-fixtures.ts b/tools/update-fixtures.ts index 11c8b99c..5a23f8bb 100644 --- a/tools/update-fixtures.ts +++ b/tools/update-fixtures.ts @@ -10,6 +10,7 @@ import { astToJson, normalizeError, scopeToJSON, + styleContextToJson, } from "../tests/src/parser/test-utils"; import type ts from "typescript"; import type ESTree from "estree"; @@ -150,19 +151,6 @@ for (const { input, inputFileName, outputFileName, config } of listupFixtures( ); } -/** StyleContext to JSON string */ -function styleContextToJson(styleContext: parser.StyleContext): string { - return JSON.stringify(styleContext, nodeReplacer, 2); - - /** JSON string replacer for StyleContext */ - function nodeReplacer(key: string, value: any): any { - if (key === "file" || key === "url") { - return undefined; - } - return value; - } -} - for (const { input, inputFileName, outputFileName, config } of listupFixtures( STYLE_LOCATION_FIXTURE_ROOT, )) {