fix dotget

This commit is contained in:
Chris Wanstrath 2025-10-29 21:37:42 -07:00
parent 789481f4ef
commit f31be80bb0
2 changed files with 49 additions and 2 deletions

View File

@ -75,6 +75,12 @@ export const trackScope = new ContextTracker<TrackerContext>({
return new TrackerContext(context.scope, [...context.pendingIds, text]) return new TrackerContext(context.scope, [...context.pendingIds, text])
} }
// Track identifiers in array destructuring: [ a b ] = ...
if (!inParams && term === terms.Identifier && isArrayDestructuring(input)) {
const text = readIdentifierText(input, input.pos, stack.pos)
return new TrackerContext(Scope.add(context.scope, text), context.pendingIds)
}
return context return context
}, },
@ -98,3 +104,26 @@ export const trackScope = new ContextTracker<TrackerContext>({
hash: (context) => context.scope.hash(), hash: (context) => context.scope.hash(),
}) })
// Check if we're parsing array destructuring: [ a b ] = ...
const isArrayDestructuring = (input: InputStream): boolean => {
let pos = 0
// Find closing bracket
while (pos < 200 && input.peek(pos) !== 93 /* ] */) {
if (input.peek(pos) === -1) return false // EOF
pos++
}
if (input.peek(pos) !== 93 /* ] */) return false
pos++
// Skip whitespace
while (input.peek(pos) === 32 /* space */ ||
input.peek(pos) === 9 /* tab */ ||
input.peek(pos) === 10 /* \n */) {
pos++
}
return input.peek(pos) === 61 /* = */
}

View File

@ -630,6 +630,24 @@ describe('Array destructuring', () => {
Number 1 Number 1
Number 2`) Number 2`)
}) })
test('works with dotget', () => {
expect('[ a ] = [ [1 2 3] ]; a.1').toMatchTree(`
Assign
Array
Identifier a
Eq =
Array
Array
Number 1
Number 2
Number 3
FunctionCallOrIdentifier
DotGet
IdentifierBeforeDot a
Number 1`)
})
}) })
describe('Conditional ops', () => { describe('Conditional ops', () => {