diff --git a/.gitattributes b/.gitattributes index 81a5a79..d5d60e0 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -/package-lock.json linguist-generated=true binary +/package-lock.json linguist-generated=true -diff merge diff --git a/package.json b/package.json index 68c9896..e1e359e 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "scripts": { "build": "rm -rf dist && NODE_ENV=production tsc", - "format": "prettier --write .", - "format-verify": "prettier --check .", - "lint": "eslint .", + "format": "prettier --write . --log-level=warn", + "format-verify": "prettier --check . --log-level=warn", + "lint": "eslint . --fix", "prepublishOnly": "npm run verify && npm run build && publint", "start-building": "rm -rf dist && NODE_ENV=production tsc --watch", "test": "ava", diff --git a/src/rules/no-double-negative-variables.test.ts b/src/rules/no-double-negative-variables.test.ts index 79623c3..1fdbac5 100644 --- a/src/rules/no-double-negative-variables.test.ts +++ b/src/rules/no-double-negative-variables.test.ts @@ -38,7 +38,7 @@ invalid( invalid( "array destructuring double negatives", noDoubleNegativeVariables, - `let [hideBor, noBaz, quux] = 0 as any`, + `let [hideBor, noBaz, quux] = 0 as any`, { count: 2, }, @@ -47,7 +47,7 @@ invalid( invalid( "deep destructuring double negative", noDoubleNegativeVariables, - `let { foo: [{ bar: hideQQQ }] } = 0 as any;`, + `let { foo: [{ bar: hideQQQ }] } = 0 as any;`, ); invalid( diff --git a/src/rules/no-imports-down.test.ts b/src/rules/no-imports-down.test.ts index 6c8f9bb..54b8ef2 100644 --- a/src/rules/no-imports-down.test.ts +++ b/src/rules/no-imports-down.test.ts @@ -14,6 +14,12 @@ invalid( "import {} from './getFoo/bar'", ); +invalid( + "no-imports-down errors only on the path", + noImportsDown, + "import {} from './getFoo/bar'", +); + valid( "no-imports-down can import from packages at one level", noImportsDown, diff --git a/src/rules/no-imports-down.ts b/src/rules/no-imports-down.ts index 043b8f1..5eb3e2c 100644 --- a/src/rules/no-imports-down.ts +++ b/src/rules/no-imports-down.ts @@ -47,6 +47,7 @@ const rule: Rule = { const message = getMessage(importType); if (message) context.report({ + loc: node.source?.loc, message: message.join(" "), node, }); diff --git a/src/tester/index.ts b/src/tester/index.ts index da2bee3..3c32846 100644 --- a/src/tester/index.ts +++ b/src/tester/index.ts @@ -24,15 +24,41 @@ const ruleTester = new RuleTester(); export function invalid( name: string, rule: Rule, + /** + * Can contain ... tags to indicate where the error should be + */ code: string, options: { count?: number; ruleOptions?: unknown[] } = {}, ) { test(name, (t) => { - const blankError = {}; - const errors = Array.from(new Array(options.count || 1)).map( - () => blankError, - ); - ruleTester.run(name, rule as any, { + const errors: Omit, "messageId">[] = + Array.from(new Array(options.count || 1)).map(() => ({})); + function countPos(s: string) { + const lines = s.split("\n"); + const last = lines[lines.length - 1]; + return { col: last.length + 1, line: lines.length }; + } + let errNum = 0; + while (code.includes("")) { + const [before, ..._b] = code.split(""); + const b = _b.join(""); + if (!b.includes("")) throw new Error("No closing tag"); + const [inside, ..._after] = b.split(""); + const after = _after.join(""); + const start = countPos(before); + const end = countPos(before + inside); + errors[errNum] = { + ...(errors[errNum] ?? {}), + column: start.col, + endColumn: end.col, + endLine: end.line, + line: start.line, + }; + code = before + inside + after; + errNum += 1; + } + + ruleTester.run("dummy rule name for test " + name, rule as any, { invalid: [ { code, errors: errors as any, options: options.ruleOptions || [] }, ], @@ -49,7 +75,7 @@ export function valid( options: { ruleOptions?: unknown[] } = {}, ) { test(name, (t) => { - ruleTester.run("dummy-name", rule as any, { + ruleTester.run("dummy rule name for test " + name, rule as any, { invalid: [], valid: [ {