Skip to content

Commit

Permalink
💥 change the AST of filters syntax (vuejs/eslint-plugin-vue#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Dec 1, 2018
1 parent 59d52ed commit 1fe4127
Show file tree
Hide file tree
Showing 23 changed files with 3,989 additions and 48 deletions.
13 changes: 13 additions & 0 deletions docs/ast.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ interface VSlotScopeExpression <: Expression {
type: "VSlotScopeExpression"
id: Pattern
}

interface VFilterSequenceExpression <: Expression {
type: "VFilterSequenceExpression"
expression: Expression
filters: [ VFilter ]
}

interface VFilter <: Node {
type: "VFilter"
callee: Identifier
arguments: [ Expression ]
}
```

- This is mustaches or directive values.
Expand All @@ -119,6 +131,7 @@ interface VSlotScopeExpression <: Expression {
- `VForExpression` is an expression node like [ForInStatement] but it has an array as `left` property and does not have `body` property. This is the value of [`v-for` directives].
- `VOnExpression` is an expression node like [BlockStatement] but it does not have braces. This is the value of [`v-on` directives] only if the `v-on` directive doesn't have that argument.
- `VSlotScopeExpression` is an expression node like [VariableDeclarator]. This is the value of [`slot-scope` attribute] or the `scope` attribute of `<template>` elements.
- `VFilterSequenceExpression` is an expression node for [Vue.js Filters](https://vuejs.org/v2/guide/filters.html) syntax.

> Note: `vue-eslint-parser` transforms `v-for="(x, i) in list"` to `for(let [x, i] in list);` then gives the configured parser (`espree` by default) it. This implies that it needs the capability to parse ES2015 destructuring in order to parse [`v-for` directives].
Expand Down
23 changes: 23 additions & 0 deletions src/ast/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export type Node =
| VForExpression
| VOnExpression
| VSlotScopeExpression
| VFilterSequenceExpression
| VFilter

//------------------------------------------------------------------------------
// Script
Expand Down Expand Up @@ -688,6 +690,26 @@ export interface VSlotScopeExpression extends HasLocation, HasParent {
id: ESLintPattern
}

/**
* The node of a filter sequence which is separated by `|`.
*/
export interface VFilterSequenceExpression extends HasLocation, HasParent {
type: "VFilterSequenceExpression"
parent: VExpressionContainer
expression: ESLintExpression
filters: VFilter[]
}

/**
* The node of a filter sequence which is separated by `|`.
*/
export interface VFilter extends HasLocation, HasParent {
type: "VFilter"
parent: VFilterSequenceExpression
callee: ESLintIdentifier
arguments: (ESLintExpression | ESLintSpreadElement)[]
}

/**
* The union type of any nodes.
*/
Expand Down Expand Up @@ -722,6 +744,7 @@ export interface VExpressionContainer extends HasLocation, HasParent {
parent: VDocumentFragment | VElement | VDirective
expression:
| ESLintExpression
| VFilterSequenceExpression
| VForExpression
| VOnExpression
| VSlotScopeExpression
Expand Down
2 changes: 2 additions & 0 deletions src/ast/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const KEYS = Evk.unionWith({
VElement: ["startTag", "children", "endTag"],
VEndTag: [],
VExpressionContainer: ["expression"],
VFilter: ["callee", "arguments"],
VFilterSequenceExpression: ["expression", "filters"],
VForExpression: ["left", "right"],
VIdentifier: [],
VLiteral: [],
Expand Down
4 changes: 3 additions & 1 deletion src/common/location-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export class LocationCalculator {
* Modify the location information of the given node with using the base offset and gaps of this calculator.
* @param node The node to modify their location.
*/
public fixLocation(node: HasLocation): void {
public fixLocation<T extends HasLocation>(node: T): T {
const range = node.range
const loc = node.loc
const gap0 = this._getGap(range[0])
Expand All @@ -129,6 +129,8 @@ export class LocationCalculator {
}
loc.end = this._getLocation(range[1])
}

return node
}

/**
Expand Down
Loading

0 comments on commit 1fe4127

Please sign in to comment.