-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
utils.ts
113 lines (104 loc) · 2.81 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { AST } from 'eslint';
import { Comment, SourceLocation } from 'estree';
import {
GraphQLNamedType,
GraphQLOutputType,
isListType,
isNonNullType,
Lexer,
Location,
Source,
Token,
TokenKind,
} from 'graphql';
import { valueFromASTUntyped } from 'graphql/utilities/valueFromASTUntyped.js';
export const valueFromNode = (...args: Parameters<typeof valueFromASTUntyped>): any => {
return valueFromASTUntyped(...args);
};
export function getBaseType(type: GraphQLOutputType): GraphQLNamedType {
if (isNonNullType(type) || isListType(type)) {
return getBaseType(type.ofType);
}
return type;
}
export function convertToken<T extends TokenKind | 'Block' | 'Line'>(
token: Token,
type: T,
): Omit<AST.Token, 'type'> & { type: T } {
const { line, column, end, start, value } = token;
return {
type,
value,
/*
* ESLint has 0-based column number
* https://eslint.org/docs/developer-guide/working-with-rules#contextreport
*/
loc: {
start: {
line,
column: column - 1,
},
end: {
line,
column: column - 1 + (end - start),
},
},
range: [start, end],
};
}
export function extractTokens(filePath: string, code: string): AST.Token[] {
const source = new Source(code, filePath);
const lexer = new Lexer(source);
const tokens: AST.Token[] = [];
let token = lexer.advance();
while (token && token.kind !== TokenKind.EOF) {
const result = convertToken(token, token.kind) as AST.Token;
tokens.push(result);
token = lexer.advance();
}
return tokens;
}
export function extractComments(loc?: Location): Comment[] {
if (!loc) {
return [];
}
const comments: Comment[] = [];
let token: Token | null = loc.startToken;
while (token) {
if (token.kind === TokenKind.COMMENT) {
const comment = convertToken(
token,
// `eslint-disable` directive works only with `Block` type comment
token.value.trimStart().startsWith('eslint') ? 'Block' : 'Line',
);
comments.push(comment);
}
token = token.next;
}
return comments;
}
export function convertLocation(location: Location): SourceLocation {
const { startToken, endToken, source, start, end } = location;
/*
* ESLint has 0-based column number
* https://eslint.org/docs/developer-guide/working-with-rules#contextreport
*/
const loc = {
start: {
/*
* Kind.Document has startToken: { line: 0, column: 0 }, we set line as 1 and column as 0
*/
line: startToken.line === 0 ? 1 : startToken.line,
column: startToken.column === 0 ? 0 : startToken.column - 1,
},
end: {
line: endToken.line,
column: endToken.column - 1,
},
source: source.body,
};
if (loc.start.column === loc.end.column) {
loc.end.column += end - start;
}
return loc;
}