From db151f85ccd8821a3a28265ab82a4afb685586ac Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Mon, 15 Jan 2018 00:25:26 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20rename=20$at=20=E2=86=92=20$=5F$twi?= =?UTF-8?q?z?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/instrument.spec.ts | 6 +++--- src/instrument.ts | 2 +- src/integration.spec.ts | 2 +- src/node-register.ts | 4 ++-- src/type-collector-snippet.spec.ts | 26 +++++++++++++------------- src/type-collector-snippet.ts | 4 ++-- src/type-collector.ts | 2 +- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/instrument.spec.ts b/src/instrument.spec.ts index 44ba05b..e214a43 100644 --- a/src/instrument.spec.ts +++ b/src/instrument.spec.ts @@ -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', () => { diff --git a/src/instrument.ts b/src/instrument.ts index 944edff..47934d8 100644 --- a/src/instrument.ts +++ b/src/instrument.ts @@ -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)); } } diff --git a/src/integration.spec.ts b/src/integration.spec.ts index 2137e23..2dc4f65 100644 --- a/src/integration.spec.ts +++ b/src/integration.spec.ts @@ -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); diff --git a/src/node-register.ts b/src/node-register.ts index 39cc1a5..661b2bc 100644 --- a/src/node-register.ts +++ b/src/node-register.ts @@ -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; @@ -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) { diff --git a/src/type-collector-snippet.spec.ts b/src/type-collector-snippet.spec.ts index 2055d8e..a06cd4d 100644 --- a/src/type-collector-snippet.spec.ts +++ b/src/type-collector-snippet.spec.ts @@ -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" for mixed array of strings and numbers', () => { - expect($at.typeName(['foo', 15])).toBe('Array'); + expect($_$twiz.typeName(['foo', 15])).toBe('Array'); }); 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'); }); }); }); diff --git a/src/type-collector-snippet.ts b/src/type-collector-snippet.ts index 79d65b3..ee176f4 100644 --- a/src/type-collector-snippet.ts +++ b/src/type-collector-snippet.ts @@ -33,7 +33,7 @@ export function getTypeName(value: any, nest = 0): string | null { const logs: { [key: string]: Set } = {}; -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); @@ -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) => { diff --git a/src/type-collector.ts b/src/type-collector.ts index 19adcbc..7e03cea 100644 --- a/src/type-collector.ts +++ b/src/type-collector.ts @@ -12,6 +12,6 @@ export function getTypeCollectorSnippet() { return `(function(host){ exports = {}; ${transpiled}; - host.$at = $at; + host.$_$twiz = $_$twiz; })(typeof self !== 'undefined' ? self : this);\n`; }