Fix 'cannot read property of undefined' (#211)
If a token is undefined, we should not read its source range!
This commit is contained in:
@ -130,9 +130,10 @@ function makeArguments(
|
|||||||
}
|
}
|
||||||
const nextBraceOrCommaToken = nextMeaningfulToken(tokens, argumentToken.index)
|
const nextBraceOrCommaToken = nextMeaningfulToken(tokens, argumentToken.index)
|
||||||
if (nextBraceOrCommaToken.token == undefined) {
|
if (nextBraceOrCommaToken.token == undefined) {
|
||||||
throw new KCLSyntaxError('Expected argument', [
|
throw new KCLSyntaxError(
|
||||||
[argumentToken.token.start, argumentToken.token.end],
|
'Expected argument',
|
||||||
])
|
rangeOfToken(argumentToken.token)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
const isIdentifierOrLiteral =
|
const isIdentifierOrLiteral =
|
||||||
nextBraceOrCommaToken.token.type === 'comma' ||
|
nextBraceOrCommaToken.token.type === 'comma' ||
|
||||||
@ -290,7 +291,7 @@ function makeArguments(
|
|||||||
}
|
}
|
||||||
throw new KCLSyntaxError(
|
throw new KCLSyntaxError(
|
||||||
'Expected a previous Argument if statement to match',
|
'Expected a previous Argument if statement to match',
|
||||||
[[argumentToken.token.start, argumentToken.token.end]]
|
rangeOfToken(argumentToken.token)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -415,18 +416,20 @@ function makeValue(
|
|||||||
lastIndex: arrowFunctionLastIndex,
|
lastIndex: arrowFunctionLastIndex,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new KCLUnimplementedError('expression with braces', [
|
throw new KCLUnimplementedError(
|
||||||
[currentToken.start, currentToken.end],
|
'expression with braces',
|
||||||
])
|
rangeOfToken(currentToken)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (currentToken.type === 'operator' && currentToken.value === '-') {
|
if (currentToken.type === 'operator' && currentToken.value === '-') {
|
||||||
const { expression, lastIndex } = makeUnaryExpression(tokens, index)
|
const { expression, lastIndex } = makeUnaryExpression(tokens, index)
|
||||||
return { value: expression, lastIndex }
|
return { value: expression, lastIndex }
|
||||||
}
|
}
|
||||||
throw new KCLSyntaxError('Expected a previous Value if statement to match', [
|
throw new KCLSyntaxError(
|
||||||
[currentToken.start, currentToken.end],
|
'Expected a previous Value if statement to match',
|
||||||
])
|
rangeOfToken(currentToken)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeVariableDeclarators(
|
function makeVariableDeclarators(
|
||||||
@ -518,9 +521,10 @@ function makeArrayElements(
|
|||||||
nextToken.token.type === 'brace' && nextToken.token.value === ']'
|
nextToken.token.type === 'brace' && nextToken.token.value === ']'
|
||||||
const isComma = nextToken.token.type === 'comma'
|
const isComma = nextToken.token.type === 'comma'
|
||||||
if (!isClosingBrace && !isComma) {
|
if (!isClosingBrace && !isComma) {
|
||||||
throw new KCLSyntaxError('Expected a comma or closing brace', [
|
throw new KCLSyntaxError(
|
||||||
[nextToken.token.start, nextToken.token.end],
|
'Expected a comma or closing brace',
|
||||||
])
|
rangeOfToken(nextToken.token)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
const nextCallIndex = isClosingBrace
|
const nextCallIndex = isClosingBrace
|
||||||
? nextToken.index
|
? nextToken.index
|
||||||
@ -633,9 +637,7 @@ function makeMemberExpression(
|
|||||||
const lastKey = keysInfo[keysInfo.length - 1]
|
const lastKey = keysInfo[keysInfo.length - 1]
|
||||||
const firstKey = keysInfo.shift()
|
const firstKey = keysInfo.shift()
|
||||||
if (!firstKey)
|
if (!firstKey)
|
||||||
throw new KCLSyntaxError('Expected a key', [
|
throw new KCLSyntaxError('Expected a key', rangeOfToken(currentToken))
|
||||||
[currentToken.start, currentToken.end],
|
|
||||||
])
|
|
||||||
const root = makeIdentifier(tokens, index)
|
const root = makeIdentifier(tokens, index)
|
||||||
let memberExpression: MemberExpression = {
|
let memberExpression: MemberExpression = {
|
||||||
type: 'MemberExpression',
|
type: 'MemberExpression',
|
||||||
@ -827,7 +829,7 @@ function makePipeBody(
|
|||||||
} else {
|
} else {
|
||||||
throw new KCLSyntaxError(
|
throw new KCLSyntaxError(
|
||||||
'Expected a previous PipeValue if statement to match',
|
'Expected a previous PipeValue if statement to match',
|
||||||
[[currentToken.start, currentToken.end]]
|
rangeOfToken(currentToken)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1094,7 +1096,7 @@ function makeBody(
|
|||||||
lastIndex,
|
lastIndex,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new KCLSyntaxError('Unexpected token', [[token.start, token.end]])
|
throw new KCLSyntaxError('Unexpected token', rangeOfToken(token))
|
||||||
}
|
}
|
||||||
export const abstractSyntaxTree = (tokens: Token[]): Program => {
|
export const abstractSyntaxTree = (tokens: Token[]): Program => {
|
||||||
const { body, nonCodeMeta } = makeBody({ tokens })
|
const { body, nonCodeMeta } = makeBody({ tokens })
|
||||||
@ -1250,7 +1252,7 @@ export function findClosingBrace(
|
|||||||
if (!['(', '{', '['].includes(searchOpeningBrace)) {
|
if (!['(', '{', '['].includes(searchOpeningBrace)) {
|
||||||
throw new KCLSyntaxError(
|
throw new KCLSyntaxError(
|
||||||
`expected to be started on a opening brace ( { [, instead found '${searchOpeningBrace}'`,
|
`expected to be started on a opening brace ( { [, instead found '${searchOpeningBrace}'`,
|
||||||
[[currentToken.start, currentToken.end]]
|
rangeOfToken(currentToken)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1262,9 +1264,10 @@ export function findClosingBrace(
|
|||||||
currentToken.value === closingBraceMap[searchOpeningBrace]
|
currentToken.value === closingBraceMap[searchOpeningBrace]
|
||||||
)
|
)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new KCLSyntaxError('Missing a closing brace', [
|
throw new KCLSyntaxError(
|
||||||
[currentToken.start, currentToken.end],
|
'Missing a closing brace',
|
||||||
])
|
rangeOfToken(currentToken)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
@ -1351,3 +1354,7 @@ export function isNotCodeToken(token: Token): boolean {
|
|||||||
token?.type === 'blockcomment'
|
token?.type === 'blockcomment'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function rangeOfToken(token: Token | undefined): [number, number][] {
|
||||||
|
return token === undefined ? [] : [[token.start, token.end]]
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user