Skip to content

Commit

Permalink
refactor(PreOrderCursorIterator): use generator delegation
Browse files Browse the repository at this point in the history
Refs #691
  • Loading branch information
char0n committed Nov 29, 2021
1 parent f672afd commit 756a597
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,20 @@ class PreOrderCursorIterator {
}

public *[Symbol.iterator]() {
let reachedRoot = false;
// @ts-ignore
const method = this[this.cursor.nodeType];
const constructor = this.constructor as any;

while (!reachedRoot) {
// @ts-ignore
const method = this[this.cursor.nodeType];
yield (method || this.createNode).call(this) as SyntaxNodeSurrogate;

yield (method || this.createNode).call(this) as SyntaxNodeSurrogate;
if (this.cursor.gotoFirstChild()) {
yield* new constructor(this.cursor);

if (this.cursor.gotoFirstChild()) {
continue; // eslint-disable-line no-continue
while (this.cursor.gotoNextSibling()) {
yield* new constructor(this.cursor);
}

if (this.cursor.gotoNextSibling()) {
continue; // eslint-disable-line no-continue
}

let retracting = true;
while (retracting) {
if (!this.cursor.gotoParent()) {
retracting = false;
reachedRoot = true;
}

if (this.cursor.gotoNextSibling()) {
retracting = false;
}
}
this.cursor.gotoParent();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { sexprs } from '@swagger-api/apidom-core';

import { lexicalAnalysis, syntacticAnalysisDirect } from '../../src/adapter-node';
import PreOrderCursorChildrenIterator from '../../src/syntactic-analysis/PreOrderCursorChildrenIterator';
import PreOrderCursorIterator from '../../src/syntactic-analysis/PreOrderCusrorIterator';

describe('syntactic-analysis', function () {
context('PreOrderCursorChildrenIterator', function () {
Expand All @@ -17,15 +16,4 @@ describe('syntactic-analysis', function () {
expect(sexprs(apiDOM)).toMatchSnapshot();
});
});

context('PreOrderCursorIterator', function () {
specify('should create optimized list of surrogate syntax nodes', async function () {
const cst = await lexicalAnalysis('[1, 2]');
const cursor = cst.walk();
const iterator = new PreOrderCursorIterator(cursor);
const optimizedList = [...iterator];

expect(optimizedList).to.have.lengthOf(7);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { assert } from 'chai';

import { lexicalAnalysis } from '../../src/adapter-node';
import PreOrderCursorIterator from '../../src/syntactic-analysis/PreOrderCusrorIterator';

describe('syntactic-analysis', function () {
context('PreOrderCursorIterator', function () {
specify('should create optimized list of surrogate syntax nodes', async function () {
const cst = await lexicalAnalysis('{"a":"b"}');
const cursor = cst.walk();
const iterator = new PreOrderCursorIterator(cursor);
const optimizedList = [...iterator];

assert.lengthOf(optimizedList, 14);
});
});
});

0 comments on commit 756a597

Please sign in to comment.