diff --git a/apps/oxlint/src-js/cli.ts b/apps/oxlint/src-js/cli.ts index 0a6e3691ceb09..d14cb7c1920b8 100644 --- a/apps/oxlint/src-js/cli.ts +++ b/apps/oxlint/src-js/cli.ts @@ -1,5 +1,5 @@ import { lint } from './bindings.js'; -import { assertIsNonNull } from './utils/asserts.js'; +import { debugAssertIsNonNull } from './utils/asserts.js'; // Lazy-loaded JS plugin-related functions. // Using `typeof wrapper` here makes TS check that the function signatures of `loadPlugin` and `loadPluginWrapper` @@ -25,7 +25,7 @@ function loadPluginWrapper(path: string, packageName: string | null): Promise(fix); + typeAssertIs(fix); let { range, text } = fix; // These checks follow ESLint, which throws if `range` is missing or invalid diff --git a/apps/oxlint/src-js/plugins/lint.ts b/apps/oxlint/src-js/plugins/lint.ts index 70cd9f15e53b9..8125a5e45e445 100644 --- a/apps/oxlint/src-js/plugins/lint.ts +++ b/apps/oxlint/src-js/plugins/lint.ts @@ -3,7 +3,7 @@ import { registeredRules } from './load.js'; import { diagnostics } from './report.js'; import { setSettingsForFile, resetSettings } from './settings.js'; import { ast, initAst, resetSourceAndAst, setupSourceForFile } from './source_code.js'; -import { assertIs, assertIsNonNull } from '../utils/asserts.js'; +import { typeAssertIs, debugAssertIsNonNull } from '../utils/asserts.js'; import { getErrorMessage } from '../utils/utils.js'; import { addVisitorToCompiled, compiledVisitor, finalizeCompiledVisitor, initCompiledVisitor } from './visitor.js'; @@ -87,7 +87,7 @@ function lintFileImpl( // Rust will only send a `bufferId` alone, if it previously sent a buffer with this same ID buffer = buffers[bufferId]!; } else { - assertIs(buffer); + typeAssertIs(buffer); const { buffer: arrayBuffer, byteOffset } = buffer; buffer.uint32 = new Uint32Array(arrayBuffer, byteOffset); buffer.float64 = new Float64Array(arrayBuffer, byteOffset); @@ -97,7 +97,7 @@ function lintFileImpl( } buffers[bufferId] = buffer; } - assertIs(buffer); + typeAssertIs(buffer); if (DEBUG) { if (typeof filePath !== 'string' || filePath.length === 0) { @@ -139,7 +139,7 @@ function lintFileImpl( let { visitor } = ruleDetails; if (visitor === null) { // Rule defined with `create` method - assertIsNonNull(ruleDetails.rule.create); + debugAssertIsNonNull(ruleDetails.rule.create); visitor = ruleDetails.rule.create(ruleDetails.context); } else { // Rule defined with `createOnce` method diff --git a/apps/oxlint/src-js/plugins/location.ts b/apps/oxlint/src-js/plugins/location.ts index e8f3d508faede..596266687e1c0 100644 --- a/apps/oxlint/src-js/plugins/location.ts +++ b/apps/oxlint/src-js/plugins/location.ts @@ -4,7 +4,7 @@ */ import { initSourceText, sourceText } from './source_code.js'; -import { assertIsNonNull } from '../utils/asserts.js'; +import { debugAssertIsNonNull } from '../utils/asserts.js'; import type { Node } from './types.ts'; @@ -61,7 +61,7 @@ const lineStartOffsets: number[] = [0]; */ export function initLines(): void { if (sourceText === null) initSourceText(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); // This implementation is based on the one in ESLint. // TODO: Investigate if using `String.prototype.matchAll` is faster. @@ -111,7 +111,7 @@ export function getLineColumnFromOffset(offset: number): LineColumn { // Build `lines` and `lineStartOffsets` tables if they haven't been already. // This also decodes `sourceText` if it wasn't already. if (lines.length === 0) initLines(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); if (offset > sourceText.length) { throw new RangeError( @@ -161,7 +161,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number { // Build `lines` and `lineStartOffsets` tables if they haven't been already. // This also decodes `sourceText` if it wasn't already. if (lines.length === 0) initLines(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); const linesCount = lineStartOffsets.length; if (line <= 0 || line > linesCount) { diff --git a/apps/oxlint/src-js/plugins/scope.ts b/apps/oxlint/src-js/plugins/scope.ts index f1b8c1c256fb4..6943a3f679efc 100644 --- a/apps/oxlint/src-js/plugins/scope.ts +++ b/apps/oxlint/src-js/plugins/scope.ts @@ -8,7 +8,7 @@ import { type ScopeManager as TSESLintScopeManager, } from '@typescript-eslint/scope-manager'; import { ast, initAst } from './source_code.js'; -import { assertIs, assertIsNonNull } from '../utils/asserts.js'; +import { typeAssertIs, debugAssertIsNonNull } from '../utils/asserts.js'; import type * as ESTree from '../generated/types.d.ts'; import type { SetNullable } from '../utils/types.ts'; @@ -109,10 +109,10 @@ const analyzeOptions: SetNullable = { */ function initTsScopeManager() { if (ast === null) initAst(); - assertIsNonNull(ast); + debugAssertIsNonNull(ast); analyzeOptions.sourceType = ast.sourceType; - assertIs(analyzeOptions); + typeAssertIs(analyzeOptions); // The effectiveness of this assertion depends on our alignment with ESTree. // It could eventually be removed as we align the remaining corner cases and the typegen. // @ts-expect-error // TODO: Our types don't quite align yet @@ -200,7 +200,7 @@ export function isGlobalReference(node: ESTree.Node): boolean { if (node.type !== 'Identifier') return false; if (tsScopeManager === null) initTsScopeManager(); - assertIsNonNull(tsScopeManager); + debugAssertIsNonNull(tsScopeManager); const { scopes } = tsScopeManager; if (scopes.length === 0) return false; @@ -231,7 +231,7 @@ export function isGlobalReference(node: ESTree.Node): boolean { export function getDeclaredVariables(node: ESTree.Node): Variable[] { // ref: https://github.com/eslint/eslint/blob/e7cda3bdf1bdd664e6033503a3315ad81736b200/lib/languages/js/source-code/source-code.js#L904 if (tsScopeManager === null) initTsScopeManager(); - assertIsNonNull(tsScopeManager); + debugAssertIsNonNull(tsScopeManager); // @ts-expect-error // TODO: Our types don't quite align yet return tsScopeManager.getDeclaredVariables(node); @@ -247,7 +247,7 @@ export function getScope(node: ESTree.Node): Scope { if (!node) throw new TypeError('Missing required argument: `node`'); if (tsScopeManager === null) initTsScopeManager(); - assertIsNonNull(tsScopeManager); + debugAssertIsNonNull(tsScopeManager); const inner = node.type !== 'Program'; diff --git a/apps/oxlint/src-js/plugins/settings.ts b/apps/oxlint/src-js/plugins/settings.ts index 1bd262259d570..296e3db9a6cf3 100644 --- a/apps/oxlint/src-js/plugins/settings.ts +++ b/apps/oxlint/src-js/plugins/settings.ts @@ -3,7 +3,7 @@ */ import { deepFreezeJsonValue } from './json.js'; -import { assertIsNonNull } from '../utils/asserts.js'; +import { debugAssertIsNonNull } from '../utils/asserts.js'; import type { JsonObject } from './json.ts'; @@ -36,7 +36,7 @@ export function setSettingsForFile(settingsJSONInput: string): undefined { * Deserialize settings from JSON. */ export function initSettings(): undefined { - assertIsNonNull(settingsJSON); + debugAssertIsNonNull(settingsJSON); settings = JSON.parse(settingsJSON); // Deep freeze the settings object, to prevent any mutation of the settings from plugins deepFreezeJsonValue(settings); diff --git a/apps/oxlint/src-js/plugins/source_code.ts b/apps/oxlint/src-js/plugins/source_code.ts index b2b5a47c1d8c2..bf829e49e5733 100644 --- a/apps/oxlint/src-js/plugins/source_code.ts +++ b/apps/oxlint/src-js/plugins/source_code.ts @@ -19,7 +19,7 @@ import { resetScopeManager, SCOPE_MANAGER } from './scope.js'; import * as scopeMethods from './scope.js'; import { resetTokens } from './tokens.js'; import * as tokenMethods from './tokens.js'; -import { assertIsNonNull } from '../utils/asserts.js'; +import { debugAssertIsNonNull } from '../utils/asserts.js'; import type { Program } from '../generated/types.d.ts'; import type { Ranged } from './location.ts'; @@ -66,7 +66,7 @@ export function setupSourceForFile( * Decode source text from buffer. */ export function initSourceText(): void { - assertIsNonNull(buffer); + debugAssertIsNonNull(buffer); const { uint32 } = buffer, programPos = uint32[DATA_POINTER_POS_32]; sourceByteLen = uint32[(programPos + SOURCE_LEN_OFFSET) >> 2]; @@ -116,7 +116,7 @@ export const SOURCE_CODE = Object.freeze({ // Get source text. get text(): string { if (sourceText === null) initSourceText(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); return sourceText; }, @@ -128,7 +128,7 @@ export const SOURCE_CODE = Object.freeze({ // Get AST of the file. get ast(): Program { if (ast === null) initAst(); - assertIsNonNull(ast); + debugAssertIsNonNull(ast); return ast; }, @@ -144,7 +144,7 @@ export const SOURCE_CODE = Object.freeze({ // Get parser services for the file. get parserServices(): Record { - assertIsNonNull(parserServices); + debugAssertIsNonNull(parserServices); return parserServices; }, @@ -163,7 +163,7 @@ export const SOURCE_CODE = Object.freeze({ */ getText(node?: Ranged | null, beforeCount?: number | null, afterCount?: number | null): string { if (sourceText === null) initSourceText(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); // ESLint treats all falsy values for `node` as undefined if (!node) return sourceText; diff --git a/apps/oxlint/src-js/plugins/tokens.ts b/apps/oxlint/src-js/plugins/tokens.ts index 02f9aad40a24a..14776534c3608 100644 --- a/apps/oxlint/src-js/plugins/tokens.ts +++ b/apps/oxlint/src-js/plugins/tokens.ts @@ -4,7 +4,7 @@ import { parse } from '@typescript-eslint/typescript-estree'; import { sourceText, initSourceText } from './source_code.js'; -import { assertIsNonNull } from '../utils/asserts.js'; +import { debugAssertIsNonNull } from '../utils/asserts.js'; import type { Comment, Node, NodeOrToken } from './types.ts'; import type { Span } from './location.ts'; @@ -143,7 +143,7 @@ let tokensWithComments: Token[] | null = null; * Initialize TS-ESLint tokens for current file. */ function initTokens() { - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); ({ tokens, comments } = parse(sourceText, { sourceType: 'module', tokens: true, @@ -181,8 +181,8 @@ export function getTokens( afterCount?: number | null, ): Token[] { if (tokens === null) initTokens(); - assertIsNonNull(tokens); - assertIsNonNull(comments); + debugAssertIsNonNull(tokens); + debugAssertIsNonNull(comments); // Maximum number of tokens to return const count = typeof countOptions === 'object' && countOptions !== null ? countOptions.count : null; @@ -568,7 +568,7 @@ export function isSpaceBetween(nodeOrToken1: NodeOrToken, nodeOrToken2: NodeOrTo // Check if there's any whitespace in the gap if (sourceText === null) initSourceText(); - assertIsNonNull(sourceText); + debugAssertIsNonNull(sourceText); return WHITESPACE_REGEXP.test(sourceText.slice(gapStart, gapEnd)); } diff --git a/apps/oxlint/src-js/plugins/visitor.ts b/apps/oxlint/src-js/plugins/visitor.ts index c1b19f82f11ed..9b3f7bafaf08f 100644 --- a/apps/oxlint/src-js/plugins/visitor.ts +++ b/apps/oxlint/src-js/plugins/visitor.ts @@ -76,7 +76,7 @@ import { LEAF_NODE_TYPES_COUNT, NODE_TYPE_IDS_MAP, NODE_TYPES_COUNT } from '../g import { parseSelector, wrapVisitFnWithSelectorMatch } from './selector.js'; import type { CompiledVisitorEntry, EnterExit, Node, VisitFn, Visitor } from './types.ts'; -import { assertIs, assertIsNonNull } from '../utils/asserts.js'; +import { typeAssertIs, debugAssertIsNonNull } from '../utils/asserts.js'; const ObjectKeys = Object.keys, { isArray } = Array; @@ -372,16 +372,16 @@ export function finalizeCompiledVisitor(): boolean { for (let i = mergedEnterVisitorTypeIds.length - 1; i >= 0; i--) { const typeId = mergedEnterVisitorTypeIds[i]; const enterExit = compiledVisitor[typeId] as CompilingNonLeafVisitorEntry; - assertIsNonNull(enterExit); - assertIs(enterExit.enter); + debugAssertIsNonNull(enterExit); + typeAssertIs(enterExit.enter); enterExit.enter = mergeVisitFns(enterExit.enter); } for (let i = mergedExitVisitorTypeIds.length - 1; i >= 0; i--) { const typeId = mergedExitVisitorTypeIds[i]; const enterExit = compiledVisitor[typeId] as CompilingNonLeafVisitorEntry; - assertIsNonNull(enterExit); - assertIs(enterExit.exit); + debugAssertIsNonNull(enterExit); + typeAssertIs(enterExit.exit); enterExit.exit = mergeVisitFns(enterExit.exit); } diff --git a/apps/oxlint/src-js/utils/asserts.ts b/apps/oxlint/src-js/utils/asserts.ts index 7996d8d7078d4..917a0682e2af9 100644 --- a/apps/oxlint/src-js/utils/asserts.ts +++ b/apps/oxlint/src-js/utils/asserts.ts @@ -7,7 +7,7 @@ * @param value - Value */ // oxlint-disable-next-line no-unused-vars -export function assertIs(value: unknown): asserts value is T {} +export function typeAssertIs(value: unknown): asserts value is T {} /** * Assert a value is not `null` or `undefined`. @@ -17,7 +17,7 @@ export function assertIs(value: unknown): asserts value is T {} * * @param value - Value */ -export function assertIsNonNull(value: T | null | undefined): asserts value is T { +export function debugAssertIsNonNull(value: T | null | undefined): asserts value is T { if (!DEBUG) return; if (value === null || value === undefined) {