Rename AstItem -> ChildAstItem and RootAstItem -> AstItem

This simplifies some of our type conversions since all ChildAstItem
are AstItem.
This commit is contained in:
Andrew Eisenberg
2020-10-07 15:40:33 -07:00
parent 732eb83d07
commit c83d1b305e
3 changed files with 27 additions and 25 deletions

View File

@@ -20,21 +20,23 @@ import { UrlValue, BqrsId } from './bqrs-cli-types';
import { showLocation } from './interface-utils';
import { isStringLoc, isWholeFileLoc, isLineColumnLoc } from './bqrs-utils';
export interface AstItem {
id: BqrsId;
label?: string;
location?: UrlValue;
fileLocation?: Location;
parent: AstItem | RootAstItem;
children: AstItem[];
children: ChildAstItem[];
order: number;
}
export type RootAstItem = Omit<AstItem, 'parent'>;
export interface ChildAstItem extends AstItem {
parent: ChildAstItem | AstItem;
}
class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
class AstViewerDataProvider implements TreeDataProvider<AstItem> {
public roots: RootAstItem[] = [];
public roots: AstItem[] = [];
public db: DatabaseItem | undefined;
private _onDidChangeTreeData =
@@ -52,13 +54,13 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
refresh(): void {
this._onDidChangeTreeData.fire();
}
getChildren(item?: AstItem): ProviderResult<(AstItem | RootAstItem)[]> {
getChildren(item?: AstItem): ProviderResult<AstItem[]> {
const children = item ? item.children : this.roots;
return children.sort((c1, c2) => (c1.order - c2.order));
}
getParent(item: AstItem): ProviderResult<AstItem> {
return item.parent as AstItem;
getParent(item: ChildAstItem): ProviderResult<AstItem> {
return item.parent;
}
getTreeItem(item: AstItem): TreeItem {
@@ -96,7 +98,7 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
}
export class AstViewer {
private treeView: TreeView<AstItem | RootAstItem>;
private treeView: TreeView<AstItem>;
private treeDataProvider: AstViewerDataProvider;
private currentFile: string | undefined;
@@ -114,7 +116,7 @@ export class AstViewer {
ctx.subscriptions.push(window.onDidChangeTextEditorSelection(this.updateTreeSelection, this));
}
updateRoots(roots: RootAstItem[], db: DatabaseItem, fileName: string) {
updateRoots(roots: AstItem[], db: DatabaseItem, fileName: string) {
this.treeDataProvider.roots = roots;
this.treeDataProvider.db = db;
this.treeDataProvider.refresh();
@@ -132,12 +134,12 @@ export class AstViewer {
// range that contains the selection.
// Some nodes do not have a location, but their children might, so must
// recurse though location-less AST nodes to see if children are correct.
function findBest(selectedRange: Range, items?: RootAstItem[]): RootAstItem | undefined {
function findBest(selectedRange: Range, items?: AstItem[]): AstItem | undefined {
if (!items || !items.length) {
return;
}
for (const item of items) {
let candidate: RootAstItem | undefined = undefined;
let candidate: AstItem | undefined = undefined;
if (isInside(selectedRange, item.fileLocation?.range)) {
candidate = item;
}

View File

@@ -2,7 +2,7 @@ import { QueryWithResults } from '../run-queries';
import { CodeQLCliServer } from '../cli';
import { DecodedBqrsChunk, BqrsId, EntityValue } from '../bqrs-cli-types';
import { DatabaseItem } from '../databases';
import { AstItem, RootAstItem } from '../astViewer';
import { ChildAstItem, AstItem } from '../astViewer';
import fileRangeFromURI from './fileRangeFromURI';
/**
@@ -11,7 +11,7 @@ import fileRangeFromURI from './fileRangeFromURI';
*/
export default class AstBuilder {
private roots: RootAstItem[] | undefined;
private roots: AstItem[] | undefined;
private bqrsPath: string;
constructor(
queryResults: QueryWithResults,
@@ -22,14 +22,14 @@ export default class AstBuilder {
this.bqrsPath = queryResults.query.resultsPaths.resultsPath;
}
async getRoots(): Promise<RootAstItem[]> {
async getRoots(): Promise<AstItem[]> {
if (!this.roots) {
this.roots = await this.parseRoots();
}
return this.roots;
}
private async parseRoots(): Promise<RootAstItem[]> {
private async parseRoots(): Promise<AstItem[]> {
const options = { entities: ['id', 'url', 'string'] };
const [nodeTuples, edgeTuples, graphProperties] = await Promise.all([
await this.cli.bqrsDecode(this.bqrsPath, 'nodes', options),
@@ -41,7 +41,7 @@ export default class AstBuilder {
throw new Error('AST is invalid');
}
const idToItem = new Map<BqrsId, AstItem | RootAstItem>();
const idToItem = new Map<BqrsId, AstItem>();
const parentToChildren = new Map<BqrsId, BqrsId[]>();
const childToParent = new Map<BqrsId, BqrsId>();
const astOrder = new Map<BqrsId, number>();
@@ -89,21 +89,21 @@ export default class AstBuilder {
label: entity.label,
location: entity.url,
fileLocation: fileRangeFromURI(entity.url, this.db),
children: [] as AstItem[],
children: [] as ChildAstItem[],
order: Number.MAX_SAFE_INTEGER
};
idToItem.set(id, item as RootAstItem);
idToItem.set(id, item);
const parent = idToItem.get(childToParent.has(id) ? childToParent.get(id)! : -1);
if (parent) {
const astItem = item as unknown as AstItem;
(astItem).parent = parent;
const astItem = item as ChildAstItem;
astItem.parent = parent;
parent.children.push(astItem);
}
const children = parentToChildren.has(id) ? parentToChildren.get(id)! : [];
children.forEach(childId => {
const child = idToItem.get(childId) as AstItem | undefined;
const child = idToItem.get(childId) as ChildAstItem | undefined;
if (child) {
child.parent = item;
item.children.push(child);

View File

@@ -4,7 +4,7 @@ import * as chaiAsPromised from 'chai-as-promised';
import * as sinon from 'sinon';
import * as yaml from 'js-yaml';
import { AstViewer, RootAstItem } from '../../astViewer';
import { AstViewer, AstItem } from '../../astViewer';
import { ExtensionContext, commands, Range } from 'vscode';
import { DatabaseItem } from '../../databases';
@@ -14,7 +14,7 @@ const expect = chai.expect;
describe('AstViewer', () => {
let astRoots: RootAstItem[];
let astRoots: AstItem[];
let viewer: AstViewer;
beforeEach(async () => {
// the ast is stored in yaml because there are back pointers
@@ -125,7 +125,7 @@ describe('AstViewer', () => {
}
async function buildAst() {
const astRoots = yaml.safeLoad(await fs.readFile(`${__dirname}/../../../src/vscode-tests/no-workspace/data/astViewer.yml`, 'utf8')) as RootAstItem[];
const astRoots = yaml.safeLoad(await fs.readFile(`${__dirname}/data/astViewer.yml`, 'utf8')) as AstItem[];
// convert range properties into vscode.Range instances
function convertToRangeInstances(obj: any) {