Skip to content

Commit

Permalink
test: committing incremental compiler before removing it
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Aug 9, 2018
1 parent 2a3da21 commit 2dba4fc
Show file tree
Hide file tree
Showing 10 changed files with 759 additions and 159 deletions.
62 changes: 32 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
// tslint:disable:member-ordering
import { TsJestInstance, TsJestGlobalOptions } from './types';
import TsProgram from './ts-program';
import { TsJestGlobalOptions, TsJestConfig } from './types';
import TsProgram from './ts-program.simple';
import Memoize from './memoize';
import { normalizeDiagnosticTypes } from './utils/diagnostics';

const rootDirFor = (jestConfig: jest.ProjectConfig): string => {
return jestConfig.rootDir || process.cwd();
};

// TODO: could be used if we need to handle the cache key ourself
// const normalizeJestConfig = (jestConfig: jest.ProjectConfig): jest.ProjectConfig => {
// const config = { ...jestConfig, rootDir: rootDirFor(jestConfig) };
// delete config.cacheDirectory;
// delete config.name;
// return config;
// };

class TsJestTransformer implements jest.Transformer {
private _instances = new Map<jest.Path, TsJestInstance>();
@Memoize((_, rootDir) => rootDir)
instanceFor(
jestConfig: jest.ProjectConfig,
rootDir: jest.Path = jestConfig.rootDir || process.cwd(),
): TsJestInstance {
@Memoize()
configFor(jestConfig: jest.ProjectConfig): TsJestConfig {
const { globals = {} } = jestConfig as any;
const options: TsJestGlobalOptions = { ...globals['ts-jest'] };

const shouldWrapHtml = !!globals.__TRANSFORM_HTML__;

let tsProgram: TsProgram;
const instance: TsJestInstance = {
shouldWrapHtml,
get tsProgram(): TsProgram {
return tsProgram || (tsProgram = new TsProgram(rootDir, options));
},
get tsConfig() {
return this.tsProgram.compilerOptions;
},
// TODO: get using babel-jest
return {
inputOptions: options,
useBabelJest: !!options.useBabelJest,
diagnostics: normalizeDiagnosticTypes(options.diagnostics),
};
this._instances.set(rootDir, instance);
return instance;
}

@Memoize()
programFor(jestConfig: jest.ProjectConfig): TsProgram {
const myConfig = this.configFor(jestConfig);
return new TsProgram(rootDirFor(jestConfig), myConfig);
}

process(
Expand All @@ -40,17 +43,16 @@ class TsJestTransformer implements jest.Transformer {
let result: string | jest.TransformedSource;

// get the tranformer instance
const instance = this.instanceFor(jestConfig);
const program = this.programFor(jestConfig);
const config = this.configFor(jestConfig);
const instrument: boolean =
!!transformOptions && transformOptions.instrument;

// transpile TS code (source maps are included)
result = instance.tsProgram.transpileModule(
path,
source,
transformOptions && transformOptions.instrument,
);
result = program.transpileModule(path, source, instrument);

// calling babel-jest transformer
if (instance.useBabelJest) {
if (config.useBabelJest) {
result = this.babelJest.process(
result,
path,
Expand Down
131 changes: 131 additions & 0 deletions src/transformers/hoisting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// import {
// TransformationContext,
// EmitHint,
// Node,
// JsxEmit,
// SyntaxKind,
// SourceFile,
// JsxSelfClosingElement,
// JsxClosingElement,
// JsxOpeningElement,
// Bundle,
// isPropertyAccessExpression,
// isPropertyAssignment,
// PropertyAccessExpression,
// Expression,
// createElementAccess,
// setTextRange,
// PropertyAssignment,
// isIdentifier,
// updatePropertyAssignment,
// Identifier,
// } from 'typescript';
// import { chainBundle, getOriginalNodeId, getNodeId } from './utilis';

// export default function hoistingTransformer(
// context: TransformationContext,
// ): (x: SourceFile | Bundle) => SourceFile | Bundle {
// const compilerOptions = context.getCompilerOptions();

// // enable emit notification only if using --jsx preserve or react-native
// let previousOnEmitNode: (hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) => void;
// let noSubstitution: boolean[];

// if (compilerOptions.jsx === JsxEmit.Preserve || compilerOptions.jsx === JsxEmit.ReactNative) {
// previousOnEmitNode = context.onEmitNode;
// context.onEmitNode = onEmitNode;
// context.enableEmitNotification(SyntaxKind.JsxOpeningElement);
// context.enableEmitNotification(SyntaxKind.JsxClosingElement);
// context.enableEmitNotification(SyntaxKind.JsxSelfClosingElement);
// noSubstitution = [];
// }

// const previousOnSubstituteNode = context.onSubstituteNode;
// context.onSubstituteNode = onSubstituteNode;
// context.enableSubstitution(SyntaxKind.PropertyAccessExpression);
// context.enableSubstitution(SyntaxKind.PropertyAssignment);
// return chainBundle(transformSourceFile);

// function transformSourceFile(node: SourceFile) {
// return node;
// }

// /**
// * Called by the printer just before a node is printed.
// *
// * @param hint A hint as to the intended usage of the node.
// * @param node The node to emit.
// * @param emitCallback A callback used to emit the node.
// */
// function onEmitNode(hint: EmitHint, node: Node, emitCallback: (emitContext: EmitHint, node: Node) => void) {
// switch (node.kind) {
// case SyntaxKind.JsxOpeningElement:
// case SyntaxKind.JsxClosingElement:
// case SyntaxKind.JsxSelfClosingElement:
// const tagName = (node as JsxOpeningElement | JsxClosingElement | JsxSelfClosingElement).tagName;
// noSubstitution[getOriginalNodeId(tagName)] = true;
// break;
// }

// previousOnEmitNode(hint, node, emitCallback);
// }

// /**
// * Hooks node substitutions.
// *
// * @param hint A hint as to the intended usage of the node.
// * @param node The node to substitute.
// */
// function onSubstituteNode(hint: EmitHint, node: Node) {
// if (getNodeId(node) && noSubstitution && noSubstitution[getNodeId(node)]) {
// return previousOnSubstituteNode(hint, node);
// }

// node = previousOnSubstituteNode(hint, node);
// if (isPropertyAccessExpression(node)) {
// return substitutePropertyAccessExpression(node);
// } else if (isPropertyAssignment(node)) {
// return substitutePropertyAssignment(node);
// }
// return node;
// }

// /**
// * Substitutes a PropertyAccessExpression whose name is a reserved word.
// *
// * @param node A PropertyAccessExpression
// */
// function substitutePropertyAccessExpression(node: PropertyAccessExpression): Expression {
// const literalName = trySubstituteReservedName(node.name);
// if (literalName) {
// return setTextRange(createElementAccess(node.expression, literalName), node);
// }
// return node;
// }

// /**
// * Substitutes a PropertyAssignment whose name is a reserved word.
// *
// * @param node A PropertyAssignment
// */
// function substitutePropertyAssignment(node: PropertyAssignment): PropertyAssignment {
// const literalName = isIdentifier(node.name) && trySubstituteReservedName(node.name);
// if (literalName) {
// return updatePropertyAssignment(node, literalName, node.initializer);
// }
// return node;
// }

// /**
// * If an identifier name is a reserved word, returns a string literal for the name.
// *
// * @param name An Identifier
// */
// function trySubstituteReservedName(name: Identifier) {
// const token = name.originalKeywordKind || (nodeIsSynthesized(name) ? stringToToken(idText(name)) : undefined);
// if (token !== undefined && token >= SyntaxKind.FirstReservedWord && token <= SyntaxKind.LastReservedWord) {
// return setTextRange(createLiteral(name), name);
// }
// return undefined;
// }
// }
Loading

0 comments on commit 2dba4fc

Please sign in to comment.