Avevo letto "tutto internet", ma non ho trovato alcun esempio sull'ottenere l'albero di sintassi (proprio come in Esprima) da TypeScrypt source. Voglio dire come posso ottenere oggetto come questo (Esprima Parser esempio)TypeScript: get syntax tree
{
"type": "Program",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "answer"
},
"init": {
"type": "BinaryExpression",
"operator": "*",
"left": {
"type": "Literal",
"value": 6,
"raw": "6"
},
"right": {
"type": "Literal",
"value": 7,
"raw": "7"
}
}
}
],
"kind": "var"
}
]
}
dal codice javascript
var answer = 6 * 7;
solo per il testo dattiloscritto fonte?
P.S. Spero molto per il tuo aiuto, perché non voglio scrivere la tua terribile bicicletta)
P.P.S. Credo che i file typescript.ts lib (js) e typescriptServices.ts (js) per aiutare me, ma io non so come :(
Risolto
Grazie mille per l'utente Steve . Fenton Ecco il mio codice, se chiunque sia interessato a:.
// uses
var typeScriptLS = new Harness.TypeScriptLS();
var ServicesFactory = new Services.TypeScriptServicesFactory();
var serviceShim = ServicesFactory.createLanguageServiceShim(typeScriptLS);
// add lib.d.ts
var _libText = window.document.getElementById('lib.d.ts').innerText;
typeScriptLS.addScript('lib.d.ts', _libText.replace(/\r\n?/g,"\n"), true);
// add greeter.ts
var _sourceText = window.document.getElementById('greeter.ts').innerText;
typeScriptLS.addScript('greeter.ts', _sourceText.replace(/\r\n?/g,"\n"), true);
// script name
var _scriptName = 'greeter.ts';
// get syntax tree
var _st = serviceShim.languageService.getSyntaxTree(_scriptName);
//console.log(_st);
console.log(JSON.stringify(_st, "", 2));
casi in cui è 'Harness' definito nei file distribuiti? Non vedo "Harness" ovunque in tsc.js, typescript.js o typescriptServices.js. O più in generale, come si esegue questo esempio di codice risolto? –
Mi dispiace se stavate aspettando./src/harness/ in https://github.com/Microsoft/TypeScript. Ma ora il mio codice potrebbe diventare non valido – bukvaG
Possibile duplicato di [Come possiamo ottenere l'albero della sintassi di TypeScript?] (Http://stackoverflow.com/questions/18714501/how-can-we-get-the-syntax-tree- di dattiloscritto) – ColinE