Skip to content

Commit

Permalink
Merge branch 'base'
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sokolov committed Sep 10, 2024
2 parents f26d617 + fe90fc7 commit 10c95a5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/package-lock.json linguist-generated=true binary
/package-lock.json linguist-generated=true -diff merge
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-double-negative-variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ invalid(
invalid(
"array destructuring double negatives",
noDoubleNegativeVariables,
`let [hideBor, noBaz, quux] = 0 as any`,
`let [<err>hideBor</err>, <err>noBaz</err>, quux] = 0 as any`,
{
count: 2,
},
Expand All @@ -47,7 +47,7 @@ invalid(
invalid(
"deep destructuring double negative",
noDoubleNegativeVariables,
`let { foo: [{ bar: hideQQQ }] } = 0 as any;`,
`let { foo: [{ bar: <err>hideQQQ</err> }] } = 0 as any;`,
);

invalid(
Expand Down
6 changes: 6 additions & 0 deletions src/rules/no-imports-down.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ invalid(
"import {} from './getFoo/bar'",
);

invalid(
"no-imports-down errors only on the path",
noImportsDown,
"import {} from <err>'./getFoo/bar'</err>",
);

valid(
"no-imports-down can import from packages at one level",
noImportsDown,
Expand Down
1 change: 1 addition & 0 deletions src/rules/no-imports-down.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const rule: Rule = {
const message = getMessage(importType);
if (message)
context.report({
loc: node.source?.loc,
message: message.join(" "),
node,
});
Expand Down
38 changes: 32 additions & 6 deletions src/tester/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,41 @@ const ruleTester = new RuleTester();
export function invalid(
name: string,
rule: Rule,
/**
* Can contain <err>...</err> 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<TSESLint.TestCaseError<string>, "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("<err>")) {
const [before, ..._b] = code.split("<err>");
const b = _b.join("<err>");
if (!b.includes("</err>")) throw new Error("No closing </err> tag");
const [inside, ..._after] = b.split("</err>");
const after = _after.join("</err>");
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 || [] },
],
Expand All @@ -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: [
{
Expand Down

0 comments on commit 10c95a5

Please sign in to comment.