Skip to content

Commit e57fd55

Browse files
committed
Make the indent rules supports TypeScript
1 parent a969878 commit e57fd55

File tree

64 files changed

+2691
-512
lines changed

Some content is hidden

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

64 files changed

+2691
-512
lines changed

lib/utils/indent-common.js

Lines changed: 365 additions & 510 deletions
Large diffs are not rendered by default.

lib/utils/indent-ts.js

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

lib/utils/indent-utils.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict'
2+
3+
/**
4+
* Check whether the given token is a wildcard.
5+
* @param {Token|undefined|null} token The token to check.
6+
* @returns {boolean} `true` if the token is a wildcard.
7+
*/
8+
function isWildcard(token) {
9+
return token != null && token.type === 'Punctuator' && token.value === '*'
10+
}
11+
12+
/**
13+
* Check whether the given token is a question.
14+
* @param {Token|undefined|null} token The token to check.
15+
* @returns {boolean} `true` if the token is a question.
16+
*/
17+
function isQuestion(token) {
18+
return token != null && token.type === 'Punctuator' && token.value === '?'
19+
}
20+
21+
/**
22+
* Check whether the given token is an extends keyword.
23+
* @param {Token|undefined|null} token The token to check.
24+
* @returns {boolean} `true` if the token is an extends keywordn.
25+
*/
26+
function isExtendsKeyword(token) {
27+
return token != null && token.type === 'Keyword' && token.value === 'extends'
28+
}
29+
30+
/**
31+
* Check whether the given token is a whitespace.
32+
* @param {Token|undefined|null} token The token to check.
33+
* @returns {boolean} `true` if the token is a whitespace.
34+
*/
35+
function isNotWhitespace(token) {
36+
return token != null && token.type !== 'HTMLWhitespace'
37+
}
38+
39+
/**
40+
* Check whether the given token is a comment.
41+
* @param {Token|undefined|null} token The token to check.
42+
* @returns {boolean} `true` if the token is a comment.
43+
*/
44+
function isComment(token) {
45+
return (
46+
token != null &&
47+
(token.type === 'Block' ||
48+
token.type === 'Line' ||
49+
token.type === 'Shebang' ||
50+
(typeof token.type ===
51+
'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ &&
52+
token.type.endsWith('Comment')))
53+
)
54+
}
55+
56+
/**
57+
* Check whether the given token is a comment.
58+
* @param {Token|undefined|null} token The token to check.
59+
* @returns {boolean} `false` if the token is a comment.
60+
*/
61+
function isNotComment(token) {
62+
return (
63+
token != null &&
64+
token.type !== 'Block' &&
65+
token.type !== 'Line' &&
66+
token.type !== 'Shebang' &&
67+
!(
68+
typeof token.type ===
69+
'string' /* Although acorn supports new tokens, espree may not yet support new tokens.*/ &&
70+
token.type.endsWith('Comment')
71+
)
72+
)
73+
}
74+
75+
/**
76+
* Check whether the given node is not an empty text node.
77+
* @param {ASTNode} node The node to check.
78+
* @returns {boolean} `false` if the token is empty text node.
79+
*/
80+
function isNotEmptyTextNode(node) {
81+
return !(node.type === 'VText' && node.value.trim() === '')
82+
}
83+
84+
/**
85+
* Check whether the given token is a pipe operator.
86+
* @param {Token|undefined|null} token The token to check.
87+
* @returns {boolean} `true` if the token is a pipe operator.
88+
*/
89+
function isPipeOperator(token) {
90+
return token != null && token.type === 'Punctuator' && token.value === '|'
91+
}
92+
93+
/**
94+
* Get the last element.
95+
* @template T
96+
* @param {T[]} xs The array to get the last element.
97+
* @returns {T | undefined} The last element or undefined.
98+
*/
99+
function last(xs) {
100+
return xs.length === 0 ? undefined : xs[xs.length - 1]
101+
}
102+
103+
module.exports = {
104+
isWildcard,
105+
isQuestion,
106+
isExtendsKeyword,
107+
isNotWhitespace,
108+
isComment,
109+
isNotComment,
110+
isNotEmptyTextNode,
111+
isPipeOperator,
112+
last
113+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@types/natural-compare": "^1.4.0",
6464
"@types/node": "^13.13.5",
6565
"@types/semver": "^7.2.0",
66-
"@typescript-eslint/parser": "^3.0.2",
66+
"@typescript-eslint/parser": "^4.28.0",
6767
"@vuepress/plugin-pwa": "^1.4.1",
6868
"babel-eslint": "^10.1.0",
6969
"env-cmd": "^10.1.0",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--{"options":[2, {"ignores": ["ArrayExpression"]}]}-->
2+
<script>
3+
var a =
4+
[
5+
{
6+
a: 42
7+
}
8+
]
9+
var x = {
10+
a:
11+
[],
12+
b:
13+
1,
14+
c:
15+
{},
16+
d:
17+
2
18+
}
19+
</script>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--{"options":[2, {"ignores": ["ObjectExpression"]}]}-->
2+
<script>
3+
var a =
4+
[
5+
{
6+
a: 42
7+
}
8+
]
9+
var x = {
10+
a:
11+
[],
12+
b:
13+
1,
14+
c:
15+
{},
16+
d:
17+
2
18+
}
19+
</script>
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>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class
4+
Foo
5+
<
6+
T
7+
>
8+
extends
9+
Bar
10+
<
11+
T
12+
>
13+
{
14+
prop:string
15+
}
16+
</script>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class
4+
Foo
5+
<
6+
T
7+
>
8+
extends
9+
Bar
10+
<
11+
T
12+
>
13+
implements
14+
Baz1
15+
<
16+
T
17+
>
18+
,
19+
Baz2
20+
<
21+
T
22+
>
23+
{
24+
prop:string
25+
}
26+
</script>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class
4+
Foo
5+
<
6+
T
7+
>
8+
implements
9+
Baz1
10+
<
11+
T
12+
>
13+
,
14+
Baz2
15+
<
16+
T
17+
>
18+
extends
19+
Bar
20+
<
21+
T
22+
>
23+
{
24+
prop:string
25+
}
26+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class
4+
Foo
5+
implements
6+
Baz1
7+
,
8+
Baz2
9+
extends
10+
Bar
11+
{
12+
prop:string
13+
}
14+
</script>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
2+
<script lang="ts">
3+
class A {
4+
a;
5+
public b;
6+
protected c;
7+
private d;
8+
}
9+
10+
class B {
11+
readonly a;
12+
readonly public b;
13+
protected readonly c;
14+
}
15+
</script>

0 commit comments

Comments
 (0)