Skip to content
This repository has been archived by the owner on Oct 5, 2021. It is now read-only.

Commit

Permalink
refactor: rename $at → $_$twiz
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed Jan 14, 2018
1 parent f9d2493 commit db151f8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/instrument.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { instrument } from './instrument';
describe('instrument', () => {
it('should instrument function parameters without types', () => {
const input = `function (a) { return 5; }`;
expect(instrument(input, 'test.ts')).toEqual(`function (a) {$at("a",a,11,"test.ts"); return 5; }`);
expect(instrument(input, 'test.ts')).toEqual(`function (a) {$_$twiz("a",a,11,"test.ts"); return 5; }`);
});

it('should correctly instrument optional function parameters', () => {
const input = `function (a?) { return 5; }`;
expect(instrument(input, 'test.ts')).toEqual(`function (a?) {$at("a",a,12,"test.ts"); return 5; }`);
expect(instrument(input, 'test.ts')).toEqual(`function (a?) {$_$twiz("a",a,12,"test.ts"); return 5; }`);
});

it('should instrument class method parameters', () => {
const input = `class Foo { bar(a) { return 5; } }`;
expect(instrument(input, 'test.ts')).toEqual(`class Foo { bar(a) {$at("a",a,17,"test.ts"); return 5; } }`);
expect(instrument(input, 'test.ts')).toEqual(`class Foo { bar(a) {$_$twiz("a",a,17,"test.ts"); return 5; } }`);
});

it('should not instrument function parameters that already have a type', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function visit(node: ts.Node, replacements: Replacement[], fileName: string) {
typeInsertionPos,
JSON.stringify(fileName),
];
const instrumentExpr = `$at(${params.join(',')});`;
const instrumentExpr = `$_$twiz(${params.join(',')});`;
replacements.push(Replacement.insert(node.body.getStart() + 1, instrumentExpr));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('integration test', () => {
const compiled = ts.transpile(instrumented);

// Step 3: evaluate the code, and collect the runtime type information
const collectedTypes = vm.runInNewContext(getTypeCollectorSnippet() + compiled + '$at.get();');
const collectedTypes = vm.runInNewContext(getTypeCollectorSnippet() + compiled + '$_$twiz.get();');

// Step 4: put the collected typed into the code
mockFs.readFileSync.mockReturnValue(input);
Expand Down
4 changes: 2 additions & 2 deletions src/node-register.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { instrument } from './index';
import { $at } from './type-collector-snippet';
import { $_$twiz } from './type-collector-snippet';

type ICompileFunction = (source: string, filename: string) => void;

Expand All @@ -12,7 +12,7 @@ export function register(options?: IRegisterOptions) {
extensions: ['.ts', '.tsx'],
}, options);

(global as any).$at = $at;
(global as any).$_$twiz = $_$twiz;

const oldHooks: {[key: string]: any} = {};
for (const extension of options.extensions) {
Expand Down
26 changes: 13 additions & 13 deletions src/type-collector-snippet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
import { $at } from './type-collector-snippet';
import { $_$twiz } from './type-collector-snippet';

describe('type-collector', () => {
describe('getTypeName', () => {
it('should should return "null" for null', () => {
expect($at.typeName(null)).toBe('null');
expect($_$twiz.typeName(null)).toBe('null');
});

it('should should return "undefined" for undefined', () => {
expect($at.typeName(undefined)).toBe('undefined');
expect($_$twiz.typeName(undefined)).toBe('undefined');
});

it('should should return "boolean" for a true', () => {
expect($at.typeName(true)).toBe('boolean');
expect($_$twiz.typeName(true)).toBe('boolean');
});

it('should should return "number" for a NaN', () => {
// Not-a-Number is a actually number. I know, this is funny.
expect($at.typeName(NaN)).toBe('number');
expect($_$twiz.typeName(NaN)).toBe('number');
});

it('should should return "string" for a string', () => {
expect($at.typeName('hello')).toBe('string');
expect($_$twiz.typeName('hello')).toBe('string');
});

it('should should return "Set" for a Set', () => {
expect($at.typeName(new Set())).toBe('Set');
expect($_$twiz.typeName(new Set())).toBe('Set');
});

it('should should return null for an empty array', () => {
expect($at.typeName([])).toBe(null);
expect($_$twiz.typeName([])).toBe(null);
});

it('should should return "string[]" for array of strings', () => {
expect($at.typeName(['foo', 'bar'])).toBe('string[]');
expect($_$twiz.typeName(['foo', 'bar'])).toBe('string[]');
});

it('should should return "Array<number|string>" for mixed array of strings and numbers', () => {
expect($at.typeName(['foo', 15])).toBe('Array<number|string>');
expect($_$twiz.typeName(['foo', 15])).toBe('Array<number|string>');
});

it('should should return "string[][]" for array of string arrays', () => {
expect($at.typeName([['foo'], [], ['bar', 'baz']])).toBe('string[][]');
expect($_$twiz.typeName([['foo'], [], ['bar', 'baz']])).toBe('string[][]');
});

it('should return "Function" for functions', () => {
expect($at.typeName(() => 0)).toBe('Function');
expect($_$twiz.typeName(() => 0)).toBe('Function');
});

it('should throw a NestError in case of array has includes itself', () => {
const a = [];
a.push(a);
expect(() => $at.typeName(a)).toThrowError('NestError');
expect(() => $_$twiz.typeName(a)).toThrowError('NestError');
});
});
});
4 changes: 2 additions & 2 deletions src/type-collector-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getTypeName(value: any, nest = 0): string | null {

const logs: { [key: string]: Set<string> } = {};

export function $at(name: string, value: any, pos: number, filename: string) {
export function $_$twiz(name: string, value: any, pos: number, filename: string) {
const index = JSON.stringify({ filename, pos } as IKey);
try {
const typeName = getTypeName(value);
Expand All @@ -52,7 +52,7 @@ export function $at(name: string, value: any, pos: number, filename: string) {
}

// tslint:disable:no-namespace
export namespace $at {
export namespace $_$twiz {
export const typeName = getTypeName;
export const get = () => {
return Object.keys(logs).map((key) => {
Expand Down
2 changes: 1 addition & 1 deletion src/type-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export function getTypeCollectorSnippet() {
return `(function(host){
exports = {};
${transpiled};
host.$at = $at;
host.$_$twiz = $_$twiz;
})(typeof self !== 'undefined' ? self : this);\n`;
}

0 comments on commit db151f8

Please sign in to comment.