From f87bd67127782aa94fb054402176747a9792020b Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 31 Jul 2024 15:31:53 -0700 Subject: [PATCH] Add a couple additional lints to ensure consistent style --- .eslintrc | 5 +++++ lib/src/compiler-path.ts | 4 ++-- lib/src/compiler.test.ts | 2 +- lib/src/exception.ts | 2 +- lib/src/legacy/value/wrap.ts | 4 ++-- lib/src/packet-transformer.test.ts | 2 +- lib/src/request-tracker.ts | 6 +++--- lib/src/value/calculations.ts | 9 ++++++--- lib/src/value/color.ts | 2 +- lib/src/value/utils.ts | 2 +- test/utils.ts | 2 +- tool/get-deprecations.ts | 2 +- tool/prepare-optional-release.ts | 2 +- tool/prepare-release.ts | 2 +- 14 files changed, 27 insertions(+), 19 deletions(-) diff --git a/.eslintrc b/.eslintrc index f135f8dd..3b5b9123 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,6 +1,11 @@ { "extends": "./node_modules/gts/", "rules": { + "@typescript-eslint/explicit-function-return-type": [ + "error", + {"allowExpressions": true} + ], + "func-style": ["error", "declaration"], "prefer-const": ["error", {"destructuring": "all"}], // It would be nice to sort import declaration order as well, but that's not // autofixable and it's not worth the effort of handling manually. diff --git a/lib/src/compiler-path.ts b/lib/src/compiler-path.ts index 60675e5b..5e7d9bf5 100644 --- a/lib/src/compiler-path.ts +++ b/lib/src/compiler-path.ts @@ -10,9 +10,9 @@ import {isErrnoException} from './utils'; * Detect if the current running node binary is linked with musl libc by * checking if the binary contains a string like "/.../ld-musl-$ARCH.so" */ -const isLinuxMusl = function () { +function isLinuxMusl(): boolean { return fs.readFileSync(process.execPath).includes('/ld-musl-'); -}; +} /** The full command for the embedded compiler executable. */ export const compilerCommand = (() => { diff --git a/lib/src/compiler.test.ts b/lib/src/compiler.test.ts index 58fbc61f..df72f27e 100644 --- a/lib/src/compiler.test.ts +++ b/lib/src/compiler.test.ts @@ -9,7 +9,7 @@ import * as compilerModule from './compiler/utils'; import {Compiler, initCompiler} from './compiler/sync'; const createDispatcher = jest.spyOn(compilerModule, 'createDispatcher'); -function getIdHistory() { +function getIdHistory(): number[] { return createDispatcher.mock.calls.map(([id]) => id); } diff --git a/lib/src/exception.ts b/lib/src/exception.ts index 48eb6dfd..3e00f40e 100644 --- a/lib/src/exception.ts +++ b/lib/src/exception.ts @@ -19,7 +19,7 @@ export class Exception extends Error implements SassException { this.span = deprotofySourceSpan(failure.span!); } - toString() { + toString(): string { return this.message; } } diff --git a/lib/src/legacy/value/wrap.ts b/lib/src/legacy/value/wrap.ts index 178da2ec..7da2e1cd 100644 --- a/lib/src/legacy/value/wrap.ts +++ b/lib/src/legacy/value/wrap.ts @@ -42,7 +42,7 @@ export function wrapFunction( } else { return args => new Promise((resolve, reject) => { - const done = (result: unknown) => { + function done(result: unknown): void { try { if (result instanceof Error) { reject(result); @@ -52,7 +52,7 @@ export function wrapFunction( } catch (error: unknown) { reject(error); } - }; + } // The cast here is necesary to work around microsoft/TypeScript#33815. const syncResult = (callback as (...args: unknown[]) => unknown).apply( diff --git a/lib/src/packet-transformer.test.ts b/lib/src/packet-transformer.test.ts index 603801f0..78297883 100644 --- a/lib/src/packet-transformer.test.ts +++ b/lib/src/packet-transformer.test.ts @@ -55,7 +55,7 @@ describe('packet transformer', () => { describe('decode', () => { let rawBuffers$: Subject; - function expectDecoding(expected: Buffer[], done: () => void) { + function expectDecoding(expected: Buffer[], done: () => void): void { const actual: Buffer[] = []; packets.outboundProtobufs$.subscribe({ next: protobuf => actual.push(protobuf), diff --git a/lib/src/request-tracker.ts b/lib/src/request-tracker.ts index f26db4ca..200f51c1 100644 --- a/lib/src/request-tracker.ts +++ b/lib/src/request-tracker.ts @@ -16,7 +16,7 @@ export class RequestTracker { > = []; /** The next available request ID. */ - get nextId() { + get nextId(): number { for (let i = 0; i < this.requests.length; i++) { if (this.requests[i] === undefined || this.requests[i] === null) { return i; @@ -32,7 +32,7 @@ export class RequestTracker { add( id: number, expectedResponseType: InboundResponseType | OutboundResponseType - ) { + ): void { if (id < 0) { throw Error(`Invalid request ID ${id}.`); } else if (this.requests[id]) { @@ -47,7 +47,7 @@ export class RequestTracker { * Resolves a pending request with matching ID `id` and expected response type * `type`. Throws an error if the Protocol Error is violated. */ - resolve(id: number, type: InboundResponseType | OutboundResponseType) { + resolve(id: number, type: InboundResponseType | OutboundResponseType): void { if (this.requests[id] === undefined || this.requests[id] === null) { throw Error(`Response ID ${id} does not match any pending requests.`); } else if (this.requests[id] !== type) { diff --git a/lib/src/value/calculations.ts b/lib/src/value/calculations.ts index ca3eebe4..7f4c2a4e 100644 --- a/lib/src/value/calculations.ts +++ b/lib/src/value/calculations.ts @@ -23,9 +23,12 @@ function assertCalculationValue(value: CalculationValue): void { } } -const isValidClampArg = (value: CalculationValue): boolean => - value instanceof CalculationInterpolation || - (value instanceof SassString && !value.hasQuotes); +function isValidClampArg(value: CalculationValue): boolean { + return ( + value instanceof CalculationInterpolation || + (value instanceof SassString && !value.hasQuotes) + ); +} /* A SassScript calculation */ export class SassCalculation extends Value { diff --git a/lib/src/value/color.ts b/lib/src/value/color.ts index b2aaad1f..a47c4cd5 100644 --- a/lib/src/value/color.ts +++ b/lib/src/value/color.ts @@ -350,7 +350,7 @@ function hwbToRgb( hue: number, scaledWhiteness: number, scaledBlackness: number -) { +): number { const factor = 1 - scaledWhiteness - scaledBlackness; const channel = hueToRgb(0, 1, hue) * factor + scaledWhiteness; return fuzzyRound(channel * 255); diff --git a/lib/src/value/utils.ts b/lib/src/value/utils.ts index 18584bfb..d40ed98c 100644 --- a/lib/src/value/utils.ts +++ b/lib/src/value/utils.ts @@ -116,7 +116,7 @@ export function fuzzyAssertInRange( } /** Returns `dividend % modulus`, but always in the range `[0, modulus)`. */ -export function positiveMod(dividend: number, modulus: number) { +export function positiveMod(dividend: number, modulus: number): number { const result = dividend % modulus; return result < 0 ? result + modulus : result; } diff --git a/test/utils.ts b/test/utils.ts index f8733917..c071d481 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -48,7 +48,7 @@ export function expectEqualIgnoringWhitespace( string1: string, string2: string ): void { - function strip(str: string) { + function strip(str: string): string { return str.replace(/\s+/g, ''); } expect(strip(string1)).toBe(strip(string2)); diff --git a/tool/get-deprecations.ts b/tool/get-deprecations.ts index 73618b4d..9571335f 100644 --- a/tool/get-deprecations.ts +++ b/tool/get-deprecations.ts @@ -34,7 +34,7 @@ function toVersionCode(version: string | undefined): string { * Generates the list of deprecations based on the YAML file in the language * repo. */ -export async function getDeprecations(outDirectory: string) { +export async function getDeprecations(outDirectory: string): Promise { const yamlText = fs.readFileSync(yamlFile, 'utf8'); const deprecations = parse(yamlText) as YamlData; diff --git a/tool/prepare-optional-release.ts b/tool/prepare-optional-release.ts index c0e606c3..c2dc58de 100644 --- a/tool/prepare-optional-release.ts +++ b/tool/prepare-optional-release.ts @@ -123,7 +123,7 @@ async function patchLauncherScript( path: string, dartPlatform: DartPlatform, dartArch: DartArch -) { +): Promise { if (dartPlatform !== 'linux') return; const scriptPath = p.join(path, 'dart-sass', 'sass'); diff --git a/tool/prepare-release.ts b/tool/prepare-release.ts index efdc95a5..4065ffcf 100644 --- a/tool/prepare-release.ts +++ b/tool/prepare-release.ts @@ -35,7 +35,7 @@ void (async () => { // Quick sanity checks to make sure the release we are preparing is a suitable // candidate for release. -async function sanityCheckBeforeRelease() { +async function sanityCheckBeforeRelease(): Promise { console.log('Running sanity checks before releasing.'); const releaseVersion = pkg.version;