Skip to content

Commit

Permalink
Merge pull request #18 from Group-One-Technology/jr.fix/lexer
Browse files Browse the repository at this point in the history
Fix: Lexer Bugs Found
  • Loading branch information
jorenrui authored Feb 26, 2024
2 parents 2ccc4a1 + e1b43f8 commit 8f9d6b1
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions lib/generators/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import { Parser } from 'acorn'
import * as walk from 'acorn-walk'
import escodegen from 'escodegen'

const FUNCTION_NODE_TYPES = [
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
]

function getMemberIdentifier(node) {
if (node.type === 'MemberExpression') {
const object = getMemberIdentifier(node.object)
Expand Down Expand Up @@ -49,14 +55,9 @@ function getDeclaredVariables(node, isParent = true) {
}, [])

return ids
} else if (
[
'FunctionDeclaration',
'ArrowFunctionExpression',
'FunctionExpression',
].includes(node.type)
) {
const ids = []
} else if (FUNCTION_NODE_TYPES.includes(node.type)) {
const ids = ['arguments']

if (isParent) {
const params = node.params.reduce((acc, param) => {
if (param.type === 'Identifier') acc.push(param.name)
Expand All @@ -72,6 +73,7 @@ function getDeclaredVariables(node, isParent = true) {
}

if (node.id?.type === 'Identifier') ids.push(node.id.name)

return ids
} else if (node.type === 'ObjectPattern') {
const ids = node.properties.reduce((acc, prop) => {
Expand Down Expand Up @@ -177,6 +179,17 @@ export class Lexer {

if (currentNode == null) break

if (isParent) {
const parentFunctionNode = ancestors.find((node) =>
FUNCTION_NODE_TYPES.includes(node.type)
)

if (parentFunctionNode) {
const declarations = getDeclaredVariables(parentFunctionNode, true)
if (declarations.includes(id)) return true
}
}

const declarations = getDeclaredVariables(currentNode, isParent)
if (declarations.includes(id)) return true
}
Expand Down Expand Up @@ -223,6 +236,8 @@ export class Lexer {
if (this.isDeclared(node.name, ancestors))
this._declaredIdentifiers.push(node.name)
else this._identifiers.push(node.name)
} else if (node?.type === 'ThisExpression') {
this._identifiers.push('this')
} else {
const declarations = getDeclaredVariables(node, true).filter(
(id) =>
Expand Down

0 comments on commit 8f9d6b1

Please sign in to comment.