feat: more flexible squared property read expression, fixes #643 (#646)

* fix: more flexible squared property read expression, fixes #643

* fix: unecessary error wrapping in browser bundles

* style: update code style and types

* perf: use token.value when evalToken
This commit is contained in:
Jun Yang
2023-08-23 00:45:49 +08:00
committed by GitHub
parent dc6a301387
commit 660d9be55f
22 changed files with 261 additions and 167 deletions
+3
View File
@@ -1,7 +1,9 @@
import { Token } from './token'
import { TokenKind } from '../parser'
import { literalValues, LiteralValue } from '../util'
export class LiteralToken extends Token {
public content: LiteralValue
public literal: string
public constructor (
public input: string,
@@ -11,5 +13,6 @@ export class LiteralToken extends Token {
) {
super(TokenKind.Literal, input, begin, end, file)
this.literal = this.getText()
this.content = literalValues[this.literal]
}
}
+7 -4
View File
@@ -1,12 +1,15 @@
import { Token } from './token'
import { IdentifierToken } from './identifier-token'
import { TokenKind } from '../parser'
export class NumberToken extends Token {
public content: number
constructor (
public whole: IdentifierToken,
public decimal?: IdentifierToken
public input: string,
public begin: number,
public end: number,
public file?: string
) {
super(TokenKind.Number, whole.input, whole.begin, decimal ? decimal.end : whole.end, whole.file)
super(TokenKind.Number, input, begin, end, file)
this.content = Number(this.getText())
}
}
-14
View File
@@ -1,14 +0,0 @@
import { QuotedToken, PropertyAccessToken, IdentifierToken } from '.'
describe('PropertyAccessToken', function () {
describe('#propertyName', function () {
it('should return correct value for IdentifierToken', function () {
const token = new PropertyAccessToken(new IdentifierToken('foo', 0, 3), [], 3)
expect(token.propertyName).toBe('foo')
})
it('should return correct value for QuotedToken', function () {
const token = new PropertyAccessToken(new QuotedToken('"foo bar"', 0, 9), [], 9)
expect(token.propertyName).toBe('foo bar')
})
})
})
+12 -9
View File
@@ -1,18 +1,21 @@
import { Token } from './token'
import { LiteralToken } from './literal-token'
import { ValueToken } from './value-token'
import { IdentifierToken } from './identifier-token'
import { NumberToken } from './number-token'
import { RangeToken } from './range-token'
import { QuotedToken } from './quoted-token'
import { TokenKind, parseStringLiteral } from '../parser'
import { TokenKind } from '../parser'
export class PropertyAccessToken extends Token {
public propertyName: string
constructor (
public variable: IdentifierToken | QuotedToken,
public props: (IdentifierToken | QuotedToken | PropertyAccessToken)[],
end: number
public variable: QuotedToken | RangeToken | LiteralToken | NumberToken | undefined,
public props: (ValueToken | IdentifierToken)[],
input: string,
begin: number,
end: number,
file?: string
) {
super(TokenKind.PropertyAccess, variable.input, variable.begin, end, variable.file)
this.propertyName = this.variable instanceof IdentifierToken
? this.variable.getText()
: parseStringLiteral(this.variable.getText())
super(TokenKind.PropertyAccess, input, begin, end, file)
}
}
+3
View File
@@ -1,7 +1,9 @@
import { Token } from './token'
import { TokenKind } from '../parser'
import { parseStringLiteral } from '../render/string'
export class QuotedToken extends Token {
public readonly content: string
constructor (
public input: string,
public begin: number,
@@ -9,5 +11,6 @@ export class QuotedToken extends Token {
public file?: string
) {
super(TokenKind.Quoted, input, begin, end, file)
this.content = parseStringLiteral(this.getText())
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
import { RangeToken } from './range-token'
import { LiteralToken } from './literal-token'
import { NumberToken } from './number-token'
import { QuotedToken } from './quoted-token'
import { PropertyAccessToken } from './property-access-token'
export type ValueToken = RangeToken | LiteralToken | QuotedToken | PropertyAccessToken
export type ValueToken = RangeToken | LiteralToken | QuotedToken | PropertyAccessToken | NumberToken