mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 04:40:39 -07:00
chore(TypeScript): fix linting and generate .d.ts
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import Token from './token'
|
||||
|
||||
export default class DelimitedToken extends Token {
|
||||
trim_left: boolean
|
||||
trim_right: boolean
|
||||
constructor(raw, pos, input, file, line) {
|
||||
trimLeft: boolean
|
||||
trimRight: boolean
|
||||
constructor (raw, pos, input, file, line) {
|
||||
super(raw, pos, input, file, line)
|
||||
this.trim_left = raw[2] === '-'
|
||||
this.trim_right = raw[raw.length - 3] === '-'
|
||||
this.value = raw.slice(this.trim_left ? 3 : 2, this.trim_right ? -3 : -2).trim()
|
||||
this.trimLeft = raw[2] === '-'
|
||||
this.trimRight = raw[raw.length - 3] === '-'
|
||||
this.value = raw.slice(this.trimLeft ? 3 : 2, this.trimRight ? -3 : -2).trim()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import Token from './token'
|
||||
|
||||
export default class HTMLToken extends Token {
|
||||
constructor(str, begin, input, file, line) {
|
||||
constructor (str, begin, input, file, line) {
|
||||
super(str, begin, input, file, line)
|
||||
this.type = 'html'
|
||||
this.value = str
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import DelimitedToken from './delimited-token'
|
||||
|
||||
export default class OutputToken extends DelimitedToken {
|
||||
constructor(raw, pos, input, file, line) {
|
||||
constructor (raw, pos, input, file, line) {
|
||||
super(raw, pos, input, file, line)
|
||||
this.type = 'output'
|
||||
}
|
||||
|
||||
@@ -2,18 +2,16 @@ import { ParseError } from '../util/error'
|
||||
import Liquid from 'src/liquid'
|
||||
import ParseStream from './parse-stream'
|
||||
import Token from './token'
|
||||
import Tag from 'src/template/tag/tag'
|
||||
import HTMLToken from './html-token'
|
||||
import TagToken from './tag-token'
|
||||
import OutputToken from './output-token'
|
||||
import Tag from 'src/template/tag/tag'
|
||||
import Output from 'src/template/output'
|
||||
import HTML from 'src/template/html'
|
||||
import Value from 'src/template/value'
|
||||
|
||||
export default class Parser {
|
||||
liquid: Liquid
|
||||
|
||||
constructor(liquid: Liquid) {
|
||||
constructor (liquid: Liquid) {
|
||||
this.liquid = liquid
|
||||
}
|
||||
parse (tokens: Array<Token>) {
|
||||
@@ -27,10 +25,10 @@ export default class Parser {
|
||||
parseToken (token: Token, remainTokens: Array<Token>) {
|
||||
try {
|
||||
if (token.type === 'tag') {
|
||||
return new Tag(token, remainTokens, this.liquid)
|
||||
return new Tag(token as TagToken, remainTokens, this.liquid)
|
||||
}
|
||||
if (token.type === 'output') {
|
||||
return new Output(token, this.liquid.options.strict_filters)
|
||||
return new Output(token as OutputToken, this.liquid.options.strict_filters)
|
||||
}
|
||||
return new HTML(token)
|
||||
} catch (e) {
|
||||
|
||||
@@ -5,7 +5,7 @@ import * as lexical from './lexical'
|
||||
export default class TagToken extends DelimitedToken {
|
||||
name: string
|
||||
args: string
|
||||
constructor(raw, pos, input, file, line) {
|
||||
constructor (raw, pos, input, file, line) {
|
||||
super(raw, pos, input, file, line)
|
||||
this.type = 'tag'
|
||||
const match = this.value.match(lexical.tagLine)
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ export default class Token {
|
||||
input: string
|
||||
file: string
|
||||
value: string
|
||||
constructor(raw, pos, input, file, line) {
|
||||
constructor (raw, pos, input, file, line) {
|
||||
this.line = line
|
||||
this.raw = raw
|
||||
this.input = input
|
||||
|
||||
+44
-39
@@ -2,52 +2,57 @@ import whiteSpaceCtrl from './whitespace-ctrl'
|
||||
import HTMLToken from './html-token'
|
||||
import TagToken from './tag-token'
|
||||
import OutputToken from './output-token'
|
||||
import { LiquidOptions, defaultOptions } from 'src/liquid-options'
|
||||
|
||||
enum ParseState { HTML, OUTPUT, TAG }
|
||||
|
||||
export function parse (input: string, file?: string, options?) {
|
||||
const tokens = []
|
||||
let p = 0
|
||||
let line = 1
|
||||
let state = ParseState.HTML
|
||||
let buffer = ''
|
||||
let bufferBegin = 0
|
||||
export default class Tokenizer {
|
||||
options: LiquidOptions
|
||||
constructor (options: LiquidOptions = defaultOptions) {
|
||||
this.options = options
|
||||
}
|
||||
tokenize (input: string, file?: string) {
|
||||
const tokens = []
|
||||
let p = 0
|
||||
let line = 1
|
||||
let state = ParseState.HTML
|
||||
let buffer = ''
|
||||
let bufferBegin = 0
|
||||
|
||||
while(p < input.length) {
|
||||
if (input[p] === '\n') line++
|
||||
const bin = input.substr(p, 2)
|
||||
if (state === ParseState.HTML) {
|
||||
if (bin === '{{' || bin === '{%') {
|
||||
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
|
||||
buffer = bin
|
||||
bufferBegin = p
|
||||
while (p < input.length) {
|
||||
if (input[p] === '\n') line++
|
||||
const bin = input.substr(p, 2)
|
||||
if (state === ParseState.HTML) {
|
||||
if (bin === '{{' || bin === '{%') {
|
||||
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
|
||||
buffer = bin
|
||||
bufferBegin = p
|
||||
p += 2
|
||||
state = bin === '{{' ? ParseState.OUTPUT : ParseState.TAG
|
||||
continue
|
||||
}
|
||||
} else if (state === ParseState.OUTPUT && bin === '}}') {
|
||||
buffer += '}}'
|
||||
tokens.push(new OutputToken(buffer, bufferBegin, input, file, line))
|
||||
p += 2
|
||||
state = bin === '{{' ? ParseState.OUTPUT : ParseState.TAG
|
||||
buffer = ''
|
||||
bufferBegin = p
|
||||
state = ParseState.HTML
|
||||
continue
|
||||
} else if (bin === '%}') {
|
||||
buffer += '%}'
|
||||
tokens.push(new TagToken(buffer, bufferBegin, input, file, line))
|
||||
p += 2
|
||||
buffer = ''
|
||||
bufferBegin = p
|
||||
state = ParseState.HTML
|
||||
continue
|
||||
}
|
||||
buffer += input[p++]
|
||||
}
|
||||
else if (state === ParseState.OUTPUT && bin === '}}') {
|
||||
buffer += '}}'
|
||||
tokens.push(new OutputToken(buffer, bufferBegin, input, file, line))
|
||||
p += 2
|
||||
buffer = ''
|
||||
bufferBegin = p
|
||||
state = ParseState.HTML
|
||||
continue
|
||||
}
|
||||
else if (bin === '%}') {
|
||||
buffer += '%}'
|
||||
tokens.push(new TagToken(buffer, bufferBegin, input, file, line))
|
||||
p += 2
|
||||
buffer = ''
|
||||
bufferBegin = p
|
||||
state = ParseState.HTML
|
||||
continue
|
||||
}
|
||||
buffer += input[p++]
|
||||
}
|
||||
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
|
||||
if (buffer) tokens.push(new HTMLToken(buffer, bufferBegin, input, file, line))
|
||||
|
||||
whiteSpaceCtrl(tokens, options)
|
||||
return tokens
|
||||
whiteSpaceCtrl(tokens, this.options)
|
||||
return tokens
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,47 @@
|
||||
import { assign } from 'src/util/underscore'
|
||||
import TagToken from './tag-token'
|
||||
import OutputToken from './output-token'
|
||||
import HTMLToken from './html-token'
|
||||
import DelimitedToken from 'src/parser/delimited-token'
|
||||
import Token from 'src/parser/token'
|
||||
import TagToken from 'src/parser/tag-token'
|
||||
import { LiquidOptions } from 'src/liquid-options'
|
||||
|
||||
export default function whiteSpaceCtrl (tokens, options) {
|
||||
export default function whiteSpaceCtrl (tokens: Token[], options: LiquidOptions) {
|
||||
options = assign({ greedy: true }, options)
|
||||
let inRaw = false
|
||||
|
||||
tokens.forEach((token, i) => {
|
||||
if (shouldTrimLeft(token, inRaw, options)) {
|
||||
tokens.forEach((token: Token, i: number) => {
|
||||
if (shouldTrimLeft(token as DelimitedToken, inRaw, options)) {
|
||||
trimLeft(tokens[i - 1], options.greedy)
|
||||
}
|
||||
|
||||
if (token.type === 'tag' && token.name === 'raw') inRaw = true
|
||||
if (token.type === 'tag' && token.name === 'endraw') inRaw = false
|
||||
if (token.type === 'tag' && (token as TagToken).name === 'raw') inRaw = true
|
||||
if (token.type === 'tag' && (token as TagToken).name === 'endraw') inRaw = false
|
||||
|
||||
if (shouldTrimRight(token, inRaw, options)) {
|
||||
if (shouldTrimRight(token as DelimitedToken, inRaw, options)) {
|
||||
trimRight(tokens[i + 1], options.greedy)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function shouldTrimLeft (token, inRaw, options) {
|
||||
function shouldTrimLeft (token: DelimitedToken, inRaw: boolean, options) {
|
||||
if (inRaw) return false
|
||||
if (token.type === 'tag') return token.trim_left || options.trim_tag_left
|
||||
if (token.type === 'output') return token.trim_left || options.trim_value_left
|
||||
if (token.type === 'tag') return token.trimLeft || options.trim_tag_left
|
||||
if (token.type === 'output') return token.trimLeft || options.trim_value_left
|
||||
}
|
||||
|
||||
function shouldTrimRight (token, inRaw, options) {
|
||||
function shouldTrimRight (token: DelimitedToken, inRaw: boolean, options) {
|
||||
if (inRaw) return false
|
||||
if (token.type === 'tag') return token.trim_right || options.trim_tag_right
|
||||
if (token.type === 'output') return token.trim_right || options.trim_value_right
|
||||
if (token.type === 'tag') return token.trimRight || options.trim_tag_right
|
||||
if (token.type === 'output') return token.trimRight || options.trim_value_right
|
||||
}
|
||||
|
||||
function trimLeft (token, greedy) {
|
||||
function trimLeft (token: Token, greedy: boolean) {
|
||||
if (!token || token.type !== 'html') return
|
||||
|
||||
const rLeft = greedy ? /\s+$/g : /[\t\r ]*$/g
|
||||
token.value = token.value.replace(rLeft, '')
|
||||
}
|
||||
|
||||
function trimRight (token, greedy) {
|
||||
function trimRight (token: Token, greedy: boolean) {
|
||||
if (!token || token.type !== 'html') return
|
||||
|
||||
const rRight = greedy ? /^\s+/g : /^[\t\r ]*\n?/g
|
||||
|
||||
Reference in New Issue
Block a user