Skip to content

Commit ae13704

Browse files
committed
refactor(linter/plugins): rename assertion functions (#15968)
Rename assertion functions to clarify that they're not "real" assertions which we can rely on for validating input, and are only for compile-time testing or debug assertions. `typeAssertIs` is not a great name, but I couldn't come up with anything better. `debugAssertIs` would be not be accurate, as it'd imply that in debug builds in performs an actual assertion, which it doesn't. In any case, we should replace `typeAssertIs` with specialized assertion functions which do actually perform runtime assertions in debug builds (e.g. `typeAssertIs<Fix>()` -> `debugAssertIsFix()`).
1 parent 06f4454 commit ae13704

File tree

13 files changed

+53
-53
lines changed

13 files changed

+53
-53
lines changed

apps/oxlint/src-js/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { lint } from './bindings.js';
2-
import { assertIsNonNull } from './utils/asserts.js';
2+
import { debugAssertIsNonNull } from './utils/asserts.js';
33

44
// Lazy-loaded JS plugin-related functions.
55
// 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<st
2525
return loadPlugin(path, packageName);
2626
});
2727
}
28-
assertIsNonNull(loadPlugin);
28+
debugAssertIsNonNull(loadPlugin);
2929
return loadPlugin(path, packageName);
3030
}
3131

@@ -50,7 +50,7 @@ function lintFileWrapper(
5050
): string {
5151
// `lintFileWrapper` is never called without `loadPluginWrapper` being called first,
5252
// so `lintFile` must be defined here
53-
assertIsNonNull(lintFile);
53+
debugAssertIsNonNull(lintFile);
5454
return lintFile(filePath, bufferId, buffer, ruleIds, settingsJSON);
5555
}
5656

apps/oxlint/src-js/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertIsNonNull } from './utils/asserts.js';
1+
import { debugAssertIsNonNull } from './utils/asserts.js';
22

