Skip to content

Commit

Permalink
feat: support customTransformers
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Apr 16, 2019
1 parent 6dc744b commit 9bc8ecd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function emitFile(
writePunctuation
};

const transform = ts.transformNodes(resolver, undefined, {}, [sourceFile], transformers!, /*allowDtsFiles*/ false);
const transform = ts.transformNodes(resolver, undefined, {}, [sourceFile], transformers!, /* allowDtsFiles */ false);

state.sourceFile = (transform.transformed[0] as SourceFile);

Expand Down
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const defaultOptions = {
emitHeader: true,
getModulePath: name => name,
getModuleNamespace: () => '\\',
modules: {}
modules: {},
customTransformers: []
};

export function compile(filePath: string, options: Ts2phpOptions = {}) {
Expand Down Expand Up @@ -103,8 +104,10 @@ export function compile(filePath: string, options: Ts2phpOptions = {}) {
};
});

const transformers: ts.TransformerFactory<ts.SourceFile | ts.Bundle>[] = [];
transformers.push(transform);
const transformers: ts.TransformerFactory<ts.SourceFile | ts.Bundle>[] = [
transform,
...(options.customTransformers || [])
];

const emitResolver = program.getDiagnosticsProducingTypeChecker()
.getEmitResolver(sourceFile, undefined);
Expand Down
3 changes: 2 additions & 1 deletion src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* @file 生成代码前,对 ast 进行转换
* @author meixuguang
* @author meixuguang
*/

import ts from 'typescript';

export function transform(context: ts.TransformationContext) {

const {
startLexicalEnvironment
} = context;
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export interface Ts2phpOptions {
getNamespace?: () => string;
getModulePath?: (name: string, module?: ts.ResolvedModuleFull) => string;
getModuleNamespace?: (name: string, module?: ts.ResolvedModuleFull) => string;
tsConfig?: object
tsConfig?: object,
customTransformers?: ts.TransformerFactory<ts.SourceFile | ts.Bundle>[]
}

export interface CompilerState extends Ts2phpOptions {
Expand Down

0 comments on commit 9bc8ecd

Please sign in to comment.