Skip to content

Commit

Permalink
perf: remove instanceof DelimitedToken
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 14, 2020
1 parent 59eeb78 commit 6b2ccb6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
25 changes: 13 additions & 12 deletions src/parser/token-kind.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export enum TokenKind {
Number,
Literal,
Tag,
Output,
HTML,
Filter,
Hash,
PropertyAccess,
Word,
Range,
Quoted,
Operator
Number = 1,
Literal = 2,
Tag = 4,
Output = 8,
HTML = 16,
Filter = 32,
Hash = 64,
PropertyAccess = 128,
Word = 256,
Range = 512,
Quoted = 1024,
Operator = 2048,
Delimited = Tag | Output
}
5 changes: 2 additions & 3 deletions src/parser/whitespace-ctrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Token } from '../tokens/token'
import { DelimitedToken } from '../tokens/delimited-token'
import { isTagToken, isHTMLToken } from '../util/type-guards'
import { isTagToken, isHTMLToken, isDelimitedToken } from '../util/type-guards'
import { NormalizedFullOptions } from '../liquid-options'
import { TYPES, INLINE_BLANK, BLANK } from '../util/character'

Expand All @@ -10,7 +9,7 @@ export function whiteSpaceCtrl (tokens: Token[], options: NormalizedFullOptions)

for (let i = 0; i < tokens.length; i++) {
const token = tokens[i]
if (!(token instanceof DelimitedToken)) continue
if (!isDelimitedToken(token)) continue
if (!inRaw && token.trimLeft) {
trimLeft(tokens[i - 1], options.greedy)
}
Expand Down
5 changes: 5 additions & 0 deletions src/util/type-guards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OperatorToken } from '../tokens/operator-token'
import { DelimitedToken } from '../tokens/delimited-token'
import { WordToken } from '../tokens/word-token'
import { TagToken } from '../tokens/tag-token'
import { HTMLToken } from '../tokens/html-token'
Expand All @@ -10,6 +11,10 @@ import { NumberToken } from '../tokens/number-token'
import { RangeToken } from '../tokens/range-token'
import { TokenKind } from '../parser/token-kind'

export function isDelimitedToken (val: any): val is DelimitedToken {
return !!(getKind(val) & TokenKind.Delimited)
}

export function isOperatorToken (val: any): val is OperatorToken {
return getKind(val) === TokenKind.Operator
}
Expand Down

0 comments on commit 6b2ccb6

Please sign in to comment.