|
| 1 | +import tseslint from "@typescript-eslint/eslint-plugin" |
| 2 | +import { ESLint, type Linter } from "eslint" |
| 3 | +import { describe, expect, test } from "vite-plus/test" |
| 4 | + |
| 5 | +import { createRecommendedConfig } from "../src/index.js" |
| 6 | + |
| 7 | +const tsRecommended = tseslint.configs["flat/recommended"] as unknown as |
| 8 | + | Linter.Config |
| 9 | + | Linter.Config[] |
| 10 | + |
| 11 | +const config: Linter.Config[] = [ |
| 12 | + ...(Array.isArray(tsRecommended) ? tsRecommended : [tsRecommended]), |
| 13 | + ...createRecommendedConfig({ |
| 14 | + files: ["**/*.js"], |
| 15 | + ignores: [], |
| 16 | + }), |
| 17 | +] |
| 18 | + |
| 19 | +async function lint(code: string, filename = "cartridges/app_sfra/cartridge/controllers/Home.js") { |
| 20 | + const eslint = new ESLint({ |
| 21 | + overrideConfigFile: true, |
| 22 | + overrideConfig: config, |
| 23 | + }) |
| 24 | + |
| 25 | + const results = await eslint.lintText(code, { filePath: filename }) |
| 26 | + return results[0]?.messages || [] |
| 27 | +} |
| 28 | + |
| 29 | +describe("sfcc/no-e4x-syntax", () => { |
| 30 | + test("reports JSX/E4X-like syntax in JS files", async () => { |
| 31 | + const messages = await lint("const x = <a/>; module.exports = x") |
| 32 | + |
| 33 | + expect(messages.some((m) => m.ruleId === "sfcc/no-e4x-syntax")).toBe(true) |
| 34 | + }) |
| 35 | + |
| 36 | + test("allows XML and XMLList constructor-style usage", async () => { |
| 37 | + const messages = await lint(` |
| 38 | + const xmlCtor = XML |
| 39 | + const xmlListCtor = XMLList |
| 40 | + module.exports = { xmlCtor, xmlListCtor } |
| 41 | + `) |
| 42 | + |
| 43 | + expect(messages.some((m) => m.ruleId === "sfcc/no-e4x-syntax")).toBe(false) |
| 44 | + }) |
| 45 | + |
| 46 | + test("shows parsing error for default xml namespace declaration", async () => { |
| 47 | + const messages = await lint('default xml namespace = "urn:test"') |
| 48 | + |
| 49 | + expect(messages.some((m) => m.fatal === true)).toBe(true) |
| 50 | + expect(messages.some((m) => m.ruleId === "sfcc/no-e4x-syntax")).toBe(false) |
| 51 | + }) |
| 52 | + |
| 53 | + test("keeps JSDoc-based typing flow in JS files", async () => { |
| 54 | + const messages = await lint(` |
| 55 | + /** @param {{ productID: string }} body */ |
| 56 | + function buildContext(body) { |
| 57 | + return { productID: body.productID } |
| 58 | + } |
| 59 | + module.exports = buildContext({ productID: "123" }) |
| 60 | + `) |
| 61 | + |
| 62 | + expect(messages.some((m) => m.ruleId === "sfcc/no-e4x-syntax")).toBe(false) |
| 63 | + }) |
| 64 | +}) |
0 commit comments