33
import type { Context, FileContext, LanguageOptions } from './plugins/context.ts';
44
import type { CreateOnceRule, Plugin, Rule } from './plugins/load.ts';
@@ -129,7 +129,7 @@ export function defineRule(rule: Rule): Rule {
129129
if (context === null) {
130130
({ context, visitor, beforeHook } = createContextAndVisitor(rule));
131131
}
132-
assertIsNonNull(visitor);
132+
debugAssertIsNonNull(visitor);
133133

134134
// Copy properties from ESLint's context object to `context`.
135135
// ESLint's context object is an object of form `{ id, options, report }`, with all other properties

apps/oxlint/src-js/plugins/comments.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { ast, initAst, sourceText } from './source_code.js';
6-
import { assertIsNonNull } from '../utils/asserts.js';
6+
import { debugAssertIsNonNull } from '../utils/asserts.js';
77

88
import type { Comment, Node, NodeOrToken } from './types.ts';
99

@@ -16,7 +16,7 @@ const WHITESPACE_ONLY_REGEXP = /^\s*$/;
1616
*/
1717
export function getAllComments(): Comment[] {
1818
if (ast === null) initAst();
19-
assertIsNonNull(ast);
19+
debugAssertIsNonNull(ast);
2020

2121
// `comments` property is a getter. Comments are deserialized lazily.
2222
return ast.comments;
@@ -41,8 +41,8 @@ export function getAllComments(): Comment[] {
4141
*/
4242
export function getCommentsBefore(nodeOrToken: NodeOrToken): Comment[] {
4343
if (ast === null) initAst();
44-
assertIsNonNull(ast);
45-
assertIsNonNull(sourceText);
44+
debugAssertIsNonNull(ast);
45+
debugAssertIsNonNull(sourceText);
4646

4747
const { comments } = ast,
4848
commentsLength = comments.length;
@@ -98,8 +98,8 @@ export function getCommentsBefore(nodeOrToken: NodeOrToken): Comment[] {
9898
*/
9999
export function getCommentsAfter(nodeOrToken: NodeOrToken): Comment[] {
100100
if (ast === null) initAst();
101-
assertIsNonNull(ast);
102-
assertIsNonNull(sourceText);
101+
debugAssertIsNonNull(ast);
102+
debugAssertIsNonNull(sourceText);
103103

104104
const { comments } = ast,
105105
commentsLength = comments.length;
@@ -142,7 +142,7 @@ export function getCommentsAfter(nodeOrToken: NodeOrToken): Comment[] {
142142
*/
143143
export function getCommentsInside(node: Node): Comment[] {
144144
if (ast === null) initAst();
145-
assertIsNonNull(ast);
145+
debugAssertIsNonNull(ast);
146146

147147
const { comments } = ast,
148148
commentsLength = comments.length;
@@ -186,7 +186,7 @@ export function getCommentsInside(node: Node): Comment[] {
186186
*/
187187
export function commentsExistBetween(nodeOrToken1: NodeOrToken, nodeOrToken2: NodeOrToken): boolean {
188188
if (ast === null) initAst();
189-
assertIsNonNull(ast);
189+
debugAssertIsNonNull(ast);
190190

191191
// Find the first comment after `nodeOrToken1` ends.
192192
const { comments } = ast,

apps/oxlint/src-js/plugins/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import { ast, initAst, SOURCE_CODE } from './source_code.js';
3030
import { report } from './report.js';
3131
import { settings, initSettings } from './settings.js';
32-
import { assertIsNonNull } from '../utils/asserts.js';
32+
import { debugAssertIsNonNull } from '../utils/asserts.js';
3333

3434
import type { Options, RuleDetails } from './load.ts';
3535
import type { Diagnostic } from './report.ts';
@@ -79,7 +79,7 @@ const PARSER_OPTIONS = freeze({
7979
// in case it's used in `create` to return an empty visitor if wrong type.
8080
// TODO: ESLint also has `commonjs` option.
8181
if (ast === null) initAst();
82-
assertIsNonNull(ast);
82+
debugAssertIsNonNull(ast);
8383

8484
return ast.sourceType;
8585
},
@@ -95,7 +95,7 @@ const LANGUAGE_OPTIONS = freeze({
9595
// in case it's used in `create` to return an empty visitor if wrong type.
9696
// TODO: ESLint also has `commonjs` option.
9797
if (ast === null) initAst();
98-
assertIsNonNull(ast);
98+
debugAssertIsNonNull(ast);
9999

100100
return ast.sourceType;
101101
},
@@ -257,7 +257,7 @@ const FILE_CONTEXT = freeze({
257257
if (filePath === null) throw new Error('Cannot access `context.settings` in `createOnce`');
258258

259259
if (settings === null) initSettings();
260-
assertIsNonNull(settings);
260+
debugAssertIsNonNull(settings);
261261

262262
return settings;
263263
},

apps/oxlint/src-js/plugins/fix.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertIs } from '../utils/asserts.js';
1+
import { typeAssertIs } from '../utils/asserts.js';
22

33
import type { RuleDetails } from './load.ts';
44
import type { Range, Ranged } from './location.ts';
@@ -159,7 +159,7 @@ export function getFixes(diagnostic: Diagnostic, ruleDetails: RuleDetails): Fix[
159159
* @returns `Fix` object
160160
*/
161161
function validateAndConformFix(fix: unknown): Fix {
162-
assertIs<Fix>(fix);
162+
typeAssertIs<Fix>(fix);
163163
let { range, text } = fix;
164164

165165
// These checks follow ESLint, which throws if `range` is missing or invalid

apps/oxlint/src-js/plugins/lint.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { registeredRules } from './load.js';
33
import { diagnostics } from './report.js';
44
import { setSettingsForFile, resetSettings } from './settings.js';
55
import { ast, initAst, resetSourceAndAst, setupSourceForFile } from './source_code.js';
6-
import { assertIs, assertIsNonNull } from '../utils/asserts.js';
6+
import { typeAssertIs, debugAssertIsNonNull } from '../utils/asserts.js';
77
import { getErrorMessage } from '../utils/utils.js';
88
import { addVisitorToCompiled, compiledVisitor, finalizeCompiledVisitor, initCompiledVisitor } from './visitor.js';
99

@@ -87,7 +87,7 @@ function lintFileImpl(
8787
// Rust will only send a `bufferId` alone, if it previously sent a buffer with this same ID
8888
buffer = buffers[bufferId]!;
8989
} else {
90-
assertIs<BufferWithArrays>(buffer);
90+
typeAssertIs<BufferWithArrays>(buffer);
9191
const { buffer: arrayBuffer, byteOffset } = buffer;
9292
buffer.uint32 = new Uint32Array(arrayBuffer, byteOffset);
9393
buffer.float64 = new Float64Array(arrayBuffer, byteOffset);
@@ -97,7 +97,7 @@ function lintFileImpl(
9797
}
9898
buffers[bufferId] = buffer;
9999
}
100-
assertIs<BufferWithArrays>(buffer);
100+
typeAssertIs<BufferWithArrays>(buffer);
101101

102102
if (DEBUG) {
103103
if (typeof filePath !== 'string' || filePath.length === 0) {
@@ -139,7 +139,7 @@ function lintFileImpl(
139139
let { visitor } = ruleDetails;
140140
if (visitor === null) {
141141
// Rule defined with `create` method
142-
assertIsNonNull(ruleDetails.rule.create);
142+
debugAssertIsNonNull(ruleDetails.rule.create);
143143
visitor = ruleDetails.rule.create(ruleDetails.context);
144144
} else {
145145
// Rule defined with `createOnce` method

apps/oxlint/src-js/plugins/location.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { initSourceText, sourceText } from './source_code.js';
7-
import { assertIsNonNull } from '../utils/asserts.js';
7+
import { debugAssertIsNonNull } from '../utils/asserts.js';
88

99
import type { Node } from './types.ts';
1010

@@ -61,7 +61,7 @@ const lineStartOffsets: number[] = [0];
6161
*/
6262
export function initLines(): void {
6363
if (sourceText === null) initSourceText();
64-
assertIsNonNull(sourceText);
64+
debugAssertIsNonNull(sourceText);
6565

6666
// This implementation is based on the one in ESLint.
6767
// TODO: Investigate if using `String.prototype.matchAll` is faster.
@@ -111,7 +111,7 @@ export function getLineColumnFromOffset(offset: number): LineColumn {
111111
// Build `lines` and `lineStartOffsets` tables if they haven't been already.
112112
// This also decodes `sourceText` if it wasn't already.
113113
if (lines.length === 0) initLines();
114-
assertIsNonNull(sourceText);
114+
debugAssertIsNonNull(sourceText);
115115

116116
if (offset > sourceText.length) {
117117
throw new RangeError(
@@ -161,7 +161,7 @@ export function getOffsetFromLineColumn(loc: LineColumn): number {
161161
// Build `lines` and `lineStartOffsets` tables if they haven't been already.
162162
// This also decodes `sourceText` if it wasn't already.
163163
if (lines.length === 0) initLines();
164-
assertIsNonNull(sourceText);
164+
debugAssertIsNonNull(sourceText);
165165

166166
const linesCount = lineStartOffsets.length;
167167
if (line <= 0 || line > linesCount) {

apps/oxlint/src-js/plugins/scope.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
type ScopeManager as TSESLintScopeManager,
99
} from '@typescript-eslint/scope-manager';
1010
import { ast, initAst } from './source_code.js';
11-
import { assertIs, assertIsNonNull } from '../utils/asserts.js';
11+
import { typeAssertIs, debugAssertIsNonNull } from '../utils/asserts.js';
1212

1313
import type * as ESTree from '../generated/types.d.ts';
1414
import type { SetNullable } from '../utils/types.ts';
@@ -109,10 +109,10 @@ const analyzeOptions: SetNullable<AnalyzeOptions, 'sourceType'> = {
109109
*/
110110
function initTsScopeManager() {
111111
if (ast === null) initAst();
112-
assertIsNonNull(ast);
112+
debugAssertIsNonNull(ast);
113113

114114
analyzeOptions.sourceType = ast.sourceType;
115-
assertIs<AnalyzeOptions>(analyzeOptions);
115+
typeAssertIs<AnalyzeOptions>(analyzeOptions);
116116
// The effectiveness of this assertion depends on our alignment with ESTree.
117117
// It could eventually be removed as we align the remaining corner cases and the typegen.
118118
// @ts-expect-error // TODO: Our types don't quite align yet
@@ -200,7 +200,7 @@ export function isGlobalReference(node: ESTree.Node): boolean {
200200
if (node.type !== 'Identifier') return false;
201201

202202
if (tsScopeManager === null) initTsScopeManager();
203-
assertIsNonNull(tsScopeManager);
203+
debugAssertIsNonNull(tsScopeManager);
204204

205205
const { scopes } = tsScopeManager;
206206
if (scopes.length === 0) return false;
@@ -231,7 +231,7 @@ export function isGlobalReference(node: ESTree.Node): boolean {
231231
export function getDeclaredVariables(node: ESTree.Node): Variable[] {
232232
// ref: https://github.com/eslint/eslint/blob/e7cda3bdf1bdd664e6033503a3315ad81736b200/lib/languages/js/source-code/source-code.js#L904
233233
if (tsScopeManager === null) initTsScopeManager();
234-
assertIsNonNull(tsScopeManager);
234+
debugAssertIsNonNull(tsScopeManager);
235235

236236
// @ts-expect-error // TODO: Our types don't quite align yet
237237
return tsScopeManager.getDeclaredVariables(node);
@@ -247,7 +247,7 @@ export function getScope(node: ESTree.Node): Scope {
247247
if (!node) throw new TypeError('Missing required argument: `node`');
248248

249249
if (tsScopeManager === null) initTsScopeManager();
250-
assertIsNonNull(tsScopeManager);
250+
debugAssertIsNonNull(tsScopeManager);
251251

252252
const inner = node.type !== 'Program';
253253

apps/oxlint/src-js/plugins/settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import { deepFreezeJsonValue } from './json.js';
6-
import { assertIsNonNull } from '../utils/asserts.js';
6+
import { debugAssertIsNonNull } from '../utils/asserts.js';
77

88
import type { JsonObject } from './json.ts';
99

@@ -36,7 +36,7 @@ export function setSettingsForFile(settingsJSONInput: string): undefined {
3636
* Deserialize settings from JSON.
3737
*/
3838
export function initSettings(): undefined {
39-
assertIsNonNull(settingsJSON);
39+
debugAssertIsNonNull(settingsJSON);
4040
settings = JSON.parse(settingsJSON);
4141
// Deep freeze the settings object, to prevent any mutation of the settings from plugins
4242
deepFreezeJsonValue(settings);

apps/oxlint/src-js/plugins/source_code.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { resetScopeManager, SCOPE_MANAGER } from './scope.js';
1919
import * as scopeMethods from './scope.js';
2020
import { resetTokens } from './tokens.js';
2121
import * as tokenMethods from './tokens.js';
22-
import { assertIsNonNull } from '../utils/asserts.js';
22+
import { debugAssertIsNonNull } from '../utils/asserts.js';
2323

2424
import type { Program } from '../generated/types.d.ts';
2525
import type { Ranged } from './location.ts';
@@ -66,7 +66,7 @@ export function setupSourceForFile(
6666
* Decode source text from buffer.
6767
*/
6868
export function initSourceText(): void {
69-
assertIsNonNull(buffer);
69+
debugAssertIsNonNull(buffer);
7070
const { uint32 } = buffer,
7171
programPos = uint32[DATA_POINTER_POS_32];
7272
sourceByteLen = uint32[(programPos + SOURCE_LEN_OFFSET) >> 2];
@@ -116,7 +116,7 @@ export const SOURCE_CODE = Object.freeze({
116116
// Get source text.
117117
get text(): string {
118118
if (sourceText === null) initSourceText();
119-
assertIsNonNull(sourceText);
119+
debugAssertIsNonNull(sourceText);
120120
return sourceText;
121121
},
122122

@@ -128,7 +128,7 @@ export const SOURCE_CODE = Object.freeze({
128128
// Get AST of the file.
129129
get ast(): Program {
130130
if (ast === null) initAst();
131-
assertIsNonNull(ast);
131+
debugAssertIsNonNull(ast);
132132
return ast;
133133
},
134134

@@ -144,7 +144,7 @@ export const SOURCE_CODE = Object.freeze({
144144

145145
// Get parser services for the file.
146146
get parserServices(): Record<string, unknown> {
147-
assertIsNonNull(parserServices);
147+
debugAssertIsNonNull(parserServices);
148148
return parserServices;
149149
},
150150

@@ -163,7 +163,7 @@ export const SOURCE_CODE = Object.freeze({
163163
*/
164164
getText(node?: Ranged | null, beforeCount?: number | null, afterCount?: number | null): string {
165165
if (sourceText === null) initSourceText();
166-
assertIsNonNull(sourceText);
166+
debugAssertIsNonNull(sourceText);
167167

168168
// ESLint treats all falsy values for `node` as undefined
169169
if (!node) return sourceText;

0 commit comments

Comments
 (0)