Skip to content

Commit

Permalink
fix: update detection of object property shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenrui committed Nov 14, 2023
1 parent 3af3299 commit 100bbae
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,31 @@ export default class Lexer {
} else if (token.value === '}' && isObject) {
objectParent.pop();
if (objectParent.length === 0) isObject = false;

const lastToken = this._getLastToken();
const lastSecondToken = this._getLastToken(lastToken.index);

// identify ES6 object property shorthand
if (lastToken?.type === TOKEN.IdentifierName && !(lastSecondToken?.type === TOKEN.Punctuator && lastSecondToken.value === ':')) {
if (this._tokens[this._tokens.length - 1]?.type === TOKEN.WhiteSpace)
this._tokens.pop();

this._tokens.push({ type: TOKEN.Punctuator, value: ':', index: this._tokens.length });
this._tokens.push({ type: TOKEN.WhiteSpace, value: ' ', index: this._tokens.length });
this._tokens.push({ type: lastToken.type, value: lastToken.value, index: this._tokens.length });
}
} else if (isObject && token.value === ',') {
const lastToken = this._getLastToken();
const lastSecondToken = this._getLastToken(lastToken.index);

// identify ES6 object property shorthand
if (lastToken?.type === TOKEN.IdentifierName) {
this._tokens.push({ type: lastToken.type, value: lastToken.value, index: this._tokens.length })
if (lastToken?.type === TOKEN.IdentifierName && !(lastSecondToken?.type === TOKEN.Punctuator && lastSecondToken.value === ':')) {
if (this._tokens[this._tokens.length - 1]?.type === TOKEN.WhiteSpace)
this._tokens.pop();

this._tokens.push({ type: TOKEN.Punctuator, value: ':', index: this._tokens.length });
this._tokens.push({ type: TOKEN.WhiteSpace, value: ' ', index: this._tokens.length });
this._tokens.push({ type: lastToken.type, value: lastToken.value, index: this._tokens.length });
}
}
}
Expand Down

0 comments on commit 100bbae

Please sign in to comment.