import {expect} from 'chai';
import {Node, Decorator, ClassDeclaration, SyntaxKind, CompilerOptions} from 'typescript';
import * as _ from 'lodash';
import {transpile, TranspilerConfig, VisitorContext} from '../src';
const config: TranspilerConfig = {
sourceFileName: 'sample.tsx',
visitors: []
};
describe('transpiler', function() {
it('fails on parser errors', function() {
const source = 'let a =
;';
const transpiled = transpile(source, config);
expect(transpiled.code).not.to.be.ok;
expect(transpiled.diags).not.to.be.empty;
});
describe('e2e regression test', () => {
const config2: TranspilerConfig = {
compilerOptions:
{
inlineSourceMap: false,
sourceMap: true,
inlineSources: false,
noEmitHelpers: false
},
sourceFileName: 'sample.tsx',
visitors: [{
filter: function(node: Node): boolean {
return node.kind == SyntaxKind.ClassDeclaration && node.decorators && node.decorators.length > 0;
},
visit: function(node: Node, context: VisitorContext): void {
let targetPosition: number = node.pos;
const classNode: ClassDeclaration = node;
if (!_.isEmpty(classNode.decorators)) {
targetPosition = (_.last(classNode.decorators)).end + 1;
}
// console.log('targetPosition', targetPosition);
context.insertLine(targetPosition, `@fooo(\`------------------------
'tags': ['@type'],
'properties': [
{
'name': 'title',
'type': 'core3.types.String'
},
{
'name': 'price',
'type': 'core3.types.Number'
},
{
'name': 'flag',
'type': 'core3.types.Boolean'
},
{
'name': 'func',
'type': 'core3.types.Function'
}
],
'methods': []
\`)`);
}
}]
};
it('checks sample code doesn\'t get garbled up the same way it once did', () => {
const source = `
function bar(){
}
var foo = bar;
var fooo = bar;
@bar
class ImageType {
}
@bar
class ImageProps {
}
@bar
class ImageState {
}
export class CropUtils{
static getContainLayout(imageWidth:number, imageHeight:number, boxWidth:number, boxHeight:number): Layout {
var layout:Layout = { x:0, y:0 };
var imageRatio = imageWidth / imageHeight;
var boxRatio = boxWidth / boxHeight;
if(imageRatio < boxRatio){
layout.w = boxHeight * imageRatio;
layout.h = boxHeight;
layout.x = Math.round(boxWidth/2 - layout.w/2);
} else {
layout.w = boxWidth;
layout.h = boxWidth / imageRatio;
layout.y = Math.round(boxHeight/2 - layout.h/2);
}
return layout;
}
static getCoverLayout(imageWidth:number, imageHeight:number, boxWidth:number, boxHeight:number): Layout {
var layout:Layout = { x:0, y:0 };
var imageRatio = imageWidth / imageHeight;
var boxRatio = boxWidth / boxHeight;
if(imageRatio < boxRatio){
layout.w = boxWidth;
layout.h = Math.round(boxWidth / imageRatio);
layout.y = Math.round(boxHeight/2 - layout.h/2);
} else {
layout.w = Math.round(boxHeight * imageRatio);
layout.h = boxHeight;
layout.x = Math.round(boxWidth/2 - layout.w/2);
}
return layout;
}
}
@bar
export default class Image{
}
`;
const transpiled = transpile(source, config2);
// transpiled code has exports.ABC = XYZ, which accesses the spec's
// exports object on eval(). wrap it IIFE.
const wrappedCode = `
(function (exports) {
${transpiled.code}
})({});
`;
expect(() => eval(wrappedCode)).not.to.throw();
});
});
});