Skip to content

Commit

Permalink
fix(typescript): emit code/json not code facts; remove proto dependen…
Browse files Browse the repository at this point in the history
…cy (#5494)
  • Loading branch information
jaysachs committed Jan 12, 2023
1 parent 0a9814e commit 2996b79
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
2 changes: 0 additions & 2 deletions kythe/typescript/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ ts_library(
tsconfig = ":tsconfig",
deps = [
":kythe",
"//kythe/proto:common_ts_proto",
"@npm//@types/node",
"@npm//google-protobuf",
"@npm//typescript",
],
)
Expand Down
40 changes: 10 additions & 30 deletions kythe/typescript/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as ts from 'typescript';

import {MarkedSource} from '../proto/common_pb';
import {EdgeKind, FactName, JSONEdge, JSONFact, makeOrdinalEdge, NodeKind, OrdinalEdge, Subkind, VName} from './kythe';
import {EdgeKind, FactName, JSONEdge, JSONFact, JSONMarkedSource, makeOrdinalEdge, MarkedSourceKind, NodeKind, OrdinalEdge, Subkind, VName} from './kythe';
import * as utf8 from './utf8';

const LANGUAGE = 'typescript';
Expand Down Expand Up @@ -729,25 +728,6 @@ function fmtMarkedSource(s: string) {
return s;
}

function makeMarkedSource({
kind,
preText,
postText,
childList,
}: {
kind?: keyof typeof MarkedSource.Kind,
preText?: string,
postText?: string,
childList?: MarkedSource[]
}): MarkedSource {
const ms = new MarkedSource();
if (kind !== undefined) ms.setKind(MarkedSource.Kind[kind]);
if (preText !== undefined) ms.setPreText(fmtMarkedSource(preText));
if (postText !== undefined) ms.setPostText(fmtMarkedSource(postText));
if (childList !== undefined) ms.setChildList(childList);
return ms;
}

function isNonNullableArray<T>(arr: Array<T>): arr is Array<NonNullable<T>> {
return arr.findIndex(el => el === undefined || el === null) === -1;
}
Expand Down Expand Up @@ -1712,7 +1692,7 @@ class Visitor {
decl: ts.VariableDeclaration|ts.PropertyAssignment|
ts.PropertyDeclaration|ts.BindingElement|ts.ShorthandPropertyAssignment,
declVName: VName) {
const codeParts: MarkedSource[] = [];
const codeParts: JSONMarkedSource[] = [];
const initializerList = decl.parent;
let varDecl;
const bindingPath: Array<string|number|undefined> = [];
Expand Down Expand Up @@ -1749,12 +1729,12 @@ class Visitor {
}
const ty = this.typeChecker.getTypeAtLocation(decl);
const tyStr = this.typeChecker.typeToString(ty, decl);
codeParts.push(makeMarkedSource({kind: 'CONTEXT', preText: declKw}));
codeParts.push(makeMarkedSource({kind: 'BOX', preText: ' '}));
codeParts.push({kind: MarkedSourceKind.CONTEXT, pre_text: fmtMarkedSource(declKw)});
codeParts.push({kind: MarkedSourceKind.BOX, pre_text: ' '});
codeParts.push(
makeMarkedSource({kind: 'IDENTIFIER', preText: decl.name.getText()}));
{kind: MarkedSourceKind.IDENTIFIER, pre_text: fmtMarkedSource(decl.name.getText())});
codeParts.push(
makeMarkedSource({kind: 'TYPE', preText: ': ', postText: tyStr}));
{kind: MarkedSourceKind.TYPE, pre_text: ': ', post_text: fmtMarkedSource(tyStr)});
if ('initializer' in varDecl && varDecl.initializer) {
let init: ts.Node = varDecl.initializer;

Expand All @@ -1765,13 +1745,13 @@ class Visitor {
init = narrowedInit || init;
}

codeParts.push(makeMarkedSource({kind: 'BOX', preText: ' = '}));
codeParts.push({kind: MarkedSourceKind.BOX, pre_text: ' = '});
codeParts.push(
makeMarkedSource({kind: 'INITIALIZER', preText: init.getText()}));
{kind: MarkedSourceKind.INITIALIZER, pre_text: fmtMarkedSource(init.getText())});
}

const markedSource = makeMarkedSource({kind: 'BOX', childList: codeParts});
this.emitFact(declVName, FactName.CODE, markedSource.serializeBinary());
const markedSource = ({kind: MarkedSourceKind.BOX, child: codeParts});
this.emitFact(declVName, FactName.CODE_JSON, JSON.stringify(markedSource));
}

/**
Expand Down
43 changes: 42 additions & 1 deletion kythe/typescript/kythe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export enum NodeKind {
*/
export enum FactName {
BUILD_CONFIG = '/kythe/build/config',
CODE = '/kythe/code',
CODE_JSON = '/kythe/code/json',
COMPLETE = '/kythe/complete',
CONTEXT_URL = '/kythe/context/url',
DETAILS = '/kythe/details',
Expand Down Expand Up @@ -232,3 +232,44 @@ export interface JSONEdge {
edge_kind: EdgeKind|OrdinalEdge;
fact_name: '/';
}

/*
* Kythe marked source Linkd expressed in the schema JSON-style encoding.
*/
export interface JSONLink {
definition: string[];
}

/*
* Enum corresponding to Kythe MarkedSource.Kind proto enum.
*/
export enum MarkedSourceKind {
BOX,
TYPE,
PARAMETER,
IDENTIFIER,
CONTEXT,
INITIALIZER,
PARAMETER_LOOKUP_BY_PARAM,
LOOKUP_BY_PARAM,
PARAMETER_LOOKUP_BY_PARAM_WITH_DEFAULTS,
LOOKUP_BY_TYPED,
PARAMETER_LOOKUP_BY_TPARAM,
LOOKUP_BY_TPARAM,
}


/*
* Kythe MarkedSource expressed in the schema JSON-style encoding.
*/
export interface JSONMarkedSource {
kind: MarkedSourceKind;
pre_text?: string;
child?: JSONMarkedSource[];
post_child_text?: string;
post_text?: string;
lookup_index?: number;
default_children_count?: number;
add_final_list_token?: boolean;
link?: JSONLink[];
}
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"jasmine-core": "^3.7.1"
},
"dependencies": {
"google-protobuf": "^3.15.6",
"source-map-support": "^0.5.19",
"typescript": "~3.8"
},
Expand Down

0 comments on commit 2996b79

Please sign in to comment.