Skip to content

Commit

Permalink
Perform small cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Apr 5, 2024
1 parent f492fcd commit c7116f6
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/estree-util-value-to-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ export interface Options {
* The ESTree node.
*/
export function valueToEstree(value: unknown, options: Options = {}): Expression {
const declarations: VariableDeclarator[] = []
const stack: unknown[] = []
const collectedContexts = new Map<unknown, Context>()
const finalizers: Expression[] = []
Expand Down Expand Up @@ -575,7 +574,6 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression

analyze(value)

const rootContext = collectedContexts.get(value)!
for (const [val, context] of collectedContexts) {
if (context.recursive || context.count > 1) {
// Assign reused or recursive references to a variable.
Expand All @@ -591,17 +589,15 @@ export function valueToEstree(value: unknown, options: Options = {}): Expression
return generate(value)
}

for (const context of namedContexts.sort(compareContexts)) {
declarations.push({
type: 'VariableDeclarator',
id: identifier(context.name!),
init: generate(context.value, true)
})
}
const declarations = namedContexts.sort(compareContexts).map<VariableDeclarator>((context) => ({
type: 'VariableDeclarator',
id: identifier(context.name!),
init: generate(context.value, true)
}))

const rootContext = collectedContexts.get(value)
finalizers.push(
finalExpression ||
(rootContext.name ? identifier(rootContext.name) : generate(rootContext.value))
finalExpression || (rootContext ? identifier(rootContext.name!) : generate(value))
)

return {
Expand Down

0 comments on commit c7116f6

Please sign in to comment.