Skip to content

Commit c4abee4

Browse files
committed
refactor(linter/plugins): move location-related types into location.ts (#15634)
Pure refactor. Move TS type defs for location-related types into `location.ts`. `types.ts` is too crowded. Also add JSDoc comments to these type defs.
1 parent 0d52a5e commit c4abee4

File tree

8 files changed

+50
-39
lines changed

8 files changed

+50
-39
lines changed

apps/oxlint/src-js/generated/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Auto-generated code, DO NOT EDIT DIRECTLY!
22
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/typescript.rs`.
33

4-
import { Span, Comment } from '../plugins/types.ts';
4+
import { Span } from '../plugins/location.ts';
5+
import { Comment } from '../plugins/types.ts';
56
export { Span, Comment };
67

78
export interface Program extends Span {

apps/oxlint/src-js/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,13 @@ export type {
3333
RuleReplacedByInfo,
3434
RuleReplacedByExternalSpecifier,
3535
} from './plugins/rule_meta.ts';
36+
export type { LineColumn, Location, Range, Ranged, Span } from './plugins/location.ts';
3637
export type {
3738
AfterHook,
3839
BeforeHook,
3940
Comment,
40-
LineColumn,
41-
Location,
4241
Node,
4342
NodeOrToken,
44-
Range,
45-
Ranged,
46-
Span,
4743
Token,
4844
Visitor,
4945
VisitorWithHooks,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { settings, initSettings } from './settings.js';
3434
import type { Fix, FixFn } from './fix.ts';
3535
import type { RuleDetails } from './load.ts';
3636
import type { SourceCode } from './source_code.ts';
37-
import type { Location, Ranged } from './types.ts';
37+
import type { Location, Ranged } from './location.ts';
3838
import type { ModuleKind } from '../generated/types.d.ts';
3939

4040
const { hasOwn, keys: ObjectKeys, freeze, assign: ObjectAssign, create: ObjectCreate } = Object;

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

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

33
import type { Diagnostic } from './context.ts';
44
import type { RuleDetails } from './load.ts';
5-
import type { Range, Ranged } from './types.ts';
5+
import type { Range, Ranged } from './location.ts';
66

77
const { prototype: ArrayPrototype, from: ArrayFrom } = Array,
88
{ getPrototypeOf, hasOwn, prototype: ObjectPrototype } = Object,

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,48 @@
55

66
import { initSourceText, sourceText } from './source_code.js';
77

8-
import type { LineColumn, Location, Node } from './types.ts';
8+
import type { Node } from './types.ts';
99

1010
const { defineProperty } = Object;
1111

12+
/**
13+
* Range of source offsets.
14+
*/
15+
export type Range = [number, number];
16+
17+
/**
18+
* Interface for any type which has `range` field.
19+
*/
20+
export interface Ranged {
21+
range: Range;
22+
}
23+
24+
/**
25+
* Interface for any type which has location properties.
26+
*/
27+
export interface Span extends Ranged {
28+
start: number;
29+
end: number;
30+
loc: Location;
31+
}
32+
33+
/**
34+
* Source code location.
35+
*/
36+
export interface Location {
37+
start: LineColumn;
38+
end: LineColumn;
39+
}
40+
41+
/**
42+
* Line number + column number pair.
43+
* `line` is 1-indexed, `column` is 0-indexed.
44+
*/
45+
export interface LineColumn {
46+
line: number;
47+
column: number;
48+
}
49+
1250
// Pattern for splitting source text into lines
1351
const LINE_BREAK_PATTERN = /\r\n|[\r\n\u2028\u2029]/gu;
1452

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import * as scopeMethods from './scope.js';
2020
import * as tokenMethods from './tokens.js';
2121

2222
import type { Program } from '../generated/types.d.ts';
23-
import type { BufferWithArrays, Node, Ranged } from './types.ts';
23+
import type { Ranged } from './location.ts';
24+
import type { BufferWithArrays, Node } from './types.ts';
2425
import type { ScopeManager } from './scope.ts';
2526

2627
const { max } = Math;

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export interface Visitor {
66
}
77
*/
88

9+
import type { Span } from './location.ts';
10+
911
import type { VisitorObject as Visitor } from '../generated/visitor.d.ts';
1012
export type { Visitor };
1113

@@ -25,34 +27,6 @@ export interface VisitorWithHooks extends Visitor {
2527
// Visit function for a specific AST node type.
2628
export type VisitFn = (node: Node) => void;
2729

28-
// Range of source offsets.
29-
export type Range = [number, number];
30-
31-
// Interface for any type which has `range` field
32-
export interface Ranged {
33-
range: Range;
34-
}
35-
36-
// Interface for any type which has location properties.
37-
export interface Span extends Ranged {
38-
start: number;
39-
end: number;
40-
loc: Location;
41-
}
42-
43-
// Source code location.
44-
export interface Location {
45-
start: LineColumn;
46-
end: LineColumn;
47-
}
48-
49-
// Line number + column number pair.
50-
// `line` is 1-indexed, `column` is 0-indexed.
51-
export interface LineColumn {
52-
line: number;
53-
column: number;
54-
}
55-
5630
// AST node type.
5731
export interface Node extends Span {}
5832

tasks/ast_tools/src/generators/typescript.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,8 @@ fn amend_oxlint_types(code: &str) -> String {
491491

492492
#[rustfmt::skip]
493493
code.insert_str(0, "
494-
import { Span, Comment } from '../plugins/types.ts';
494+
import { Span } from '../plugins/location.ts';
495+
import { Comment } from '../plugins/types.ts';
495496
export { Span, Comment };
496497
497498
");

0 commit comments

Comments
 (0)