Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion @coven/iterables/async/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const take = (
yield item;
count += 1n;
} else {
break;
return;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion @coven/iterables/async/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const zip = <ItemFirst>(
.next();

if (done) {
break;
return;
}

yield [itemFirst as ItemFirst, value] as const;
Expand Down
2 changes: 1 addition & 1 deletion @coven/iterables/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const zip = <ItemFirst>(
const { done = false, value } = iteratorSecond.next();

if (done) {
break;
return;
}

yield [itemFirst, value as ItemSecond];
Expand Down
21 changes: 21 additions & 0 deletions lint/mod.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { maxLines } from "./rules/max-lines.ts";
import { noBreak } from "./rules/no-break.ts";
import { noClass } from "./rules/no-class.ts";
import { noContinue } from "./rules/no-continue.ts";
import { noDefaultExport } from "./rules/no-default-export.ts";
import { noDoWhile } from "./rules/no-do-while.ts";
import { noEnum } from "./rules/no-enum.ts";
import { noForIn } from "./rules/no-for-in.ts";
import { noFunction } from "./rules/no-function.ts";
import { noNull } from "./rules/no-null.ts";
import { noSwitch } from "./rules/no-switch.ts";
import { noThis } from "./rules/no-this.ts";
import { noThrow } from "./rules/no-throw.ts";
import { noTry } from "./rules/no-try.ts";
import { noWhile } from "./rules/no-while.ts";

// deno-lint-ignore coven/no-default-export
export default {
name: "coven",
rules: {
"max-lines": maxLines,
"no-break": noBreak,
"no-class": noClass,
"no-continue": noContinue,
"no-default-export": noDefaultExport,
"no-do-while": noDoWhile,
"no-enum": noEnum,
"no-for-in": noForIn,
"no-function": noFunction,
"no-null": noNull,
"no-switch": noSwitch,
"no-this": noThis,
"no-throw": noThrow,
"no-try": noTry,
"no-while": noWhile,
},
} satisfies Deno.lint.Plugin;
26 changes: 26 additions & 0 deletions lint/no.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Utility type to figure out what the Node type is based on the Visitor.
*/
type FindContext<
Visitor extends keyof Deno.lint.LintVisitor,
Node extends Deno.lint.Node = Deno.lint.Node,
> = Node extends { type: Visitor } ? Node : never;

/**
* Shorthand for common "Avoid this, do that." lintinrg rules.
*/
export const no = <Visitor extends keyof Deno.lint.LintVisitor>(
visitor: Visitor,
message: string,
condition?: (
data: { context: Deno.lint.RuleContext; node: FindContext<Visitor> },
) => boolean,
): Deno.lint.Rule => ({
create: (context): Deno.lint.LintVisitor => ({
[visitor]: (node: FindContext<Visitor>): void => {
(condition?.({ context, node }) ?? true)
? context.report({ message, node })
: undefined;
},
}),
});
9 changes: 9 additions & 0 deletions lint/rules/max-lines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { no } from "../no.ts";

const LINE_LIMIT = 300;

export const maxLines: Deno.lint.Rule = no(
"Program",
`Avoid large files. Split this into smaller files (${LINE_LIMIT} lines or less).`,
({ context }) => context.sourceCode.text.split("\n").length > LINE_LIMIT,
);
6 changes: 6 additions & 0 deletions lint/rules/no-break.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noBreak: Deno.lint.Rule = no(
"BreakStatement",
"Avoid using `break`. Use explicit control flow.",
);
16 changes: 6 additions & 10 deletions lint/rules/no-class.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const noClass: Deno.lint.Rule = {
create: (context): Deno.lint.LintVisitor => ({
ClassExpression: (node) => {
context.report({
node,
message: "Avoid using `class`. Use a function instead.",
});
},
}),
};
import { no } from "../no.ts";

export const noClass: Deno.lint.Rule = no(
"ClassExpression",
"Avoid using `class`. Use a function instead.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-continue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noContinue: Deno.lint.Rule = no(
"ContinueStatement",
"Avoid using `continue`. Use explicit control flow.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-default-export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noDefaultExport: Deno.lint.Rule = no(
"ExportDefaultDeclaration",
"Avoid using default exports. Use named exports instead.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-do-while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noDoWhile: Deno.lint.Rule = no(
"DoWhileStatement",
"Avoid using `do..while`. Use utils from `@coven/iterables` or any modern looping method.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noEnum: Deno.lint.Rule = no(
"TSEnumDeclaration",
"Avoid using `enum`. Use union types or object literals instead.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-for-in.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noForIn: Deno.lint.Rule = no(
"ForInStatement",
"Avoid using `for..in`. Use utils from `@coven/iterables` or any modern looping method.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noFunction: Deno.lint.Rule = no(
"FunctionDeclaration",
"Avoid using `function`. Use an arrow expression instead.",
);
18 changes: 7 additions & 11 deletions lint/rules/no-null.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { no } from "../no.ts";

/**
* Checks for `Object.create`.
*/
Expand All @@ -11,14 +13,8 @@ const isObjectCreate = (
&& node.callee.property.type === "Identifier"
&& node.callee.property.name === "create";

export const noNull: Deno.lint.Rule = {
create: (context): Deno.lint.LintVisitor => ({
Literal: (node): void =>
(node.raw === "null" && !isObjectCreate(node.parent))
? context.report({
node,
message: "Use `undefined` instead of `null`.",
})
: undefined,
}),
};
export const noNull: Deno.lint.Rule = no(
"Literal",
"Avoid using `null`. Use `undefined` instead.",
({ node }) => node.raw === "null" && !isObjectCreate(node.parent),
);
6 changes: 6 additions & 0 deletions lint/rules/no-switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noSwitch: Deno.lint.Rule = no(
"SwitchStatement",
"Avoid using `switch`. Use a dictionary instead.",
);
16 changes: 6 additions & 10 deletions lint/rules/no-this.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const noThis: Deno.lint.Rule = {
create: (context): Deno.lint.LintVisitor => ({
ThisExpression: (node) => {
context.report({
node,
message: "Avoid using `this`. Use predictable values instead.",
});
},
}),
};
import { no } from "../no.ts";

export const noThis: Deno.lint.Rule = no(
"ThisExpression",
"Avoid using `this`. Use explicit values instead.",
);
15 changes: 6 additions & 9 deletions lint/rules/no-throw.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export const noThrow: Deno.lint.Rule = {
create: (context): Deno.lint.LintVisitor => ({
ThrowStatement: (node): void =>
context.report({
node,
message: "Avoid using `throw`. Return the error instead.",
}),
}),
};
import { no } from "../no.ts";

export const noThrow: Deno.lint.Rule = no(
"ThrowStatement",
"Avoid using `throw`. Return the error instead.",
);
16 changes: 6 additions & 10 deletions lint/rules/no-try.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export const noTry: Deno.lint.Rule = {
create: (context): Deno.lint.LintVisitor => ({
TryStatement: (node): void =>
context.report({
node,
message:
"Avoid `try`/`catch`. Use `attempt` from `@coven/parsers`, or a Promise.",
}),
}),
};
import { no } from "../no.ts";

export const noTry: Deno.lint.Rule = no(
"TryStatement",
"Avoid using `try`/`catch`. Use `attempt` from `@coven/parsers`, or a Promise.",
);
6 changes: 6 additions & 0 deletions lint/rules/no-while.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { no } from "../no.ts";

export const noWhile: Deno.lint.Rule = no(
"WhileStatement",
"Avoid using `while`. Use utils from `@coven/iterables` or any modern looping method.",
);