Skip to content

Commit

Permalink
Supports ES2022 (Class Fileds)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Apr 22, 2021
1 parent 7e5f2e9 commit b2dcb52
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 23 deletions.
6 changes: 5 additions & 1 deletion .vscode/settings.json
Expand Up @@ -9,5 +9,9 @@
"vue"
],
"typescript.tsdk": "node_modules/typescript/lib",
"vetur.validation.script": false
"vetur.validation.script": false,
"[typescript]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
}
4 changes: 4 additions & 0 deletions lib/rules/require-slots-as-functions.js
Expand Up @@ -113,6 +113,10 @@ module.exports = {
if (!utils.isThis(object.object, context)) {
return
}
if (node.property.type === 'PrivateIdentifier') {
// Unreachable
return
}
verify(node, node.property)
}
})
Expand Down
19 changes: 14 additions & 5 deletions lib/utils/indent-common.js
Expand Up @@ -60,8 +60,10 @@ const KNOWN_NODES = new Set([
'NewExpression',
'ObjectExpression',
'ObjectPattern',
'PrivateIdentifier',
'Program',
'Property',
'PropertyDefinition',
'RestElement',
'ReturnStatement',
'SequenceExpression',
Expand Down Expand Up @@ -667,7 +669,7 @@ module.exports.defineVisitor = function create(
/**
* Collect prefix tokens of the given property.
* The prefix includes `async`, `get`, `set`, `static`, and `*`.
* @param {Property|MethodDefinition} node The property node to collect prefix tokens.
* @param {Property|MethodDefinition|PropertyDefinition} node The property node to collect prefix tokens.
*/
function getPrefixTokens(node) {
const prefixes = []
Expand Down Expand Up @@ -1759,9 +1761,8 @@ module.exports.defineVisitor = function create(
setOffset([dotToken, propertyToken], 1, objectToken)
}
},
/** @param {MethodDefinition | Property} node */
'MethodDefinition, Property'(node) {
const isMethod = node.type === 'MethodDefinition' || node.method === true
/** @param {MethodDefinition | Property | PropertyDefinition} node */
'MethodDefinition, Property, PropertyDefinition'(node) {
const prefixTokens = getPrefixTokens(node)
const hasPrefix = prefixTokens.length >= 1

Expand Down Expand Up @@ -1795,7 +1796,10 @@ module.exports.defineVisitor = function create(
}
}

if (isMethod) {
if (
node.type === 'MethodDefinition' ||
(node.type === 'Property' && node.method === true)
) {
const leftParenToken = tokenStore.getTokenAfter(lastKeyToken)

setOffset(leftParenToken, 1, lastKeyToken)
Expand All @@ -1804,6 +1808,11 @@ module.exports.defineVisitor = function create(
const valueToken = tokenStore.getTokenAfter(colonToken)

setOffset([colonToken, valueToken], 1, lastKeyToken)
} else if (node.type === 'PropertyDefinition' && node.value != null) {
const eqToken = tokenStore.getTokenAfter(lastKeyToken)
const initToken = tokenStore.getTokenAfter(eqToken)

setOffset([eqToken, initToken], 1, lastKeyToken)
}
},
/** @param {NewExpression} node */
Expand Down
7 changes: 4 additions & 3 deletions lib/utils/index.js
Expand Up @@ -1798,23 +1798,24 @@ function skipChainExpression(node) {
*/
function getStaticPropertyName(node) {
if (node.type === 'Property' || node.type === 'MethodDefinition') {
const key = node.key

if (!node.computed) {
const key = node.key
if (key.type === 'Identifier') {
return key.name
}
}
const key = node.key
// @ts-expect-error
return getStringLiteralValue(key)
} else if (node.type === 'MemberExpression') {
const property = node.property
if (!node.computed) {
const property = node.property
if (property.type === 'Identifier') {
return property.name
}
return null
}
const property = node.property
// @ts-expect-error
return getStringLiteralValue(property)
}
Expand Down
64 changes: 64 additions & 0 deletions tests/fixtures/script-indent/class-fields-private-methods-01.vue
@@ -0,0 +1,64 @@
<!--{}-->
<script>
class Counter {
get
#x
(
)
{
}
set
#x
(
value
) {
}
static
get
#y
(
) {
}
static
set
#y
(
value
) {
}
inc() {
this
.#inc
(
)
this.
#inc
(
)
}
#inc
(
) {
this
.#x++;
this.
#x++;
Counter
.#staticInc
(
)
Counter.
#staticInc(
)
}
static
#staticInc
(
) {
Counter
.#y++;
Counter.
#y++;
}
}
</script>
@@ -0,0 +1,22 @@
<!--{}-->
<script>
class Counter {
#x
=
0;
static
#y
=
0;
inc() {
this
.#x++;
this.
#x++;
Counter
.#y++;
Counter.
#y++;
}
}
</script>
16 changes: 16 additions & 0 deletions tests/fixtures/script-indent/class-fields-properties-01.vue
@@ -0,0 +1,16 @@
<!--{}-->
<script>
class Counter {
[
x
]
=
0;
static
[
y
]
=
0;
}
</script>
12 changes: 12 additions & 0 deletions tests/fixtures/script-indent/class-fields-properties-02.vue
@@ -0,0 +1,12 @@
<!--{}-->
<script>
class Counter {
x
=
0;
static
y
=
0;
}
</script>
2 changes: 1 addition & 1 deletion tests/lib/rules/script-indent.js
Expand Up @@ -114,7 +114,7 @@ function unIndent(strings) {
const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 2022,
sourceType: 'module'
}
})
Expand Down
2 changes: 2 additions & 0 deletions typings/eslint-plugin-vue/global.d.ts
Expand Up @@ -71,6 +71,7 @@ declare global {
// ---- ES Nodes ----

type Identifier = VAST.Identifier
type PrivateIdentifier = VAST.PrivateIdentifier
type Literal = VAST.Literal
type Program = VAST.Program
type SwitchCase = VAST.SwitchCase
Expand Down Expand Up @@ -135,6 +136,7 @@ declare global {
type AssignmentPattern = VAST.AssignmentPattern
type ClassBody = VAST.ClassBody
type MethodDefinition = VAST.MethodDefinition
type PropertyDefinition = VAST.PropertyDefinition
type ModuleDeclaration = VAST.ModuleDeclaration
type ImportDeclaration = VAST.ImportDeclaration
type ExportNamedDeclaration = VAST.ExportNamedDeclaration
Expand Down
4 changes: 4 additions & 0 deletions typings/eslint-plugin-vue/util-types/ast/ast.ts
Expand Up @@ -179,6 +179,8 @@ export type NodeListenerMap = {
export type ESNodeListenerMap = {
Identifier: ES.Identifier
'Identifier:exit': ES.Identifier
PrivateIdentifier: ES.PrivateIdentifier
'PrivateIdentifier:exit': ES.PrivateIdentifier
Literal: ES.Literal
'Literal:exit': ES.Literal
Program: ES.Program
Expand Down Expand Up @@ -317,6 +319,8 @@ export type ESNodeListenerMap = {
'ClassBody:exit': ES.ClassBody
MethodDefinition: ES.MethodDefinition
'MethodDefinition:exit': ES.MethodDefinition
PropertyDefinition: ES.PropertyDefinition
'PropertyDefinition:exit': ES.PropertyDefinition
ImportDeclaration: ES.ImportDeclaration
'ImportDeclaration:exit': ES.ImportDeclaration
ExportNamedDeclaration: ES.ExportNamedDeclaration
Expand Down

0 comments on commit b2dcb52

Please sign in to comment.