forked from prettier/angular-estree-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
152 lines (124 loc) · 3.28 KB
/
utils.ts
File metadata and controls
152 lines (124 loc) · 3.28 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { LocationInformation, RawNGSpan } from './types.js';
function stripSurroundingSpaces(
{ start: startIndex, end: endIndex }: RawNGSpan,
text: string,
) {
let start = startIndex;
let end = endIndex;
while (end !== start && /\s/.test(text[end - 1])) {
end--;
}
while (start !== end && /\s/.test(text[start])) {
start++;
}
return { start, end };
}
function expandSurroundingSpaces(
{ start: startIndex, end: endIndex }: RawNGSpan,
text: string,
) {
let start = startIndex;
let end = endIndex;
while (end !== text.length && /\s/.test(text[end])) {
end++;
}
while (start !== 0 && /\s/.test(text[start - 1])) {
start--;
}
return { start, end };
}
function expandSurroundingParens(span: RawNGSpan, text: string) {
return text[span.start - 1] === '(' && text[span.end] === ')'
? { start: span.start - 1, end: span.end + 1 }
: span;
}
export function fitSpans(
span: RawNGSpan,
text: string,
hasParentParens: boolean,
): { outerSpan: RawNGSpan; innerSpan: RawNGSpan; hasParens: boolean } {
let parensCount = 0;
const outerSpan = { start: span.start, end: span.end };
while (true) {
const spacesExpandedSpan = expandSurroundingSpaces(outerSpan, text);
const parensExpandedSpan = expandSurroundingParens(
spacesExpandedSpan,
text,
);
if (
spacesExpandedSpan.start === parensExpandedSpan.start &&
spacesExpandedSpan.end === parensExpandedSpan.end
) {
break;
}
outerSpan.start = parensExpandedSpan.start;
outerSpan.end = parensExpandedSpan.end;
parensCount++;
}
return {
hasParens: (hasParentParens ? parensCount - 1 : parensCount) !== 0,
outerSpan: stripSurroundingSpaces(
hasParentParens
? { start: outerSpan.start + 1, end: outerSpan.end - 1 }
: outerSpan,
text,
),
innerSpan: stripSurroundingSpaces(span, text),
};
}
function getCharacterSearchTestFunction(pattern: RegExp | string) {
if (typeof pattern === 'string') {
return (character: string) => character === pattern;
}
return (character: string) => pattern.test(character);
}
export function getCharacterLastIndex(
text: string,
pattern: RegExp | string,
fromIndex: number,
) {
const test = getCharacterSearchTestFunction(pattern);
for (let index = fromIndex; index >= 0; index--) {
const character = text[index];
if (test(character)) {
return index;
}
}
/* c8 ignore next 4 */
throw new Error(
`Cannot find front char ${pattern} from index ${fromIndex} in ${JSON.stringify(
text,
)}`,
);
}
export function getCharacterIndex(
text: string,
pattern: RegExp | string,
fromIndex: number,
) {
const test = getCharacterSearchTestFunction(pattern);
for (let index = fromIndex; index < text.length; index++) {
const character = text[index];
if (test(character)) {
return index;
}
}
throw new Error(
`Cannot find character ${pattern} from index ${fromIndex} in ${JSON.stringify(
text,
)}`,
);
}
export function lowercaseFirst(str: string) {
return str.slice(0, 1).toLowerCase() + str.slice(1);
}
export function sourceSpanToLocationInformation(
span: RawNGSpan,
): LocationInformation {
const { start, end } = span;
return {
start,
end,
range: [start, end],
};
}