More progress.
This commit is contained in:
@@ -40,6 +40,7 @@ export module CommonDataTypes {
|
||||
export interface TreeNode<TExtraData> {
|
||||
id: string | undefined;
|
||||
name: string;
|
||||
value: string | undefined;
|
||||
children: TreeNode<TExtraData>[];
|
||||
data: TExtraData;
|
||||
isMarked: boolean;
|
||||
|
||||
@@ -3,8 +3,9 @@ import { ExtractedData } from "../DataExtractionResult";
|
||||
export interface DataExtractor<T extends ExtractedData> {
|
||||
id: string;
|
||||
getExtractions(
|
||||
data: any,
|
||||
extractionCollector: ExtractionCollector<T>
|
||||
data: unknown,
|
||||
extractionCollector: ExtractionCollector<T>,
|
||||
evalFn: <TEval>(expression: string) => TEval
|
||||
): void;
|
||||
}
|
||||
|
||||
@@ -19,3 +20,18 @@ export interface DataExtraction<TData extends ExtractedData> {
|
||||
name: string;
|
||||
extractData(): TData;
|
||||
}
|
||||
|
||||
export function getExtractions<T extends ExtractedData>(
|
||||
extractor: DataExtractor<T>,
|
||||
data: unknown,
|
||||
evalFn: <T>(expression: string) => T
|
||||
): DataExtraction<T>[] {
|
||||
const extractions = new Array<DataExtraction<T>>();
|
||||
const collector: ExtractionCollector<T> = {
|
||||
addExtraction(extraction) {
|
||||
extractions.push(extraction);
|
||||
},
|
||||
};
|
||||
extractor.getExtractions(data, collector, evalFn);
|
||||
return extractions;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ export interface DataExtractorApi {
|
||||
): void;
|
||||
getData(
|
||||
value: unknown,
|
||||
evalFn: <T>(expression: string) => T,
|
||||
preferredDataExtractorId: string | undefined
|
||||
): JSONString<DataResult>;
|
||||
}
|
||||
@@ -54,7 +55,7 @@ export function selfContainedInitDataExtractorApi(): boolean {
|
||||
registerExtractor(extractor) {
|
||||
obj[prefix + extractor.id] = extractor;
|
||||
},
|
||||
getData(value, preferredDataExtractorId) {
|
||||
getData(value, evalFn, preferredDataExtractorId) {
|
||||
const extractions = new Array<DataExtraction<ExtractedData>>();
|
||||
const collector: ExtractionCollector<ExtractedData> = {
|
||||
addExtraction(extraction) {
|
||||
@@ -62,7 +63,7 @@ export function selfContainedInitDataExtractorApi(): boolean {
|
||||
},
|
||||
};
|
||||
for (const e of getExtractors()) {
|
||||
e.getExtractions(value, collector);
|
||||
e.getExtractions(value, collector, evalFn);
|
||||
}
|
||||
extractions.sort((a, b) => b.priority - a.priority);
|
||||
let usedExtraction = extractions[0];
|
||||
@@ -113,7 +114,7 @@ export function selfContainedInitDataExtractorApi(): boolean {
|
||||
kind: {
|
||||
text: true,
|
||||
},
|
||||
text: data.toString(),
|
||||
text: "" + data,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -7,13 +7,16 @@ export class TypeScriptAstDataExtractor
|
||||
readonly id = "TypeScriptAst";
|
||||
|
||||
getExtractions(
|
||||
data: any,
|
||||
collector: ExtractionCollector<CommonDataTypes.AstData>
|
||||
data: unknown,
|
||||
collector: ExtractionCollector<CommonDataTypes.AstData>,
|
||||
evalFn: <TEval>(expression: string) => TEval
|
||||
): void {
|
||||
if (typeof data !== "object" || data === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const require = evalFn<(request: string) => unknown>("require");
|
||||
|
||||
const tsApi = require("typescript") as typeof ts;
|
||||
if (!tsApi) {
|
||||
return;
|
||||
@@ -61,6 +64,12 @@ export class TypeScriptAstDataExtractor
|
||||
})
|
||||
.filter(c => c !== null);
|
||||
|
||||
let value: string | undefined = undefined;
|
||||
|
||||
if (tsApi.isIdentifier(node)) {
|
||||
value = node.text;
|
||||
}
|
||||
|
||||
return {
|
||||
name: name,
|
||||
id: memberName,
|
||||
@@ -70,6 +79,7 @@ export class TypeScriptAstDataExtractor
|
||||
position: 0,
|
||||
},
|
||||
isMarked: node === marked,
|
||||
value,
|
||||
// startPos: node.pos,
|
||||
// endPos: node.end
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user