Skip to content

Commit

Permalink
fix: Removed debug code
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Jun 14, 2019
1 parent bc09372 commit e7afd37
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
"name": "typescript-transform-unspec",
"version": "0.0.0-dev",
"license": "MIT",
"description": "Typescript transform plugin",
"description": "Typescript transform plugin removes spec definition from source file",
"main": "index.js",
"typings": "index.d.ts",
"author": "2019",
"keywords": [],
"keywords": [
"typescript-transformer",
"typescript-plugin",
"typescript-transform-plugin",
"typescript-compiler"
],
"engines": {
"node": ">=8"
},
Expand Down
4 changes: 1 addition & 3 deletions src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ it('hello world test', () => {
expect(hello()).toBe('hello world');
});

jest.mock('foo');

describe('hello world test', () => {
beforeAll(() => {
jest.mock('foo');
jest.mock('inspector');
});
});
8 changes: 3 additions & 5 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
/* eslint-disable @typescript-eslint/tslint/config */
import * as lib from './index';
import transformPlugin from './index';
import * as ts from 'typescript';

let transformPlugin: typeof lib.typescriptTransformUnspec = lib as any;

function transform(sourceText: string) {
const program = ts.createProgram({
rootNames: [],
options: {},
});
const transformer = transformPlugin(program, {});
const transformer = transformPlugin(program);
const sourceFile = ts.createSourceFile('filename.tsx', sourceText, ts.ScriptTarget.ES5, true, ts.ScriptKind.TSX);
let result: string | undefined;
const writeCallback = (fileName: string, data: string) => {
Expand All @@ -20,7 +18,7 @@ function transform(sourceText: string) {
}

it('smoke test', () => {
expect(lib).toBeTruthy();
expect(transformPlugin).toBeTruthy();
expect(transform(';')).toBe(';');
});

Expand Down
11 changes: 5 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as ts from 'typescript';

export function typescriptTransformUnspec(program: ts.Program, options?): ts.TransformerFactory<ts.SourceFile> {
export default function typescriptTransformUnspec(program: ts.Program): ts.TransformerFactory<ts.SourceFile> { // tslint:disable-line:no-default-export
return (context) => (file) => visitNodeAndChildren(file, program, context);
}

function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.SourceFile {
return ts.visitEachChild(visitor(node), (childNode) => visitNodeAndChildren(childNode, program, context), context) as ts.SourceFile;
const visitor = (childNode) => visitNodeAndChildren(childNode, program, context);
return ts.visitEachChild<ts.SourceFile>(<ts.SourceFile>filterNode(node), visitor, context);
}

function visitor(node: ts.Node): ts.Node | undefined {
function filterNode<T extends ts.Node>(node: T): T | undefined {
if (isSpecExpressionStatement(node)) {
return undefined;
}
Expand Down Expand Up @@ -44,18 +45,16 @@ function isSpecExpressionStatement(node: ts.Node) {
if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)) {
const callExpr = node.expression.expression;
if (ts.isIdentifier(callExpr) && isTestExpressionText(callExpr.escapedText)) {
console.log("callExpr", callExpr.flags);
return true;
}
if (ts.isPropertyAccessExpression(callExpr)) {
const propertyAccessExpr = callExpr.expression;
if (ts.isIdentifier(propertyAccessExpr) && isTestExpressionText(propertyAccessExpr.escapedText)) {
console.log("propertyAccessExpr", propertyAccessExpr.flags);
return true;
}
}
return false;
}
return false;
}

module.exports = typescriptTransformUnspec;
Expand Down

0 comments on commit e7afd37

Please sign in to comment.