8000 Add twig support · vuejs/vue-eslint-parser@e21436d · GitHub
[go: up one dir, main page]

Skip to content

Commit e21436d

Browse files
committed
Add twig support
1 parent 6aa8585 commit e21436d

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

src/ast/nodes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ export interface VDocumentFragment
964964
extends HasLocation,
965965
HasParent,
966966
HasConcreteInfo {
967+
twigExpressions: Token[]
967968
type: "VDocumentFragment"
968969
parent: null
969970
children: (VElement | VText | VExpressionContainer | VStyleElement)[]

src/html/custom-tokenizer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface CustomTemplateTokenizer {
1111
* The tokenized low level comment tokens
1212
*/
1313
readonly comments: Token[]
14+
/**
15+
* The tokenized twig expression tokens
16+
*/
17+
readonly twigExpressions: Token[]
1418
/**
1519
* The source code text.
1620
*/

src/html/intermediate-tokenizer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class IntermediateTokenizer {
8282

8383
public readonly tokens: Token[]
8484
public readonly comments: Token[]
85+
public readonly twigExpressions: Token[]
8586

8687
/**
8788
* The source code text.
@@ -140,6 +141,7 @@ export class IntermediateTokenizer {
140141
this.expressionTokens = []
141142
this.tokens = []
142143
this.comments = []
144+
this.twigExpressions = []
143145
}
144146

145147
/**
@@ -228,6 +230,19 @@ export class IntermediateTokenizer {
228230
return null
229231
}
230232

233+
/**
234+
* Process the given twig token.
235+
* @param token The twig token to process.
236+
*/
237+
private processTwigExpression(token: Token): IntermediateToken | null {
238+
this.twigExpressions.push(token)
239+
240+
if (this.currentToken != null && this.currentToken.type === "Text") {
241+
return this.commit()
242+
}
243+
return null
244+
}
245+
231246
/**
232247
* Process the given text token.
233248
* @param token The text token to process.
@@ -321,6 +336,14 @@ export class IntermediateTokenizer {
321336
return this.processComment(token)
322337
}
323338

339+
/**
340+
* Process a TwigExpression token.
341+
* @param token The token to process.
342+
*/
343+
protected TwigExpression(token: Token): IntermediateToken | null {
344+
return this.processTwigExpression(token)
345+
}
346+
324347
/**
325348
* Process a HTMLEndTagOpen token.
326349
* @param token The token to process.

src/html/parser.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ export class Parser {
205205
return this.tokenizer.comments
206206
}
207207

208+
/**
209+
* The twig expressions.
210+
*/
211+
private get twigExpressions(): Token[] {
212+
return this.tokenizer.twigExpressions
213+
}
214+
208215
/**
209216
* The syntax errors which are found in this parsing.
210217
*/
@@ -270,6 +277,7 @@ export class Parser {
270277
children: [],
271278
tokens: this.tokens,
272279
comments: this.comments,
280+
twigExpressions: this.twigExpressions,
273281
errors: this.errors,
274282
}
275283
this.elementStack = []

src/html/tokenizer.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
SEMICOLON,
5151
SOLIDUS,
5252
toLowerCodePoint,
53+
PERCENT,
5354
} from "./util/unicode"
5455
import type { ParserOptions } from "../common/parser-options"
5556

@@ -73,6 +74,7 @@ export type TokenType =
7374
| "HTMLWhitespace"
7475
| "VExpressionStart"
7576
| "VExpressionEnd"
77+
| "TwigExpression"
7678

7779
/**
7880
* Enumeration of tokenizer's state types.
@@ -127,6 +129,7 @@ export type TokenizerState =
127129
| "V_EXPRESSION_START"
128130
| "V_EXPRESSION_DATA"
129131
| "V_EXPRESSION_END"
132+
| "TWIG_EXPRESSION"
130133
// ---- Use RAWTEXT state for <script> elements instead ----
131134
// "SCRIPT_DATA" |
132135
// "SCRIPT_DATA_LESS_THAN_SIGN" |
@@ -616,6 +619,11 @@ export class Tokenizer {
616619
this.setStartTokenMark()
617620
return "TAG_OPEN"
618621
}
622+
if (cp === LEFT_CURLY_BRACKET) {
623+
this.setStartTokenMark()
624+
this.returnState = "DATA"
625+
return "TWIG_EXPRESSION"
626+
}
619627
if (cp === LEFT_CURLY_BRACKET && this.expressionEnabled) {
620628
this.setStartTokenMark()
621629
this.returnState = "DATA"
@@ -1010,6 +1018,11 @@ export class Tokenizer {
10101018
this.appendTokenValue(cp, "HTMLIdentifier")
10111019
return "ATTRIBUTE_NAME"
10121020
}
1021+
if (cp === LEFT_CURLY_BRACKET) {
1022+
this.setStartTokenMark()
1023+
this.returnState = "BEFORE_ATTRIBUTE_NAME"
1024+
return "TWIG_EXPRESSION"
1025+
}
10131026

10141027
this.startToken("HTMLIdentifier")
10151028
return this.reconsumeAs("ATTRIBUTE_NAME")
@@ -1951,6 +1964,32 @@ export class Tokenizer {
19511964
this.appendTokenValue(RIGHT_CURLY_BRACKET, null)
19521965
return this.reconsumeAs(this.returnState)
19531966
}
1967+
/**
1968+
* Create `{% ... %} `token.
1969+
* @param cp The current code point.
1970+
* @returns The next state.
1971+
*/
1972+
protected TWIG_EXPRESSION(cp: number): TokenizerState {
1973+
if (cp !== PERCENT) {
1974+
this.appendTokenValue(LEFT_CURLY_BRACKET, null)
1975+
return this.reconsumeAs(this.returnState)
1976+
}
1977+
1978+
this.startToken("TwigExpression")
1979+
this.appendTokenValue(LEFT_CURLY_BRACKET, null)
1980+
this.appendTokenValue(PERCENT, null)
1981+
1982+
cp = this.consumeNextCodePoint()
1983+
while (cp !== PERCENT) {
1984+
this.appendTokenValue(cp, null)
1985+
cp = this.consumeNextCodePoint()
1986+
}
1987+
1988+
cp = this.consumeNextCodePoint()
1989+
this.appendTokenValue(PERCENT, null)
1990+
this.appendTokenValue(RIGHT_CURLY_BRACKET, null)
1991+
return this.returnState
1992+
}
19541993
}
19551994

19561995
/*eslint-enable no-constant-condition, no-param-reassign */

src/html/util/unicode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const SPACE = 0x20
1414
export const EXCLAMATION_MARK = 0x21 // !
1515
export const QUOTATION_MARK = 0x22 // "
1616
export const NUMBER_SIGN = 0x23 // #
17+
export const PERCENT = 0x25 // %
1718
export const AMPERSAND = 0x26 // &
1819
export const APOSTROPHE = 0x27 // '
1920
export const LEFT_PARENTHESIS = 0x28 // (

0 commit comments

Comments
 (0)
0