From 7a78bb99db3b3c94d1ac022b444369fdfb12e01b Mon Sep 17 00:00:00 2001 From: Trevor Paley <10186337+TheUnlocked@users.noreply.github.com> Date: Thu, 18 Apr 2024 19:07:21 -0700 Subject: [PATCH] Fix insufficient cache invalidation in the incremental parser --- package.json | 2 +- src/parser/parser.ts | 19 +++++++++++++------ tests/grammar/incremental.spec.ts | 9 +++++++++ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 399836a..2086e4d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@necode-org/mike", - "version": "0.3.6", + "version": "0.3.7", "author": { "name": "Trevor Paley", "url": "https://github.com/TheUnlocked" diff --git a/src/parser/parser.ts b/src/parser/parser.ts index d18f48e..232d2e5 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -166,14 +166,21 @@ export class Parser extends DiagnosticsMixin implements Rules { for (const token of removedTokens) { let node = this.memoTable.get(token)?.[0]?.node; this.memoTable.delete(token); - while (node) { - const firstToken = node.tokens!.find(x => !isTrivia(x))!; - const memoEntry = this.memoTable.get(firstToken); - if (memoEntry) { - this.memoTable.set(firstToken, memoEntry.filter(x => x.node !== node)); + if (node) { + for (const token of node.tokens!) { + this.memoTable.delete(token); + } + + while (node) { + const firstToken = node.tokens!.find(x => !isTrivia(x))!; + + const memoEntry = this.memoTable.get(firstToken); + if (memoEntry) { + this.memoTable.set(firstToken, memoEntry.filter(x => x.node !== node)); + } + node = node.parent; } - node = node.parent; } } diff --git a/tests/grammar/incremental.spec.ts b/tests/grammar/incremental.spec.ts index 2227eb3..8eb65dc 100644 --- a/tests/grammar/incremental.spec.ts +++ b/tests/grammar/incremental.spec.ts @@ -202,6 +202,15 @@ export default () => describe('incremental', () => { } `]}`; + testIncremental('modify condition in if statement')` + on foo() { + let x = 1; + if x ${['', '== 1']} { + + } + } + `; + it('should be a no-op if a mutation is reversed', () => { const p1 = new Parser(); // 0 5 10 15 20 25 30 35 40