typescript-compiler-api

TS compiler API: prevent Export. to appear on nodes where there is not a export modifier

本秂侑毒 提交于 2021-02-10 17:35:33
问题 in js an exported variable is declared like: "Exports.my_var = cool_value " , when using the Typyscript compiler api i have some varibales that originally have the export modifier like: export let a = 1; a = a + 1; that resolves in Exports.a = 1; Exports.a = Exports.a + 1; but i don't want them to be exported in the js file, i tried removing the export modifiers on the nodes, but they are still compiled with the Exports. [UPDATE] some example code Transformer: ast = VisitNode(ast,function() {

How to add new property to a TypeScript class with TypeScript compiler API?

馋奶兔 提交于 2021-02-10 14:14:54
问题 I try to add new propery to my awesome.model.ts file. The original content is like this: import { TagInterface, TagUIFieldsInterface } from './tag.interface'; export class Tag implements TagInterface { readonly api_endpoint = '/tag'; id: ID; name: string; fields: FieldContainerInterface<TagUIFieldsInterface> = { // ... }; init(data?: any): TagInterface { // ... } } I want to add a new propery color_code: string; after the name property's line. To look like this: import { TagInterface,

TypeScript Compiler API: How to get type with resolved type arguments?

二次信任 提交于 2021-01-28 08:10:14
问题 I want to merge class declarations in a .dt.s file to generate a cleaner public API. I am stuck on how to make this work with generic type arguments. Let's say I have: class A1<T> { // Non-exported class I want to hide data?: T; } export class B1 extends A1<string> { } Ideally, I want to turn this into: export class B1 { data?: string; } I can get the type of A1 and then copy its members. But how do get a resolved version of A1 that uses string instead of T ? For reference, this is my current

How can I inject additional statements into a function using ts.transform

偶尔善良 提交于 2020-06-27 10:57:31
问题 I'm using the Typescript compiler API (ts.transform, ts.updateFunctionDeclaration) to inject additional statements at the beginning of functions in existing source files. This works great, except that when I print the transformed code (using ts.Printer) the first comment in the original source is emitted above my injected statements. How can I prevent that happening? I've tried various ways of constructing the new nodes returned by the transformer, but nothing changes the output. Here's my

How can I inject additional statements into a function using ts.transform

徘徊边缘 提交于 2020-06-27 10:57:08
问题 I'm using the Typescript compiler API (ts.transform, ts.updateFunctionDeclaration) to inject additional statements at the beginning of functions in existing source files. This works great, except that when I print the transformed code (using ts.Printer) the first comment in the original source is emitted above my injected statements. How can I prevent that happening? I've tried various ways of constructing the new nodes returned by the transformer, but nothing changes the output. Here's my

TypeScript Transformer - Follow Module Imports

安稳与你 提交于 2020-05-24 05:18:45
问题 I really would like to know how I could follow module imports in a custom typescript transformer. As a matter of fact I would like to achieve the following: I would like to find a function call like e.g. transform(PlaygroundComponent) And then I would like to find the corresponding class (PlaygroundComponent) retrieved as an argument of the function call and adjust/transform it. This class may be imported from another file. I am really wondering how I could achieve this. Currently I do not