Skip to content

Commit 65873ba

Browse files
committed
refactor(linter/plugins): add stubs for all SourceCode methods (#14285)
Add stub implementations for all methods and properties of `SourceCode`. All unimplemented methods/getters just throw an error.
1 parent d8d3d18 commit 65873ba

File tree

5 files changed

+593
-11
lines changed

5 files changed

+593
-11
lines changed

apps/oxlint/src-js/index.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,31 @@ import type { CreateOnceRule, Plugin, Rule } from './plugins/load.ts';
33
import type { BeforeHook, Visitor, VisitorWithHooks } from './plugins/types.ts';
44

55
export type { Context, Diagnostic } from './plugins/context.ts';
6-
export type { Fix, Fixer, FixFn, NodeOrToken, Range } from './plugins/fix.ts';
6+
export type { Fix, Fixer, FixFn, Range } from './plugins/fix.ts';
77
export type { CreateOnceRule, CreateRule, Plugin, Rule } from './plugins/load.ts';
8-
export type { SourceCode } from './plugins/source_code.ts';
9-
export type { AfterHook, BeforeHook, Node, RuleMeta, Visitor, VisitorWithHooks } from './plugins/types.ts';
8+
export type {
9+
Definition,
10+
DefinitionType,
11+
Reference,
12+
Scope,
13+
ScopeManager,
14+
ScopeType,
15+
Variable,
16+
} from './plugins/scope.ts';
17+
export type { CountOptions, FilterFn, RangeOptions, SkipOptions, SourceCode } from './plugins/source_code.ts';
18+
export type {
19+
AfterHook,
20+
BeforeHook,
21+
Comment,
22+
LineColumn,
23+
Location,
24+
Node,
25+
NodeOrToken,
26+
RuleMeta,
27+
Token,
28+
Visitor,
29+
VisitorWithHooks,
30+
} from './plugins/types.ts';
1031

1132
const { defineProperty, getPrototypeOf, hasOwn, setPrototypeOf, create: ObjectCreate } = Object;
1233

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

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

33
import type { Diagnostic, InternalContext } from './context.ts';
4-
import type { Node } from './types.ts';
4+
import type { NodeOrToken } from './types.ts';
55

66
const { prototype: ArrayPrototype, from: ArrayFrom } = Array,
77
{ getPrototypeOf, hasOwn, prototype: ObjectPrototype } = Object,
@@ -20,9 +20,6 @@ export type Fix = { range: Range; text: string };
2020

2121
export type Range = [number, number];
2222

23-
// Currently we only support `Node`s, but will add support for `Token`s later
24-
export type NodeOrToken = Node;
25-
2623
// Fixer, passed as argument to `fix` function passed to `Context#report()`.
2724
//
2825
// Fixer is stateless, so reuse a single object for all fixes.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import type * as ESTree from '@oxc-project/types';
2+
3+
import type { Node } from './types.ts';
4+
5+
type Identifier =
6+
| ESTree.IdentifierName
7+
| ESTree.IdentifierReference
8+
| ESTree.BindingIdentifier
9+
| ESTree.LabelIdentifier
10+
| ESTree.TSThisParameter
11+
| ESTree.TSIndexSignatureName;
12+
13+
export class ScopeManager {
14+
// TODO
15+
}
16+
17+
export interface Scope {
18+
type: ScopeType;
19+
isStrict: boolean;
20+
upper: Scope | null;
21+
childScopes: Scope[];
22+
variableScope: Scope;
23+
block: Node;
24+
variables: Variable[];
25+
set: Map<string, Variable>;
26+
references: Reference[];
27+
through: Reference[];
28+
functionExpressionScope: boolean;
29+
implicit?: {
30+
variables: Variable[];
31+
set: Map<string, Variable>;
32+
};
33+
}
34+
35+
export type ScopeType =
36+
| 'block'
37+
| 'catch'
38+
| 'class'
39+
| 'class-field-initializer'
40+
| 'class-static-block'
41+
| 'for'
42+
| 'function'
43+
| 'function-expression-name'
44+
| 'global'
45+
| 'module'
46+
| 'switch'
47+
| 'with';
48+
49+
export interface Variable {
50+
name: string;
51+
scope: Scope;
52+
identifiers: Identifier[];
53+
references: Reference[];
54+
defs: Definition[];
55+
}
56+
57+
export interface Reference {
58+
identifier: Identifier;
59+
from: Scope;
60+
resolved: Variable | null;
61+
writeExpr: ESTree.Expression | null;
62+
init: boolean;
63+
isWrite(): boolean;
64+
isRead(): boolean;
65+
isReadOnly(): boolean;
66+
isWriteOnly(): boolean;
67+
isReadWrite(): boolean;
68+
}
69+
70+
export interface Definition {
71+
type: DefinitionType;
72+
name: Identifier;
73+
node: Node;
74+
parent: Node | null;
75+
}
76+
77+
export type DefinitionType =
78+
| 'CatchClause'
79+
| 'ClassName'
80+
| 'FunctionName'
81+
| 'ImplicitGlobalVariable'
82+
| 'ImportBinding'
83+
| 'Parameter'
84+
| 'Variable';

0 commit comments

Comments
 (0)