Skip to content
This repository has been archived by the owner on Jul 1, 2020. It is now read-only.

Commit

Permalink
feat: add support for TypeReferences
Browse files Browse the repository at this point in the history
Support changing/rewriting TypeReferences. See mocha transform as an
example.
  • Loading branch information
KnisterPeter committed May 31, 2017
1 parent 68ba800 commit 3900bda
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"fs-extra": "^3.0.1",
"globby": "^6.1.0",
"meow": "^3.7.0",
"ts-emitter": "^0.3.0"
"ts-emitter": "^0.3.1"
},
"jest": {
"transform": {
Expand Down
3 changes: 2 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class Collection<T extends ts.Node, R extends ts.Node> {
public find(kind: ts.SyntaxKind.CallExpression, pattern?: CallExpressionPattern): Collection<ts.CallExpression, R>;
public find(kind: ts.SyntaxKind.VariableDeclarationList): Collection<ts.VariableDeclarationList, R>;
public find(kind: ts.SyntaxKind.VariableDeclaration): Collection<ts.VariableDeclaration, R>;
public find(kind: ts.SyntaxKind.TypeReference, pattern?: any): Collection<ts.TypeReferenceNode, R>;
public find(kind: ts.SyntaxKind, pattern?: any): Collection<ts.Node, R> {
const marked: ts.Node[] = [];
const visitor = (node: ts.Node) => {
Expand Down Expand Up @@ -90,7 +91,7 @@ export class Collection<T extends ts.Node, R extends ts.Node> {
const replaced = fn(markedNode);
if (replaced !== markedNode) {
(replaced as any).original = markedNode;
if ((replaced as any).text && (replaced as any).text !== (markedNode as any).text) {
if ((replaced as any).text && (markedNode as any).text) {
(replaced as any).newText = (replaced as any).text;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/pattern-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export function isPatternMatching(node: ts.Node, pattern: any): boolean {
if (!matchProperty(node, 'kind', pattern)) {
return false;
}
// tslint:disable-next-line cyclomatic-complexity
switch (node.kind) {
case ts.SyntaxKind.Identifier:
return matchIdentifier(node as ts.Identifier, pattern);
Expand All @@ -13,6 +14,8 @@ export function isPatternMatching(node: ts.Node, pattern: any): boolean {
return matchPropertyAccessExpression(node as ts.PropertyAccessExpression, pattern);
case ts.SyntaxKind.FunctionExpression:
return matchFunctionExpression(node as ts.FunctionExpression, pattern);
case ts.SyntaxKind.TypeReference:
return matchTypeReference(node as ts.TypeReferenceNode, pattern);
default:
throw new Error(`Pattern for ${ts.SyntaxKind[node.kind]} not implemented`);
}
Expand All @@ -37,6 +40,12 @@ function matchCallExpression(node: ts.CallExpression, pattern: any): boolean {
return matchProperty(node, 'expression', pattern);
}

function matchTypeReference(node: ts.TypeReferenceNode, pattern: any): boolean {
let matching = true;
matching = matching && matchProperty(node, 'text', pattern);
return matching;
}

// tslint:disable-next-line cyclomatic-complexity
function matchProperty(node: any, property: string, pattern: any, name = property): boolean {
const patternValue = pattern[name];
Expand Down
14 changes: 14 additions & 0 deletions src/transforms/jest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@ test('Convert mocha test to jest test', () => {
// something
});
beforeEach(() => {
});
suite('Array', function() {
setup(() => {
});
test('Array', (done: MochaDone) => {
done();
});
specify.skip('Array', function(done) {
done();
});
Expand All @@ -23,10 +30,17 @@ test('Convert mocha test to jest test', () => {
// something
});
beforeEach(()=> {
});
describe('Array', function() {
beforeEach(()=> {
});
it('Array', (done: jest.DoneCallback) => {
done();
});
it.skip('Array', function(done) {
done();
});
Expand Down
75 changes: 43 additions & 32 deletions src/transforms/jest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as ts from 'typescript';
import { Collection } from '../collection';
import { File, API } from '../types';

const methodMap: { [key: string]: string } = {
Expand Down Expand Up @@ -33,20 +34,56 @@ const methodModifiers = ['only', 'skip'];
// return scope.isGlobal ? false : hasBinding(name, scope.parent);
// }

function findMochaMethods(ast: Collection<ts.Node, ts.Node>, mochaMethod: string):
Collection<ts.CallExpression, ts.Node> {
return ast
.find(ts.SyntaxKind.CallExpression, {
expression: {
kind: ts.SyntaxKind.Identifier,
text: mochaMethod
}
});
}

function findMochaMethodsWithModifier(ast: Collection<ts.Node, ts.Node>, mochaMethod: string, modifier: string):
Collection<ts.CallExpression, ts.Node> {
return ast
.find(ts.SyntaxKind.CallExpression, {
expression: {
kind: ts.SyntaxKind.PropertyAccessExpression,
expression: {
kind: ts.SyntaxKind.Identifier,
text: mochaMethod
},
name: {
kind: ts.SyntaxKind.Identifier,
text: modifier
}
}
});
}

export default function mochaToJest(file: File, api: API): string {
const t = api.tscodeshift;
const ast = t(file.source);

Object.keys(methodMap).forEach(mochaMethod => {
const jestMethod = methodMap[mochaMethod];

ast
.find(ts.SyntaxKind.CallExpression, {
expression: {
findMochaMethods(ast, mochaMethod)
.find(ts.SyntaxKind.TypeReference, {
typeName: {
kind: ts.SyntaxKind.Identifier,
text: mochaMethod
text: 'MochaDone'
}
})
.replaceWith(() => {
return ts.createQualifiedName(
ts.createIdentifier('jest'),
ts.createIdentifier('DoneCallback')
);
});
findMochaMethods(ast, mochaMethod)
// .filter(({ scope }) => !hasBinding(mochaMethod, scope))
.get(node => node.expression)
.replaceWith(() => {
Expand All @@ -58,36 +95,10 @@ export default function mochaToJest(file: File, api: API): string {
});

methodModifiers.forEach(modifier => {
ast
.find(ts.SyntaxKind.CallExpression, {
expression: {
kind: ts.SyntaxKind.PropertyAccessExpression,
expression: {
kind: ts.SyntaxKind.Identifier,
text: mochaMethod
},
name: {
kind: ts.SyntaxKind.Identifier,
text: modifier
}
}
})
findMochaMethodsWithModifier(ast, mochaMethod, modifier)
.get(node => (node.expression as ts.PropertyAccessExpression).expression)
.replaceWith(() => ts.createIdentifier(jestMethod));
ast
.find(ts.SyntaxKind.CallExpression, {
expression: {
kind: ts.SyntaxKind.PropertyAccessExpression,
expression: {
kind: ts.SyntaxKind.Identifier,
text: mochaMethod
},
name: {
kind: ts.SyntaxKind.Identifier,
text: modifier
}
}
})
findMochaMethodsWithModifier(ast, mochaMethod, modifier)
.get(node => (node.expression as ts.PropertyAccessExpression).name)
.replaceWith(() => ts.createIdentifier(modifier));
});
Expand Down

0 comments on commit 3900bda

Please sign in to comment.