Skip to content

Commit f46db34

Browse files
committed
Make the indent rules supports TypeScript
1 parent 0f704bd commit f46db34

File tree

65 files changed

+3169
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3169
-224
lines changed

lib/utils/indent-common.js

Lines changed: 90 additions & 218 deletions
Large diffs are not rendered by default.

lib/utils/indent-ts.js

Lines changed: 1136 additions & 0 deletions
Large diffs are not rendered by default.

lib/utils/indent-utils.js

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
'use strict'
2+
3+
/**
4+
* Check whether the given token is an arrow.
5+
* @param {Token|undefined|null} token The token to check.
6+
* @returns {boolean} `true` if the token is an arrow.
7+
*/
8+
function isArrow(token) {
9+
return token != null && token.type === 'Punctuator' && token.value === '=>'
10+
}
11+
12+
/**
13+
* Check whether the given token is a left parenthesis.
14+
* @param {Token|undefined|null} token The token to check.
15+
* @returns {boolean} `true` if the token is a left parenthesis.
16+
*/
17+
function isLeftParen(token) {
18+
return token != null && token.type === 'Punctuator' && token.value === '('
19+
}
20+
21+
/**
22+
* Check whether the given token is a left parenthesis.
23+
* @param {Token|undefined|null} token The token to check.
24+
* @returns {boolean} `false` if the token is a left parenthesis.
25+
*/
26+
function isNotLeftParen(token) {
27+
return token != null && (token.type !== 'Punctuator' || token.value !== '(')
28+
}
29+
30+
/**
31+
* Check whether the given token is a right parenthesis.
32+
* @param {Token|undefined|null} token The token to check.
33+
* @returns {boolean} `true` if the token is a right parenthesis.
34+
*/
35+
function isRightParen(token) {
36+
return token != null && token.type === 'Punctuator' && token.value === ')'
37+
}
38+
39+
/**
40+
* Check whether the given token is a right parenthesis.
41+
* @param {Token|undefined|null} token The token to check.
42+
* @returns {boolean} `false` if the token is a right parenthesis.
43+
*/
44+
function isNotRightParen(token) {
45+
return token != null && (token.type !== 'Punctuator' || token.value !== ')')
46+
}
47+
48+
/**
49+
* Check whether the given token is a left brace.
50+
* @param {Token|undefined|null} token The token to check.
51+
* @returns {boolean} `true` if the token is a left brace.
52+
*/
53+
function isLeftBrace(token) {
54+
return token != null && token.type === 'Punctuator' && token.value === '{'
55+
}
56+
57+
/**
58+
* Check whether the given token is a right brace.
59+
* @param {Token|undefined|null} token The token to check.
60+
* @returns {boolean} `true` if the token is a right brace.
61+
*/
62+
function isRightBrace(token) {
63+
return token != null && token.type === 'Punctuator' && token.value === '}'
64+
}
65+
66+
/**
67+
* Check whether the given token is a left bracket.
68+
* @param {Token|undefined|null} token The token to check.
69+
* @returns {boolean} `true` if the token is a left bracket.
70+
*/
71+
function isLeftBracket(token) {
72+
return token != null && token.type === 'Punctuator' && token.value === '['
73+
}
74+
75+
/**
76+
* Check whether the given token is a right bracket.
77+
* @param {Token|undefined|null} token The token to check.
78+
* @returns {boolean} `true` if the token is a right bracket.
79+
*/
80+
function isRightBracket(token) {
81+
return token != null && token.type === 'Punctuator' && token.value === ']'
82+
}
83+
84+
/**
85+
* Check whether the given token is a semicolon.
86+
* @param {Token|undefined|null} token The token to check.
87+
* @returns {boolean} `true` if the token is a semicolon.
88+
*/
89+
function isSemicolon(token) {
90+
return token != null && token.type === 'Punctuator' && token.value === ';'
91+
}
92+
93+
/**
94+
* Check whether the given token is a colon.
95+
* @param {Token|undefined|null} token The token to check.
96+
* @returns {boolean} `true` if the token is a colon.
97+
*/
98+
function isColon(token) {
99+
return token != null && token.type === 'Punctuator' && token.value === ':'
100+
}
101+
102+
/**
103+
* Check whether the given token is a comma.
104+
* @param {Token|undefined|null} token The token to check.
105+
* @returns {boolean} `true` if the token is a comma.
106+
*/
107+
function isComma(token) {
108+
return token != null && token.type === 'Punctuator' && token.value === ','
109+
}
110+
111+
/**
112+
* Check whether the given token is a wildcard.
113+
* @param {Token|undefined|null} token The token to check.
114+
* @returns {boolean} `true` if the token is a wildcard.
115+
*/
116+
function isWildcard(token) {
117+
return token != null && token.type === 'Punctuator' && token.value === '*'
118+
}
119+
120+
/**
121+
* Check whether the given token is a question.
122+
* @param {Token|undefined|null} token The token to check.
123+
* @returns {boolean} `true` if the token is a question.
124+
*/
125+
function isQuestion(token) {
126+
return token != null && token.type === 'Punctuator' && token.value === '?'
127+
}
128+
129+
/**
130+
* Check whether the given token is an extends keyword.
131+
* @param {Token|undefined|null} token The token to check.
132+
* @returns {boolean} `true` if the token is an extends keywordn.
133+
*/
134+
function isExtendsKeyword(token) {
135+
return token != null && token.type === 'Keyword' && token.value === 'extends'
136+
}
137+
138+
/**
139+
* Check whether the given token is a whitespace.
140+
* @param {Token|undefined|null} token The token to check.
141+
* @returns {boolean} `true` if the token is a whitespace.
142+
*/
143+
function isNotWhitespace(token) {
144+
return token != null && token.type !== 'HTMLWhitespace'
145+
}
146+
147+
/**
148+
* Check whether the given token is a comment.
149+
* @param {Token|undefined|null} token The token to check.
150+
* @returns {boolean} `true` if the token is a comment.
151+
*/
152+
function isComment(token) {
153+
return (
154+
token != null &&
155+
(token.type === 'Block' ||
156+
token.type === 'Line' ||
157+
token.type === 'Shebang' ||
158+
(typeof token.type ===
159+
'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ &&
160+
token.type.endsWith('Comment')))
161+
)
162+
}
163+
164+
/**
165+
* Check whether the given token is a comment.
166+
* @param {Token|undefined|null} token The token to check.
167+
* @returns {boolean} `false` if the token is a comment.
168+
*/
169+
function isNotComment(token) {
170+
return (
171+
token != null &&
172+
token.type !== 'Block' &&
173+
token.type !== 'Line' &&
174+
token.type !== 'Shebang' &&
175+
!(
176+
typeof token.type ===
177+
'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ &&
178+
token.type.endsWith('Comment')
179+
)
180+
)
181+
}
182+
183+
/**
184+
* Check whether the given node is not an empty text node.
185+
* @param {ASTNode} node The node to check.
186+
* @returns {boolean} `false` if the token is empty text node.
187+
*/
188+
function isNotEmptyTextNode(node) {
189+
return !(node.type === 'VText' && node.value.trim() === '')
190+
}
191+
192+
/**
193+
* Check whether the given token is a pipe operator.
194+
* @param {Token|undefined|null} token The token to check.
195+
* @returns {boolean} `true` if the token is a pipe operator.
196+
*/
197+
function isPipeOperator(token) {
198+
return token != null && token.type === 'Punctuator' && token.value === '|'
199+
}
200+
201+
/**
202+
* Get the last element.
203+
* @template T
204+
* @param {T[]} xs The array to get the last element.
205+
* @returns {T | undefined} The last element or undefined.
206+
*/
207+
function last(xs) {
208+
return xs.length === 0 ? undefined : xs[xs.length - 1]
209+
}
210+
211+
module.exports = {
212+
isArrow,
213+
isLeftParen,
214+
isNotLeftParen,
215+
isRightParen,
216+
isNotRightParen,
217+
isLeftBrace,
218+
isRightBrace,
219+
isLeftBracket,
220+
isRightBracket,
221+
isSemicolon,
222+
isColon,
223+
isComma,
224+
isWildcard,
225+
isQuestion,
226+
isExtendsKeyword,
227+
isNotWhitespace,
228+
isComment,
229+
isNotComment,
230+
isNotEmptyTextNode,
231+
isPipeOperator,
232+
last
233+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
abstract class A {
4+
abstract a;
5+
abstract public b;
6+
public abstract c;
7+
abstract protected d;
8+
protected abstract e;
9+
abstract private f;
10+
private abstract g;
11+
}
12+
13+
abstract class B {
14+
readonly abstract a;
15+
abstract readonly b;
16+
abstract readonly public c;
17+
public abstract readonly d;
18+
public readonly abstract e;
19+
public static readonly a;
20+
public abstract readonly a;
21+
}
22+
</script>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
abstract class A {
4+
a
5+
=
6+
1
7+
;
8+
abstract public b
9+
=
10+
's'
11+
;
12+
protected abstract c
13+
=
14+
i
15+
;
16+
}
17+
</script>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
abstract class A {
4+
abstract a ();
5+
abstract public b(
6+
);
7+
public abstract c
8+
();
9+
abstract protected d(
10+
p1,
11+
p2
12+
);
13+
protected abstract e(
14+
p1
15+
,p2
16+
)
17+
abstract private f(
18+
p1
19+
,
20+
p2
21+
)
22+
;
23+
private abstract g();
24+
private abstract async h(
25+
p1
26+
,
27+
p2
28+
);
29+
}
30+
</script>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
var foo =
4+
bar as
5+
boolean
6+
</script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
interface Foo {
4+
(
5+
arg1
6+
,
7+
arg2
8+
)
9+
:
10+
string
11+
;
12+
}
13+
</script>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
type Foo = {
4+
(
5+
arg1
6+
,
7+
arg2
8+
)
9+
:
10+
string
11+
;
12+
}
13+
</script>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class
4+
Foo
5+
<
6+
T
7+
>
8+
{
9+
prop:string
10+
}
11+
</script>

0 commit comments

Comments
 (0)