Skip to content
This repository was archived by the owner on May 22, 2025. It is now read-only.

Commit 5988baf

Browse files
committed
small refactoring
1 parent 2294eea commit 5988baf

File tree

4 files changed

+33
-26
lines changed

4 files changed

+33
-26
lines changed

src/transformer.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as es5processor from './es5processor';
1515
import {ModulesManifest} from './modules_manifest';
1616
import {containsInlineSourceMap, extractInlineSourceMap, parseSourceMap, removeInlineSourceMap, setInlineSourceMap, SourceMapper, SourcePosition} from './source_map_utils';
1717
import {createTransformerFromSourceMap} from './transformer_sourcemap';
18-
import {createOriginalNodeTypeChecker, firstTransform, lastTransform, preTypeScriptTransform} from './transformer_util';
18+
import {createCustomTransformers, createOriginalNodeTypeChecker} from './transformer_util';
1919
import * as tsickle from './tsickle';
2020

2121
export interface TransformerOptions extends es5processor.Es5ProcessorOptions, tsickle.Options {
@@ -117,11 +117,10 @@ export function emitWithTsickle(
117117
afterTsTransformers.push(...customTransformers.afterTs);
118118
}
119119
}
120-
121-
// add transformers that have a fixed positions
122-
beforeTsTransformers.unshift(firstTransform);
123-
beforeTsTransformers.push(preTypeScriptTransform);
124-
afterTsTransformers.push(lastTransform);
120+
customTransformers = createCustomTransformers({
121+
before: beforeTsTransformers.map(tf => skipTransformForSourceFileIfNeeded(host, tf)),
122+
after: afterTsTransformers.map(tf => skipTransformForSourceFileIfNeeded(host, tf))
123+
});
125124

126125
const writeFileDelegate = writeFile || tsHost.writeFile.bind(tsHost);
127126
const modulesManifest = new ModulesManifest();
@@ -142,11 +141,8 @@ export function emitWithTsickle(
142141
writeFileDelegate(fileName, content, writeByteOrderMark, onError, sourceFiles);
143142
};
144143

145-
const {diagnostics: tsDiagnostics, emitSkipped, emittedFiles} =
146-
program.emit(targetSourceFile, writeFileImpl, cancellationToken, emitOnlyDtsFiles, {
147-
before: beforeTsTransformers.map(tf => skipTransformForSourceFileIfNeeded(host, tf)),
148-
after: afterTsTransformers.map(tf => skipTransformForSourceFileIfNeeded(host, tf))
149-
});
144+
const {diagnostics: tsDiagnostics, emitSkipped, emittedFiles} = program.emit(
145+
targetSourceFile, writeFileImpl, cancellationToken, emitOnlyDtsFiles, customTransformers);
150146

151147
const externs: {[fileName: string]: string} = {};
152148
if (options.transformTypesToClosure) {

src/transformer_sourcemap.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,9 @@ export function createTransformerFromSourceMap(
3636
// regarding comment synthesization
3737
// - types: as they are not emited anyways
3838
// and it leads to errors with `extends` cases.
39-
if (originalNode && (
40-
isLiteralKind(node.kind) ||
41-
node.kind === ts.SyntaxKind.Identifier ||
42-
isTypeNodeKind(node.kind) ||
43-
node.kind === ts.SyntaxKind.IndexSignature
44-
)) {
39+
if (originalNode &&
40+
(isLiteralKind(node.kind) || node.kind === ts.SyntaxKind.Identifier ||
41+
isTypeNodeKind(node.kind) || node.kind === ts.SyntaxKind.IndexSignature)) {
4542
return originalNode;
4643
}
4744
node = visitEachChild(node, visitNode, context);

src/transformer_util.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,27 @@
99
import * as ts from 'typescript';
1010
import * as tsickle from './tsickle';
1111

12+
/**
13+
* Adjusts the given CustomTransformers with additional transformers
14+
* to fix bugs in TypeScript.
15+
* @param given A
16+
*/
17+
export function createCustomTransformers(given: ts.CustomTransformers): ts.CustomTransformers {
18+
if (!given.after && !given.before) {
19+
return given;
20+
}
21+
const before = given.before || [];
22+
before.unshift(firstTransform);
23+
before.push(preTypeScriptTransform);
24+
const after = given.after || [];
25+
after.unshift(postTypescriptTransform);
26+
return {before, after};
27+
}
28+
1229
/**
1330
* Transform to be used as first transform to reset some state.
1431
*/
15-
export function firstTransform(context: ts.TransformationContext) {
32+
function firstTransform(context: ts.TransformationContext) {
1633
return (sourceFile: ts.SourceFile) => {
1734
(context as TransformationContext).fileContext = new FileContext(sourceFile);
1835
return sourceFile;
@@ -58,7 +75,7 @@ class FileContext {
5875
*
5976
* This prepares the node tree to workaround some bugs in the TypeScript emitter.
6077
*/
61-
export function preTypeScriptTransform(context: ts.TransformationContext) {
78+
function preTypeScriptTransform(context: ts.TransformationContext) {
6279
return (sourceFile: ts.SourceFile) => {
6380
const fileCtx = assertFileContext(context, sourceFile);
6481

@@ -118,7 +135,7 @@ export function preTypeScriptTransform(context: ts.TransformationContext) {
118135
* This fixes places where the TypeScript transformer does not
119136
* emit synthetic comments.
120137
*/
121-
export function lastTransform(context: ts.TransformationContext) {
138+
function postTypescriptTransform(context: ts.TransformationContext) {
122139
return (sourceFile: ts.SourceFile) => {
123140
const fileContext = assertFileContext(context, sourceFile);
124141
const nodePath: ts.Node[] = [];

test/transformer_util_test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {expect} from 'chai';
1010
import * as ts from 'typescript';
1111

12-
import {firstTransform, lastTransform, preTypeScriptTransform, visitEachChild, visitNodeWithSynthesizedComments} from '../src/transformer_util';
12+
import {createCustomTransformers, visitEachChild, visitNodeWithSynthesizedComments} from '../src/transformer_util';
1313

1414
import * as testSupport from './test_support';
1515

@@ -20,10 +20,7 @@ describe('transformer util', () => {
2020
tsSources: {[fileName: string]: string},
2121
transform: ts.TransformerFactory<ts.SourceFile>): {[fileName: string]: string} {
2222
const {program, host} = testSupport.createProgramAndHost(objectToMap(tsSources));
23-
const transformers: ts.CustomTransformers = {
24-
before: [firstTransform, transform, preTypeScriptTransform],
25-
after: [lastTransform]
26-
};
23+
const transformers = createCustomTransformers({before: [transform]});
2724
const jsSources: {[fileName: string]: string} = {};
2825
program.emit(undefined, (fileName: string, data: string) => {
2926
jsSources[fileName] = data;
@@ -41,7 +38,7 @@ describe('transformer util', () => {
4138

4239
// TODO: add tests for all kinds of comments!
4340
// -> verify the hacks I added in transformer_utils!
44-
it.only('should synthesize comments', () => {
41+
it('should synthesize comments', () => {
4542
function transformComments(context: ts.TransformationContext) {
4643
return (sourceFile: ts.SourceFile): ts.SourceFile => {
4744
return visitNode(sourceFile);

0 commit comments

Comments
 (0)