Skip to content

Commit c83d1b3

Browse files
committed
Rename AstItem -> ChildAstItem and RootAstItem -> AstItem
This simplifies some of our type conversions since all ChildAstItem are AstItem.
1 parent 732eb83 commit c83d1b3

3 files changed

Lines changed: 27 additions & 25 deletions

File tree

extensions/ql-vscode/src/astViewer.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,23 @@ import { UrlValue, BqrsId } from './bqrs-cli-types';
2020
import { showLocation } from './interface-utils';
2121
import { isStringLoc, isWholeFileLoc, isLineColumnLoc } from './bqrs-utils';
2222

23+
2324
export interface AstItem {
2425
id: BqrsId;
2526
label?: string;
2627
location?: UrlValue;
2728
fileLocation?: Location;
28-
parent: AstItem | RootAstItem;
29-
children: AstItem[];
29+
children: ChildAstItem[];
3030
order: number;
3131
}
3232

33-
export type RootAstItem = Omit<AstItem, 'parent'>;
33+
export interface ChildAstItem extends AstItem {
34+
parent: ChildAstItem | AstItem;
35+
}
3436

35-
class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
37+
class AstViewerDataProvider implements TreeDataProvider<AstItem> {
3638

37-
public roots: RootAstItem[] = [];
39+
public roots: AstItem[] = [];
3840
public db: DatabaseItem | undefined;
3941

4042
private _onDidChangeTreeData =
@@ -52,13 +54,13 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
5254
refresh(): void {
5355
this._onDidChangeTreeData.fire();
5456
}
55-
getChildren(item?: AstItem): ProviderResult<(AstItem | RootAstItem)[]> {
57+
getChildren(item?: AstItem): ProviderResult<AstItem[]> {
5658
const children = item ? item.children : this.roots;
5759
return children.sort((c1, c2) => (c1.order - c2.order));
5860
}
5961

60-
getParent(item: AstItem): ProviderResult<AstItem> {
61-
return item.parent as AstItem;
62+
getParent(item: ChildAstItem): ProviderResult<AstItem> {
63+
return item.parent;
6264
}
6365

6466
getTreeItem(item: AstItem): TreeItem {
@@ -96,7 +98,7 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem | RootAstItem> {
9698
}
9799

98100
export class AstViewer {
99-
private treeView: TreeView<AstItem | RootAstItem>;
101+
private treeView: TreeView<AstItem>;
100102
private treeDataProvider: AstViewerDataProvider;
101103
private currentFile: string | undefined;
102104

@@ -114,7 +116,7 @@ export class AstViewer {
114116
ctx.subscriptions.push(window.onDidChangeTextEditorSelection(this.updateTreeSelection, this));
115117
}
116118

117-
updateRoots(roots: RootAstItem[], db: DatabaseItem, fileName: string) {
119+
updateRoots(roots: AstItem[], db: DatabaseItem, fileName: string) {
118120
this.treeDataProvider.roots = roots;
119121
this.treeDataProvider.db = db;
120122
this.treeDataProvider.refresh();
@@ -132,12 +134,12 @@ export class AstViewer {
132134
// range that contains the selection.
133135
// Some nodes do not have a location, but their children might, so must
134136
// recurse though location-less AST nodes to see if children are correct.
135-
function findBest(selectedRange: Range, items?: RootAstItem[]): RootAstItem | undefined {
137+
function findBest(selectedRange: Range, items?: AstItem[]): AstItem | undefined {
136138
if (!items || !items.length) {
137139
return;
138140
}
139141
for (const item of items) {
140-
let candidate: RootAstItem | undefined = undefined;
142+
let candidate: AstItem | undefined = undefined;
141143
if (isInside(selectedRange, item.fileLocation?.range)) {
142144
candidate = item;
143145
}

extensions/ql-vscode/src/contextual/astBuilder.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { QueryWithResults } from '../run-queries';
22
import { CodeQLCliServer } from '../cli';
33
import { DecodedBqrsChunk, BqrsId, EntityValue } from '../bqrs-cli-types';
44
import { DatabaseItem } from '../databases';
5-
import { AstItem, RootAstItem } from '../astViewer';
5+
import { ChildAstItem, AstItem } from '../astViewer';
66
import fileRangeFromURI from './fileRangeFromURI';
77

88
/**
@@ -11,7 +11,7 @@ import fileRangeFromURI from './fileRangeFromURI';
1111
*/
1212
export default class AstBuilder {
1313

14-
private roots: RootAstItem[] | undefined;
14+
private roots: AstItem[] | undefined;
1515
private bqrsPath: string;
1616
constructor(
1717
queryResults: QueryWithResults,
@@ -22,14 +22,14 @@ export default class AstBuilder {
2222
this.bqrsPath = queryResults.query.resultsPaths.resultsPath;
2323
}
2424

25-
async getRoots(): Promise<RootAstItem[]> {
25+
async getRoots(): Promise<AstItem[]> {
2626
if (!this.roots) {
2727
this.roots = await this.parseRoots();
2828
}
2929
return this.roots;
3030
}
3131

32-
private async parseRoots(): Promise<RootAstItem[]> {
32+
private async parseRoots(): Promise<AstItem[]> {
3333
const options = { entities: ['id', 'url', 'string'] };
3434
const [nodeTuples, edgeTuples, graphProperties] = await Promise.all([
3535
await this.cli.bqrsDecode(this.bqrsPath, 'nodes', options),
@@ -41,7 +41,7 @@ export default class AstBuilder {
4141
throw new Error('AST is invalid');
4242
}
4343

44-
const idToItem = new Map<BqrsId, AstItem | RootAstItem>();
44+
const idToItem = new Map<BqrsId, AstItem>();
4545
const parentToChildren = new Map<BqrsId, BqrsId[]>();
4646
const childToParent = new Map<BqrsId, BqrsId>();
4747
const astOrder = new Map<BqrsId, number>();
@@ -89,21 +89,21 @@ export default class AstBuilder {
8989
label: entity.label,
9090
location: entity.url,
9191
fileLocation: fileRangeFromURI(entity.url, this.db),
92-
children: [] as AstItem[],
92+
children: [] as ChildAstItem[],
9393
order: Number.MAX_SAFE_INTEGER
9494
};
9595

96-
idToItem.set(id, item as RootAstItem);
96+
idToItem.set(id, item);
9797
const parent = idToItem.get(childToParent.has(id) ? childToParent.get(id)! : -1);
9898

9999
if (parent) {
100-
const astItem = item as unknown as AstItem;
101-
(astItem).parent = parent;
100+
const astItem = item as ChildAstItem;
101+
astItem.parent = parent;
102102
parent.children.push(astItem);
103103
}
104104
const children = parentToChildren.has(id) ? parentToChildren.get(id)! : [];
105105
children.forEach(childId => {
106-
const child = idToItem.get(childId) as AstItem | undefined;
106+
const child = idToItem.get(childId) as ChildAstItem | undefined;
107107
if (child) {
108108
child.parent = item;
109109
item.children.push(child);

extensions/ql-vscode/src/vscode-tests/no-workspace/astViewer.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as chaiAsPromised from 'chai-as-promised';
44
import * as sinon from 'sinon';
55
import * as yaml from 'js-yaml';
66

7-
import { AstViewer, RootAstItem } from '../../astViewer';
7+
import { AstViewer, AstItem } from '../../astViewer';
88
import { ExtensionContext, commands, Range } from 'vscode';
99
import { DatabaseItem } from '../../databases';
1010

@@ -14,7 +14,7 @@ const expect = chai.expect;
1414

1515

1616
describe('AstViewer', () => {
17-
let astRoots: RootAstItem[];
17+
let astRoots: AstItem[];
1818
let viewer: AstViewer;
1919
beforeEach(async () => {
2020
// the ast is stored in yaml because there are back pointers
@@ -125,7 +125,7 @@ describe('AstViewer', () => {
125125
}
126126

127127
async function buildAst() {
128-
const astRoots = yaml.safeLoad(await fs.readFile(`${__dirname}/../../../src/vscode-tests/no-workspace/data/astViewer.yml`, 'utf8')) as RootAstItem[];
128+
const astRoots = yaml.safeLoad(await fs.readFile(`${__dirname}/data/astViewer.yml`, 'utf8')) as AstItem[];
129129

130130
// convert range properties into vscode.Range instances
131131
function convertToRangeInstances(obj: any) {

0 commit comments

Comments
 (0)