Skip to content

Commit 0d27286

Browse files
committed
Use UASTv2 in the frontend
Signed-off-by: Carlos Martín <carlos.martin.sanchez@gmail.com>
1 parent ae2fbab commit 0d27286

5 files changed

Lines changed: 28 additions & 15 deletions

File tree

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react-split-pane": "^0.1.77",
1818
"react-switch": "^3.0.4",
1919
"react-table": "^6.8.2",
20-
"uast-viewer": "^0.0.8"
20+
"uast-viewer": "^0.2.0"
2121
},
2222
"scripts": {
2323
"start": "react-app-rewired start",

frontend/src/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function parseCode(language, content, filter, customServerUrl) {
145145
serverUrl: customServerUrl
146146
}
147147
}).then(res => {
148-
if (res.data.status !== 0) {
148+
if (res.status !== 200) {
149149
throw normalizeErrors(res.data.errors);
150150
}
151151
return res.data.uast;

frontend/src/components/UASTViewer.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
3-
import { transformer } from 'uast-viewer';
3+
import { expandRootIds, uastV2 } from 'uast-viewer';
44
import UASTViewerPane from './UASTViewerPane';
55
import api from '../api';
66
import CloseIcon from '../icons/close-query-tab.svg';
77

8+
// Same values as the ones applied by withUASTEditor in CodeViewer.js
9+
// https://github.com/bblfsh/uast-viewer/blob/v0.2.0/src/withUASTEditor.js#L208
10+
const ROOT_IDS = [1];
11+
const LEVELS_EXPAND = 2;
12+
813
class UASTViewer extends Component {
914
constructor(props) {
1015
super(props);
1116

1217
this.state = {
1318
loading: false,
14-
uast: transformer({
15-
InternalType: 'Search results',
16-
Children: props.uast
17-
}),
19+
uast: this.transform(props.uast),
1820
showLocations: false,
1921
filter: '',
2022
error: null
@@ -40,12 +42,24 @@ class UASTViewer extends Component {
4042
api
4143
.filterUAST(this.props.protobufs, this.state.filter)
4244
.then(uast => {
43-
this.setState({ uast: transformer(uast) });
45+
this.setState({ uast: this.transform(uast) });
4446
})
4547
.catch(err => this.setState({ uast: null, error: err }))
4648
.then(() => this.setState({ loading: false }));
4749
}
4850

51+
// Applies the uast-viewer object shape transformer, and expands the first
52+
// 2 levels
53+
transform(uast) {
54+
const flatUAST = uastV2.transformer(uast);
55+
return expandRootIds(
56+
flatUAST,
57+
ROOT_IDS,
58+
LEVELS_EXPAND,
59+
uastV2.getChildrenIds
60+
);
61+
}
62+
4963
removeError() {
5064
this.setState({ error: null });
5165
}

frontend/src/components/UASTViewerPane.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Button } from 'react-bootstrap';
55
import './UASTViewerPane.less';
66

77
const ROOT_ID = 1;
8-
const SEARCH_RESULTS_TYPE = 'Search results';
98

109
function getSearchResults(uast) {
1110
if (!uast) {
@@ -17,8 +16,8 @@ function getSearchResults(uast) {
1716
return null;
1817
}
1918

20-
if (rootNode.InternalType === SEARCH_RESULTS_TYPE) {
21-
return rootNode.Children;
19+
if (Array.isArray(rootNode.n)) {
20+
return rootNode.n.map(c => c.id);
2221
}
2322

2423
return null;

frontend/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8059,10 +8059,10 @@ ua-parser-js@^0.7.18:
80598059
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.18.tgz#a7bfd92f56edfb117083b69e31d2aa8882d4b1ed"
80608060
integrity sha512-LtzwHlVHwFGTptfNSgezHp7WUlwiqb0gA9AALRbKaERfxwJoiX0A73QbTToxteIAuIaFshhgIZfqK8s7clqgnA==
80618061

8062-
uast-viewer@^0.0.8:
8063-
version "0.0.8"
8064-
resolved "https://registry.yarnpkg.com/uast-viewer/-/uast-viewer-0.0.8.tgz#e0e4c5bcad6bf3d62dcc48800a4350d800a01987"
8065-
integrity sha1-4OTFvK1r89YtzEiACkNQ2ACgGYc=
8062+
uast-viewer@^0.2.0:
8063+
version "0.2.0"
8064+
resolved "https://registry.yarnpkg.com/uast-viewer/-/uast-viewer-0.2.0.tgz#081cc7f2f67bf5db03dc53cd8a451f491c49e699"
8065+
integrity sha512-BTYLYY6xSu0ITjC4lO2Kb14Nw4oYKh7i3mChf4HygNJRTJ93FHaYTmyqS38cA3+1FsGnEyKSz+BwATGONLJPDQ==
80668066

80678067
uglify-js@3.4.x, uglify-js@^3.0.13:
80688068
version "3.4.3"

0 commit comments

Comments
 (0)