diff --git a/_tools/lint_plugin.ts b/_tools/lint_plugin.ts index 1fceb2f455a3..81c80efb868f 100644 --- a/_tools/lint_plugin.ts +++ b/_tools/lint_plugin.ts @@ -56,6 +56,30 @@ export default { }; }, }, + // https://docs.deno.com/runtime/contributing/style_guide/#top-level-functions-should-not-use-arrow-syntax + "no-top-level-arrow-syntax": { + create(context) { + return { + ArrowFunctionExpression(node) { + if ( + node.parent.type === "VariableDeclarator" && + node.parent.parent.type === "VariableDeclaration" && + (node.parent.parent.parent.type === "Program" || + node.parent.parent.parent.type === "ExportNamedDeclaration") + ) { + // TODO(iuioiua): add fix + context.report({ + node, + range: node.range, + message: "Top-level functions should not use arrow syntax", + hint: + "Use function declaration instead of arrow function. E.g. Use `function foo() {}` instead of `const foo = () => {}`.", + }); + } + }, + }; + }, + }, // https://docs.deno.com/runtime/contributing/style_guide/#do-not-depend-on-external-code. "no-external-code": { create(context) { diff --git a/_tools/lint_plugin_test.ts b/_tools/lint_plugin_test.ts index 86c22cce0baa..2749b14e001b 100644 --- a/_tools/lint_plugin_test.ts +++ b/_tools/lint_plugin_test.ts @@ -56,6 +56,47 @@ class MyClass { ); }); +Deno.test("deno-style-guide/no-top-level-arrow-syntax", { + ignore: !Deno.version.deno.startsWith("2"), +}, () => { + // Bad + assertLintPluginDiagnostics( + ` +const foo = () => "bar"; +export const bar = () => "foo"; + `, + [ + { + id: "deno-style-guide/no-top-level-arrow-syntax", + range: [13, 24], + fix: [], + message: "Top-level functions should not use arrow syntax", + hint: + "Use function declaration instead of arrow function. E.g. Use `function foo() {}` instead of `const foo = () => {}`.", + }, + { + id: "deno-style-guide/no-top-level-arrow-syntax", + range: [45, 56], + fix: [], + message: "Top-level functions should not use arrow syntax", + hint: + "Use function declaration instead of arrow function. E.g. Use `function foo() {}` instead of `const foo = () => {}`.", + }, + ], + ); + + // Good + assertLintPluginDiagnostics( + ` +function foo() { + const bar = () => "baz"; + + return "bar"; +}`, + [], + ); +}); + Deno.test("deno-style-guide/no-external-code", { ignore: !Deno.version.deno.startsWith("2"), }, () => { diff --git a/assert/equals_test.ts b/assert/equals_test.ts index db33a14091c4..2f9bd29b9908 100644 --- a/assert/equals_test.ts +++ b/assert/equals_test.ts @@ -9,22 +9,26 @@ import { yellow, } from "@std/internal/styles"; -const createHeader = (): string[] => [ - "", - "", - ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ - green( - bold("Expected"), - ) - }`, - "", - "", -]; +function createHeader(): string[] { + return [ + "", + "", + ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ + green( + bold("Expected"), + ) + }`, + "", + "", + ]; +} -const added: (s: string) => string = (s: string): string => - green(bold(stripAnsiCode(s))); -const removed: (s: string) => string = (s: string): string => - red(bold(stripAnsiCode(s))); +function added(s: string): string { + return green(bold(stripAnsiCode(s))); +} +function removed(s: string): string { + return red(bold(stripAnsiCode(s))); +} Deno.test({ name: "assertEquals() matches when values are equal", diff --git a/expect/_assert_equals_test.ts b/expect/_assert_equals_test.ts index dd6d57a6abd8..ef2a0df8618c 100644 --- a/expect/_assert_equals_test.ts +++ b/expect/_assert_equals_test.ts @@ -6,22 +6,26 @@ import { AssertionError, assertThrows } from "@std/assert"; import { bold, gray, green, red, stripAnsiCode, yellow } from "@std/fmt/colors"; import { assertEquals } from "./_assert_equals.ts"; -const createHeader = (): string[] => [ - "", - "", - ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ - green( - bold("Expected"), - ) - }`, - "", - "", -]; +function createHeader(): string[] { + return [ + "", + "", + ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ + green( + bold("Expected"), + ) + }`, + "", + "", + ]; +} -const added: (s: string) => string = (s: string): string => - green(bold(stripAnsiCode(s))); -const removed: (s: string) => string = (s: string): string => - red(bold(stripAnsiCode(s))); +function added(s: string): string { + return green(bold(stripAnsiCode(s))); +} +function removed(s: string): string { + return red(bold(stripAnsiCode(s))); +} Deno.test({ name: "assertEquals() matches when values are equal", diff --git a/expect/_extend_test.ts b/expect/_extend_test.ts index 964813afe379..98352b437fba 100644 --- a/expect/_extend_test.ts +++ b/expect/_extend_test.ts @@ -28,7 +28,7 @@ class Book { } } -const areAuthorsEqual: Tester = (a: unknown, b: unknown) => { +function areAuthorsEqual(a: unknown, b: unknown) { const isAAuthor = a instanceof Author; const isBAuthor = b instanceof Author; @@ -39,7 +39,7 @@ const areAuthorsEqual: Tester = (a: unknown, b: unknown) => { } else { return false; } -}; +} const areBooksEqual: Tester = function ( this: MatcherContext, diff --git a/expect/_to_equal_test.ts b/expect/_to_equal_test.ts index 6dfe51b9177d..2ada39047e34 100644 --- a/expect/_to_equal_test.ts +++ b/expect/_to_equal_test.ts @@ -4,22 +4,26 @@ import { bold, gray, green, red, stripAnsiCode, yellow } from "@std/fmt/colors"; import { AssertionError, assertThrows } from "@std/assert"; import { expect } from "./expect.ts"; -const createHeader = (): string[] => [ - "", - "", - ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ - green( - bold("Expected"), - ) - }`, - "", - "", -]; +function createHeader(): string[] { + return [ + "", + "", + ` ${gray(bold("[Diff]"))} ${red(bold("Actual"))} / ${ + green( + bold("Expected"), + ) + }`, + "", + "", + ]; +} -const added: (s: string) => string = (s: string): string => - green(bold(stripAnsiCode(s))); -const removed: (s: string) => string = (s: string): string => - red(bold(stripAnsiCode(s))); +function added(s: string): string { + return green(bold(stripAnsiCode(s))); +} +function removed(s: string): string { + return red(bold(stripAnsiCode(s))); +} Deno.test({ name: "expect().toEqual() matches when values are equal", diff --git a/fs/_map_error.ts b/fs/_map_error.ts index 99f84b895154..223f5a7ca0f9 100644 --- a/fs/_map_error.ts +++ b/fs/_map_error.ts @@ -6,10 +6,12 @@ type Class = new (...params: unknown[]) => T; type ClassOrT = T extends Class ? U : T; -const mapper = (Ctor: typeof errors[keyof typeof errors]) => (err: Error) => - Object.assign(new Ctor(err.message), { - stack: err.stack, - }) as unknown as ClassOrT; +function mapper(Ctor: typeof errors[keyof typeof errors]) { + return (err: Error) => + Object.assign(new Ctor(err.message), { + stack: err.stack, + }) as unknown as ClassOrT; +} const map: Record> = { EEXIST: mapper(errors.AlreadyExists), @@ -17,9 +19,9 @@ const map: Record> = { EBADF: mapper(errors.BadResource), }; -const isNodeErr = (e: unknown): e is Error & { code: string } => { +function isNodeErr(e: unknown): e is Error & { code: string } { return e instanceof Error && "code" in e; -}; +} export function mapError(e: E) { if (!isNodeErr(e)) return e; diff --git a/ini/stringify.ts b/ini/stringify.ts index 832509ba1c3c..a89cd938e317 100644 --- a/ini/stringify.ts +++ b/ini/stringify.ts @@ -36,11 +36,11 @@ function isPlainObject(object: unknown): object is object { return Object.prototype.toString.call(object) === "[object Object]"; } -const sort = ([_a, valA]: [string, unknown], [_b, valB]: [string, unknown]) => { +function sort([_a, valA]: [string, unknown], [_b, valB]: [string, unknown]) { if (isPlainObject(valA)) return 1; if (isPlainObject(valB)) return -1; return 0; -}; +} function defaultReplacer(_key: string, value: unknown, _section?: string) { return `${value}`; diff --git a/log/base_handler.ts b/log/base_handler.ts index 92ea897343d7..62f07ae80915 100644 --- a/log/base_handler.ts +++ b/log/base_handler.ts @@ -17,8 +17,10 @@ export type { LevelName, LogLevel, LogRecord }; * @returns A string representation of the log record. */ export type FormatterFunction = (logRecord: LogRecord) => string; -const DEFAULT_FORMATTER: FormatterFunction = ({ levelName, msg }) => - `${levelName} ${msg}`; +// deno-lint-ignore deno-style-guide/naming-convention +function DEFAULT_FORMATTER(logRecord: LogRecord): string { + return `${logRecord.levelName} ${logRecord.msg}`; +} /** Options for {@linkcode BaseHandler}. */ export interface BaseHandlerOptions { diff --git a/log/formatters_test.ts b/log/formatters_test.ts index 00e7f306ae84..bd1ee0ed9516 100644 --- a/log/formatters_test.ts +++ b/log/formatters_test.ts @@ -4,13 +4,14 @@ import { FakeTime } from "@std/testing/time"; import { jsonFormatter } from "./formatters.ts"; import { LogRecord } from "./logger.ts"; -const log = (msg: string, args: unknown[] = []) => - new LogRecord({ +function log(msg: string, args: unknown[] = []) { + return new LogRecord({ msg, args, level: 20, loggerName: "user-logger", }); +} Deno.test("jsonFormatter() handles messages without arguments", function () { using _time = new FakeTime(1);