Skip to content

Commit a969878

Browse files
authored
Supports ES2022 (Class Fields) (#1481)
* Supports ES2022 (Class Fileds) * Update
1 parent 384dae0 commit a969878

13 files changed

+255
-24
lines changed

.vscode/settings.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@
99
"vue"
1010
],
1111
"typescript.tsdk": "node_modules/typescript/lib",
12-
"vetur.validation.script": false
12+
"vetur.validation.script": false,
13+
"[typescript]": {
14+
"editor.formatOnSave": true,
15+
"editor.defaultFormatter": "esbenp.prettier-vscode"
16+
},
1317
}

lib/rules/require-slots-as-functions.js

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ module.exports = {
113113
if (!utils.isThis(object.object, context)) {
114114
return
115115
}
116+
if (node.property.type === 'PrivateIdentifier') {
117+
// Unreachable
118+
return
119+
}
116120
verify(node, node.property)
117121
}
118122
})

lib/utils/indent-common.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ const KNOWN_NODES = new Set([
6060
'NewExpression',
6161
'ObjectExpression',
6262
'ObjectPattern',
63+
'PrivateIdentifier',
6364
'Program',
6465
'Property',
66+
'PropertyDefinition',
6567
'RestElement',
6668
'ReturnStatement',
6769
'SequenceExpression',
@@ -667,7 +669,7 @@ module.exports.defineVisitor = function create(
667669
/**
668670
* Collect prefix tokens of the given property.
669671
* The prefix includes `async`, `get`, `set`, `static`, and `*`.
670-
* @param {Property|MethodDefinition} node The property node to collect prefix tokens.
672+
* @param {Property|MethodDefinition|PropertyDefinition} node The property node to collect prefix tokens.
671673
*/
672674
function getPrefixTokens(node) {
673675
const prefixes = []
@@ -1750,9 +1752,8 @@ module.exports.defineVisitor = function create(
17501752
setOffset([dotToken, propertyToken], 1, objectToken)
17511753
}
17521754
},
1753-
/** @param {MethodDefinition | Property} node */
1754-
'MethodDefinition, Property'(node) {
1755-
const isMethod = node.type === 'MethodDefinition' || node.method === true
1755+
/** @param {MethodDefinition | Property | PropertyDefinition} node */
1756+
'MethodDefinition, Property, PropertyDefinition'(node) {
17561757
const prefixTokens = getPrefixTokens(node)
17571758
const hasPrefix = prefixTokens.length >= 1
17581759

@@ -1784,7 +1785,10 @@ module.exports.defineVisitor = function create(
17841785
}
17851786
}
17861787

1787-
if (isMethod) {
1788+
if (
1789+
node.type === 'MethodDefinition' ||
1790+
(node.type === 'Property' && node.method === true)
1791+
) {
17881792
const leftParenToken = tokenStore.getTokenAfter(lastKeyToken)
17891793

17901794
setOffset(leftParenToken, 1, lastKeyToken)
@@ -1793,6 +1797,11 @@ module.exports.defineVisitor = function create(
17931797
const valueToken = tokenStore.getTokenAfter(colonToken)
17941798

17951799
setOffset([colonToken, valueToken], 1, lastKeyToken)
1800+
} else if (node.type === 'PropertyDefinition' && node.value != null) {
1801+
const eqToken = tokenStore.getTokenAfter(lastKeyToken)
1802+
const initToken = tokenStore.getTokenAfter(eqToken)
1803+
1804+
setOffset([eqToken, initToken], 1, lastKeyToken)
17961805
}
17971806
},
17981807
/** @param {NewExpression} node */

lib/utils/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1848,23 +1848,24 @@ function skipChainExpression(node) {
18481848
*/
18491849
function getStaticPropertyName(node) {
18501850
if (node.type === 'Property' || node.type === 'MethodDefinition') {
1851-
const key = node.key
1852-
18531851
if (!node.computed) {
1852+
const key = node.key
18541853
if (key.type === 'Identifier') {
18551854
return key.name
18561855
}
18571856
}
1857+
const key = node.key
18581858
// @ts-expect-error
18591859
return getStringLiteralValue(key)
18601860
} else if (node.type === 'MemberExpression') {
1861-
const property = node.property
18621861
if (!node.computed) {
1862+
const property = node.property
18631863
if (property.type === 'Identifier') {
18641864
return property.name
18651865
}
18661866
return null
18671867
}
1868+
const property = node.property
18681869
// @ts-expect-error
18691870
return getStringLiteralValue(property)
18701871
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"eslint-plugin-prettier": "^3.1.3",
7575
"eslint-plugin-vue": "file:.",
7676
"eslint4b": "^7.0.0",
77+
"espree": "^8.0.0-0",
7778
"lodash": "^4.17.15",
7879
"mocha": "^7.1.2",
7980
"nyc": "^15.0.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!--{}-->
2+
<script>
3+
class Counter {
4+
get
5+
#x
6+
(
7+
)
8+
{
9+
}
10+
set
11+
#x
12+
(
13+
value
14+
) {
15+
}
16+
static
17+
get
18+
#y
19+
(
20+
) {
21+
}
22+
static
23+
set
24+
#y
25+
(
26+
value
27+
) {
28+
}
29+
inc() {
30+
this
31+
.#inc
32+
(
33+
)
34+
this.
35+
#inc
36+
(
37+
)
38+
}
39+
#inc
40+
(
41+
) {
42+
this
43+
.#x++;
44+
this.
45+
#x++;
46+
Counter
47+
.#staticInc
48+
(
49+
)
50+
Counter.
51+
#staticInc(
52+
)
53+
}
54+
static
55+
#staticInc
56+
(
57+
) {
58+
Counter
59+
.#y++;
60+
Counter.
61+
#y++;
62+
}
63+
}
64+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!--{}-->
2+
<script>
3+
class Counter {
4+
#x
5+
=
6+
0;
7+
static
8+
#y
9+
=
10+
0;
11+
inc() {
12+
this
13+
.#x++;
14+
this.
15+
#x++;
16+
Counter
17+
.#y++;
18+
Counter.
19+
#y++;
20+
}
21+
}
22+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!--{}-->
2+
<script>
3+
class Counter {
4+
[
5+
x
6+
]
7+
=
8+
0;
9+
static
10+
[
11+
y
12+
]
13+
=
14+
0;
15+
}
16+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!--{}-->
2+
<script>
3+
class Counter {
4+
x
5+
=
6+
0;
7+
static
8+
y
9+
=
10+
0;
11+
}
12+
</script>

tests/lib/rules/script-indent.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ function unIndent(strings) {
114114
const tester = new RuleTester({
115115
parser: require.resolve('vue-eslint-parser'),
116116
parserOptions: {
117-
ecmaVersion: 2020,
118-
sourceType: 'module'
117+
ecmaVersion: 2022,
118+
sourceType: 'module',
119+
parser: require.resolve('espree') // espree v8.0.0-beta.x
119120
}
120121
})
121122

typings/eslint-plugin-vue/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ declare global {
7171
// ---- ES Nodes ----
7272

7373
type Identifier = VAST.Identifier
74+
type PrivateIdentifier = VAST.PrivateIdentifier
7475
type Literal = VAST.Literal
7576
type Program = VAST.Program
7677
type SwitchCase = VAST.SwitchCase
@@ -135,6 +136,7 @@ declare global {
135136
type AssignmentPattern = VAST.AssignmentPattern
136137
type ClassBody = VAST.ClassBody
137138
type MethodDefinition = VAST.MethodDefinition
139+
type PropertyDefinition = VAST.PropertyDefinition
138140
type ModuleDeclaration = VAST.ModuleDeclaration
139141
type ImportDeclaration = VAST.ImportDeclaration
140142
type ExportNamedDeclaration = VAST.ExportNamedDeclaration

typings/eslint-plugin-vue/util-types/ast/ast.ts

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ export type NodeListenerMap = {
179179
export type ESNodeListenerMap = {
180180
Identifier: ES.Identifier
181181
'Identifier:exit': ES.Identifier
182+
PrivateIdentifier: ES.PrivateIdentifier
183+
'PrivateIdentifier:exit': ES.PrivateIdentifier
182184
Literal: ES.Literal
183185
'Literal:exit': ES.Literal
184186
Program: ES.Program
@@ -317,6 +319,8 @@ export type ESNodeListenerMap = {
317319
'ClassBody:exit': ES.ClassBody
318320
MethodDefinition: ES.MethodDefinition
319321
'MethodDefinition:exit': ES.MethodDefinition
322+
PropertyDefinition: ES.PropertyDefinition
323+
'PropertyDefinition:exit': ES.PropertyDefinition
320324
ImportDeclaration: ES.ImportDeclaration
321325
'ImportDeclaration:exit': ES.ImportDeclaration
322326
ExportNamedDeclaration: ES.ExportNamedDeclaration

0 commit comments

Comments
 (0)