Skip to content

Commit

Permalink
Fix some edge cases in incremental parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheUnlocked committed Apr 20, 2024
1 parent 10d588a commit 6d366bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@necode-org/mike",
"version": "0.3.7",
"version": "0.3.8",
"author": {
"name": "Trevor Paley",
"url": "https://github.com/TheUnlocked"
Expand Down
14 changes: 13 additions & 1 deletion src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,20 @@ export class Parser extends DiagnosticsMixin implements Rules {

const { insertedTokens, removedTokens } = this.lexer.mutate(firstTokenIdx, numTokens, fullInsertion);

// Need to also invalidate the previous (non-trivia) token for cases where
// a new token can modify an earlier AST node. For example:
// if true {
//
// }
// els[e] {
//
// }
// Inserting the 'e' changes it from an identifier and a parse error into an else branch of the
// previous if statement. Without invalidating the previous if statement, we couldn't determine that.
const firstRemovedTokenIdx = this.tokens.slice(0, firstTokenIdx).findLastIndex(x => !isTrivia(x));

// Cache invalidation
for (const token of removedTokens) {
for (const token of this.tokens.slice(firstRemovedTokenIdx, firstTokenIdx).concat(removedTokens)) {
let node = this.memoTable.get(token)?.[0]?.node;
this.memoTable.delete(token);

Expand Down
11 changes: 11 additions & 0 deletions tests/grammar/incremental.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ export default () => describe('incremental', () => {
}
`;

testIncremental('modify identifier to create else keyword')`
on foo() {
if true {
}
els${['', 'e']} {
}
}
`;

it('should be a no-op if a mutation is reversed', () => {
const p1 = new Parser();
// 0 5 10 15 20 25 30 35 40
Expand Down

0 comments on commit 6d366bc

Please sign in to comment.