refactor: move angular source to /packages rather than modules/@angular
This commit is contained in:
148
packages/compiler/test/output/abstract_emitter_node_only_spec.ts
Normal file
148
packages/compiler/test/output/abstract_emitter_node_only_spec.ts
Normal file
@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler';
|
||||
import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter';
|
||||
import {SourceMap} from '@angular/compiler/src/output/source_map';
|
||||
|
||||
const SourceMapConsumer = require('source-map').SourceMapConsumer;
|
||||
const b64 = require('base64-js');
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('AbstractEmitter', () => {
|
||||
describe('EmitterVisitorContext', () => {
|
||||
const fileA = new ParseSourceFile('a0a1a2a3a4a5a6a7a8a9', 'a.js');
|
||||
const fileB = new ParseSourceFile('b0b1b2b3b4b5b6b7b8b9', 'b.js');
|
||||
let ctx: EmitterVisitorContext;
|
||||
|
||||
beforeEach(() => { ctx = EmitterVisitorContext.createRoot([]); });
|
||||
|
||||
it('should add source files to the source map', () => {
|
||||
ctx.print(createSourceSpan(fileA, 0), 'o0');
|
||||
ctx.print(createSourceSpan(fileA, 1), 'o1');
|
||||
ctx.print(createSourceSpan(fileB, 0), 'o2');
|
||||
ctx.print(createSourceSpan(fileB, 1), 'o3');
|
||||
const sm = ctx.toSourceMapGenerator('o.js').toJSON();
|
||||
expect(sm.sources).toEqual([fileA.url, fileB.url]);
|
||||
expect(sm.sourcesContent).toEqual([fileA.content, fileB.content]);
|
||||
});
|
||||
|
||||
it('should generate a valid mapping', () => {
|
||||
ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
|
||||
ctx.println(createSourceSpan(fileB, 1), 'fileB-1');
|
||||
ctx.print(createSourceSpan(fileA, 2), 'fileA-2');
|
||||
|
||||
expectMap(ctx, 0, 0, 'a.js', 0, 0);
|
||||
expectMap(ctx, 0, 7, 'b.js', 0, 2);
|
||||
expectMap(ctx, 1, 0, 'a.js', 0, 4);
|
||||
});
|
||||
|
||||
it('should be able to shift the content', () => {
|
||||
ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
|
||||
|
||||
const sm = ctx.toSourceMapGenerator(null, 10).toJSON();
|
||||
const smc = new SourceMapConsumer(sm);
|
||||
expect(smc.originalPositionFor({line: 11, column: 0})).toEqual({
|
||||
line: 1,
|
||||
column: 0,
|
||||
source: 'a.js',
|
||||
name: null,
|
||||
});
|
||||
});
|
||||
|
||||
it('should not map leading segment without span', () => {
|
||||
ctx.print(null, '....');
|
||||
ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
|
||||
|
||||
expectMap(ctx, 0, 0);
|
||||
expectMap(ctx, 0, 4, 'a.js', 0, 0);
|
||||
expect(nbSegmentsPerLine(ctx)).toEqual([1]);
|
||||
});
|
||||
|
||||
it('should handle indent', () => {
|
||||
ctx.incIndent();
|
||||
ctx.println(createSourceSpan(fileA, 0), 'fileA-0');
|
||||
ctx.incIndent();
|
||||
ctx.println(createSourceSpan(fileA, 1), 'fileA-1');
|
||||
ctx.decIndent();
|
||||
ctx.println(createSourceSpan(fileA, 2), 'fileA-2');
|
||||
|
||||
expectMap(ctx, 0, 0);
|
||||
expectMap(ctx, 0, 2, 'a.js', 0, 0);
|
||||
expectMap(ctx, 1, 0);
|
||||
expectMap(ctx, 1, 2);
|
||||
expectMap(ctx, 1, 4, 'a.js', 0, 2);
|
||||
expectMap(ctx, 2, 0);
|
||||
expectMap(ctx, 2, 2, 'a.js', 0, 4);
|
||||
|
||||
expect(nbSegmentsPerLine(ctx)).toEqual([1, 1, 1]);
|
||||
});
|
||||
|
||||
it('should coalesce identical span', () => {
|
||||
const span = createSourceSpan(fileA, 0);
|
||||
ctx.print(span, 'fileA-0');
|
||||
ctx.print(null, '...');
|
||||
ctx.print(span, 'fileA-0');
|
||||
ctx.print(createSourceSpan(fileB, 0), 'fileB-0');
|
||||
|
||||
expectMap(ctx, 0, 0, 'a.js', 0, 0);
|
||||
expectMap(ctx, 0, 7, 'a.js', 0, 0);
|
||||
expectMap(ctx, 0, 10, 'a.js', 0, 0);
|
||||
expectMap(ctx, 0, 17, 'b.js', 0, 0);
|
||||
|
||||
expect(nbSegmentsPerLine(ctx)).toEqual([2]);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// All lines / columns indexes are 0-based
|
||||
// Note: source-map line indexes are 1-based, column 0-based
|
||||
function expectMap(
|
||||
ctx: EmitterVisitorContext, genLine: number, genCol: number, source: string = null,
|
||||
srcLine: number = null, srcCol: number = null) {
|
||||
const sm = ctx.toSourceMapGenerator().toJSON();
|
||||
const smc = new SourceMapConsumer(sm);
|
||||
const genPosition = {line: genLine + 1, column: genCol};
|
||||
const origPosition = smc.originalPositionFor(genPosition);
|
||||
expect(origPosition.source).toEqual(source);
|
||||
expect(origPosition.line).toEqual(srcLine === null ? null : srcLine + 1);
|
||||
expect(origPosition.column).toEqual(srcCol);
|
||||
}
|
||||
|
||||
// returns the number of segments per line
|
||||
function nbSegmentsPerLine(ctx: EmitterVisitorContext) {
|
||||
const sm = ctx.toSourceMapGenerator().toJSON();
|
||||
const lines = sm.mappings.split(';');
|
||||
return lines.map(l => {
|
||||
const m = l.match(/,/g);
|
||||
return m === null ? 1 : m.length + 1;
|
||||
});
|
||||
}
|
||||
|
||||
function createSourceSpan(file: ParseSourceFile, idx: number) {
|
||||
const col = 2 * idx;
|
||||
const start = new ParseLocation(file, col, 0, col);
|
||||
const end = new ParseLocation(file, col + 2, 0, col + 2);
|
||||
const sourceSpan = new ParseSourceSpan(start, end);
|
||||
return {sourceSpan};
|
||||
}
|
||||
|
||||
export function extractSourceMap(source: string): SourceMap {
|
||||
let idx = source.lastIndexOf('\n//#');
|
||||
if (idx == -1) return null;
|
||||
const smComment = source.slice(idx).trim();
|
||||
const smB64 = smComment.split('sourceMappingURL=data:application/json;base64,')[1];
|
||||
return smB64 ? JSON.parse(decodeB64String(smB64)) : null;
|
||||
}
|
||||
|
||||
function decodeB64String(s: string): string {
|
||||
return b64.toByteArray(s).reduce((s: string, c: number) => s + String.fromCharCode(c), '');
|
||||
}
|
40
packages/compiler/test/output/abstract_emitter_spec.ts
Normal file
40
packages/compiler/test/output/abstract_emitter_spec.ts
Normal file
@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {escapeIdentifier} from '@angular/compiler/src/output/abstract_emitter';
|
||||
|
||||
export function main() {
|
||||
describe('AbstractEmitter', () => {
|
||||
describe('escapeIdentifier', () => {
|
||||
it('should escape single quotes',
|
||||
() => { expect(escapeIdentifier(`'`, false)).toEqual(`'\\''`); });
|
||||
|
||||
it('should escape backslash',
|
||||
() => { expect(escapeIdentifier('\\', false)).toEqual(`'\\\\'`); });
|
||||
|
||||
it('should escape newlines',
|
||||
() => { expect(escapeIdentifier('\n', false)).toEqual(`'\\n'`); });
|
||||
|
||||
it('should escape carriage returns',
|
||||
() => { expect(escapeIdentifier('\r', false)).toEqual(`'\\r'`); });
|
||||
|
||||
it('should escape $', () => { expect(escapeIdentifier('$', true)).toEqual(`'\\$'`); });
|
||||
it('should not escape $', () => { expect(escapeIdentifier('$', false)).toEqual(`'$'`); });
|
||||
it('should add quotes for non-identifiers',
|
||||
() => { expect(escapeIdentifier('==', false, false)).toEqual(`'=='`); });
|
||||
it('does not escape class (but it probably should)',
|
||||
() => { expect(escapeIdentifier('class', false, false)).toEqual('class'); });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function stripSourceMap(source: string): string {
|
||||
const smi = source.lastIndexOf('\n//#');
|
||||
if (smi == -1) return source;
|
||||
return source.slice(0, smi);
|
||||
}
|
66
packages/compiler/test/output/js_emitter_node_only_spec.ts
Normal file
66
packages/compiler/test/output/js_emitter_node_only_spec.ts
Normal file
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
||||
import {SourceMap} from '@angular/compiler/src/output/source_map';
|
||||
import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler/src/parse_util';
|
||||
|
||||
import {extractSourceMap} from './abstract_emitter_node_only_spec';
|
||||
|
||||
const SourceMapConsumer = require('source-map').SourceMapConsumer;
|
||||
|
||||
const someModuleUrl = 'somePackage/somePath';
|
||||
|
||||
class SimpleJsImportGenerator implements ImportResolver {
|
||||
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
|
||||
return importedUrlStr;
|
||||
}
|
||||
getImportAs(symbol: StaticSymbol): StaticSymbol { return null; }
|
||||
getTypeArity(symbol: StaticSymbol): number /*|null*/ { return null; }
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe('JavaScriptEmitter', () => {
|
||||
let importResolver: ImportResolver;
|
||||
let emitter: JavaScriptEmitter;
|
||||
let someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
importResolver = new SimpleJsImportGenerator();
|
||||
emitter = new JavaScriptEmitter(importResolver);
|
||||
});
|
||||
|
||||
function emitSourceMap(
|
||||
stmt: o.Statement | o.Statement[], exportedVars: string[] = null): SourceMap {
|
||||
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
||||
const source = emitter.emitStatements(someModuleUrl, stmts, exportedVars || []);
|
||||
return extractSourceMap(source);
|
||||
}
|
||||
|
||||
describe('source maps', () => {
|
||||
it('should emit an inline source map', () => {
|
||||
const source = new ParseSourceFile(';;;var', 'in.js');
|
||||
const startLocation = new ParseLocation(source, 0, 0, 3);
|
||||
const endLocation = new ParseLocation(source, 7, 0, 6);
|
||||
const sourceSpan = new ParseSourceSpan(startLocation, endLocation);
|
||||
const someVar = o.variable('someVar', null, sourceSpan);
|
||||
const sm = emitSourceMap(someVar.toStmt());
|
||||
const smc = new SourceMapConsumer(sm);
|
||||
|
||||
expect(sm.sources).toEqual(['in.js']);
|
||||
expect(sm.sourcesContent).toEqual([';;;var']);
|
||||
expect(smc.originalPositionFor({line: 1, column: 0}))
|
||||
.toEqual({line: 1, column: 3, source: 'in.js', name: null});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
304
packages/compiler/test/output/js_emitter_spec.ts
Normal file
304
packages/compiler/test/output/js_emitter_spec.ts
Normal file
@ -0,0 +1,304 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import {JavaScriptEmitter} from '@angular/compiler/src/output/js_emitter';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
||||
|
||||
import {stripSourceMap} from './abstract_emitter_spec';
|
||||
|
||||
const someModuleUrl = 'somePackage/somePath';
|
||||
const anotherModuleUrl = 'somePackage/someOtherPath';
|
||||
|
||||
const sameModuleIdentifier: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(someModuleUrl, 'someLocalId', [])
|
||||
};
|
||||
const externalModuleIdentifier: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId', [])
|
||||
};
|
||||
|
||||
class SimpleJsImportGenerator implements ImportResolver {
|
||||
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
|
||||
return importedUrlStr;
|
||||
}
|
||||
getImportAs(symbol: StaticSymbol): StaticSymbol { return null; }
|
||||
getTypeArity(symbol: StaticSymbol): number /*|null*/ { return null; }
|
||||
}
|
||||
|
||||
export function main() {
|
||||
// Note supported features of our OutputAstin JavaScript / ES5:
|
||||
// - types
|
||||
// - declaring fields
|
||||
|
||||
describe('JavaScriptEmitter', () => {
|
||||
let importResolver: ImportResolver;
|
||||
let emitter: JavaScriptEmitter;
|
||||
let someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
importResolver = new SimpleJsImportGenerator();
|
||||
emitter = new JavaScriptEmitter(importResolver);
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitStmt(stmt: o.Statement, exportedVars: string[] = null): string {
|
||||
const source = emitter.emitStatements(someModuleUrl, [stmt], exportedVars || []);
|
||||
return stripSourceMap(source);
|
||||
}
|
||||
|
||||
it('should declare variables', () => {
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar'])).toEqual([
|
||||
'var someVar = 1;',
|
||||
`Object.defineProperty(exports, 'someVar', { get: function() { return someVar; }});`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should read and write variables', () => {
|
||||
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
||||
.toEqual(`someVar = (someOtherVar = 1);`);
|
||||
});
|
||||
|
||||
it('should read and write keys', () => {
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
||||
.toEqual(`someMap[someKey];`);
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someMap[someKey] = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write properties', () => {
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
||||
.toEqual(`someObj.someProp;`);
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someObj.someProp = 1;`);
|
||||
});
|
||||
|
||||
it('should invoke functions and methods and constructors', () => {
|
||||
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
||||
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
||||
.toEqual('someObj.someMethod(1);');
|
||||
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
||||
.toEqual('new SomeClass(1);');
|
||||
});
|
||||
|
||||
it('should support builtin methods', () => {
|
||||
expect(emitStmt(o.variable('arr1')
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
||||
.toStmt()))
|
||||
.toEqual('arr1.concat(arr2);');
|
||||
|
||||
expect(emitStmt(o.variable('observable')
|
||||
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
||||
.toStmt()))
|
||||
.toEqual('observable.subscribe(listener);');
|
||||
|
||||
expect(
|
||||
emitStmt(
|
||||
o.variable('fn').callMethod(o.BuiltinMethod.Bind, [o.variable('someObj')]).toStmt()))
|
||||
.toEqual('fn.bind(someObj);');
|
||||
});
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
||||
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
||||
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
||||
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
||||
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt())).toEqual(`{someKey: 1};`);
|
||||
});
|
||||
|
||||
it('should support blank literals', () => {
|
||||
expect(emitStmt(o.literal(null).toStmt())).toEqual('null;');
|
||||
expect(emitStmt(o.literal(undefined).toStmt())).toEqual('undefined;');
|
||||
});
|
||||
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
||||
`var import0 = re` +
|
||||
`quire('somePackage/someOtherPath');`,
|
||||
`import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support `importAs` for external identifiers', () => {
|
||||
spyOn(importResolver, 'getImportAs')
|
||||
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
||||
`var import0 = re` +
|
||||
`quire('somePackage/importAsModule');`,
|
||||
`import0.importAsName;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
const lhs = o.variable('lhs');
|
||||
const rhs = o.variable('rhs');
|
||||
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
||||
expect(
|
||||
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
||||
.toEqual('(someVar? trueCase: falseCase);');
|
||||
|
||||
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
||||
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
||||
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('(lhs === rhs);');
|
||||
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('(lhs !== rhs);');
|
||||
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
||||
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
||||
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
||||
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
||||
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
||||
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
||||
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
||||
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
||||
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
||||
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
||||
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
||||
});
|
||||
|
||||
it('should support function expressions', () => {
|
||||
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['function() {', '};'].join('\n'));
|
||||
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))]).toStmt())).toEqual([
|
||||
'function() {', ' return 1;\n};'
|
||||
].join('\n'));
|
||||
expect(emitStmt(o.fn([new o.FnParam('param1')], []).toStmt())).toEqual([
|
||||
'function(param1) {', '};'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support function statements', () => {
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [
|
||||
]))).toEqual(['function someFn() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []), ['someFn'])).toEqual([
|
||||
'function someFn() {', '}',
|
||||
`Object.defineProperty(exports, 'someFn', { get: function() { return someFn; }});`
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [
|
||||
new o.ReturnStatement(o.literal(1))
|
||||
]))).toEqual(['function someFn() {', ' return 1;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1')], [
|
||||
]))).toEqual(['function someFn(param1) {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support comments', () => {
|
||||
expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support if stmt', () => {
|
||||
const trueCase = o.variable('trueCase').callFn([]).toStmt();
|
||||
const falseCase = o.variable('falseCase').callFn([]).toStmt();
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase]))).toEqual([
|
||||
'if (cond) { trueCase(); }'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase]))).toEqual([
|
||||
'if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support try/catch', () => {
|
||||
const bodyStmt = o.variable('body').callFn([]).toStmt();
|
||||
const catchStmt =
|
||||
o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
||||
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt]))).toEqual([
|
||||
'try {', ' body();', '} catch (error) {', ' var stack = error.stack;',
|
||||
' catchFn(error,stack);', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support support throwing',
|
||||
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
||||
|
||||
describe('classes', () => {
|
||||
let callSomeMethod: o.Statement;
|
||||
|
||||
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
||||
|
||||
it('should support declaring classes', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
||||
]))).toEqual(['function SomeClass() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, []), ['SomeClass']))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}',
|
||||
`Object.defineProperty(exports, 'SomeClass', { get: function() { return SomeClass; }});`
|
||||
].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}',
|
||||
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring constructors', () => {
|
||||
const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
||||
expect(emitStmt(
|
||||
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
||||
.toEqual(['function SomeClass() {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [new o.FnParam('someParam')], []), [])))
|
||||
.toEqual(['function SomeClass(someParam) {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', o.variable('SomeSuperClass'), [], [],
|
||||
new o.ClassMethod(null, [], [superCall]), [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', ' var self = this;',
|
||||
' SomeSuperClass.call(this, someParam);', '}',
|
||||
'SomeClass.prototype = Object.create(SomeSuperClass.prototype);'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', ' var self = this;', ' self.someMethod();', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring getters', () => {
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [new o.ClassGetter('someGetter', [])], null, [])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}',
|
||||
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`, `}});`
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
||||
[])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}',
|
||||
`Object.defineProperty(SomeClass.prototype, 'someGetter', { get: function() {`,
|
||||
` var self = this;`, ` self.someMethod();`, `}});`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support methods', () => {
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null, [new o.ClassMethod('someMethod', [], [])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {', '};'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [new o.FnParam('someParam')], [])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}',
|
||||
'SomeClass.prototype.someMethod = function(someParam) {', '};'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
||||
.toEqual([
|
||||
'function SomeClass() {', '}', 'SomeClass.prototype.someMethod = function() {',
|
||||
' var self = this;', ' self.someMethod();', '};'
|
||||
].join('\n'));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
130
packages/compiler/test/output/source_map_spec.ts
Normal file
130
packages/compiler/test/output/source_map_spec.ts
Normal file
@ -0,0 +1,130 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {SourceMapGenerator, toBase64String} from '@angular/compiler/src/output/source_map';
|
||||
|
||||
export function main() {
|
||||
describe('source map generation', () => {
|
||||
describe('generation', () => {
|
||||
it('should generate a valid source map', () => {
|
||||
const map = new SourceMapGenerator('out.js')
|
||||
.addSource('a.js', null)
|
||||
.addLine()
|
||||
.addMapping(0, 'a.js', 0, 0)
|
||||
.addMapping(4, 'a.js', 0, 6)
|
||||
.addMapping(5, 'a.js', 0, 7)
|
||||
.addMapping(8, 'a.js', 0, 22)
|
||||
.addMapping(9, 'a.js', 0, 23)
|
||||
.addMapping(10, 'a.js', 0, 24)
|
||||
.addLine()
|
||||
.addMapping(0, 'a.js', 1, 0)
|
||||
.addMapping(4, 'a.js', 1, 6)
|
||||
.addMapping(5, 'a.js', 1, 7)
|
||||
.addMapping(8, 'a.js', 1, 10)
|
||||
.addMapping(9, 'a.js', 1, 11)
|
||||
.addMapping(10, 'a.js', 1, 12)
|
||||
.addLine()
|
||||
.addMapping(0, 'a.js', 3, 0)
|
||||
.addMapping(2, 'a.js', 3, 2)
|
||||
.addMapping(3, 'a.js', 3, 3)
|
||||
.addMapping(10, 'a.js', 3, 10)
|
||||
.addMapping(11, 'a.js', 3, 11)
|
||||
.addMapping(21, 'a.js', 3, 11)
|
||||
.addMapping(22, 'a.js', 3, 12)
|
||||
.addLine()
|
||||
.addMapping(4, 'a.js', 4, 4)
|
||||
.addMapping(11, 'a.js', 4, 11)
|
||||
.addMapping(12, 'a.js', 4, 12)
|
||||
.addMapping(15, 'a.js', 4, 15)
|
||||
.addMapping(16, 'a.js', 4, 16)
|
||||
.addMapping(21, 'a.js', 4, 21)
|
||||
.addMapping(22, 'a.js', 4, 22)
|
||||
.addMapping(23, 'a.js', 4, 23)
|
||||
.addLine()
|
||||
.addMapping(0, 'a.js', 5, 0)
|
||||
.addMapping(1, 'a.js', 5, 1)
|
||||
.addMapping(2, 'a.js', 5, 2)
|
||||
.addMapping(3, 'a.js', 5, 2);
|
||||
|
||||
// Generated with https://sokra.github.io/source-map-visualization using a TS source map
|
||||
expect(map.toJSON().mappings)
|
||||
.toEqual(
|
||||
'AAAA,IAAM,CAAC,GAAe,CAAC,CAAC;AACxB,IAAM,CAAC,GAAG,CAAC,CAAC;AAEZ,EAAE,CAAC,OAAO,CAAC,UAAA,CAAC;IACR,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AACvB,CAAC,CAAC,CAAA');
|
||||
});
|
||||
|
||||
it('should include the files and their contents', () => {
|
||||
const map = new SourceMapGenerator('out.js')
|
||||
.addSource('inline.ts', 'inline')
|
||||
.addSource('inline.ts', 'inline') // make sur the sources are dedup
|
||||
.addSource('url.ts', null)
|
||||
.addLine()
|
||||
.addMapping(0, 'inline.ts', 0, 0)
|
||||
.toJSON();
|
||||
|
||||
expect(map.file).toEqual('out.js');
|
||||
expect(map.sources).toEqual(['inline.ts', 'url.ts']);
|
||||
expect(map.sourcesContent).toEqual(['inline', null]);
|
||||
});
|
||||
|
||||
it('should not generate source maps when there is no mapping', () => {
|
||||
const smg = new SourceMapGenerator('out.js').addSource('inline.ts', 'inline').addLine();
|
||||
|
||||
expect(smg.toJSON()).toEqual(null);
|
||||
expect(smg.toJsComment()).toEqual('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('encodeB64String', () => {
|
||||
it('should return the b64 encoded value', () => {
|
||||
[['', ''], ['a', 'YQ=='], ['Foo', 'Rm9v'], ['Foo1', 'Rm9vMQ=='], ['Foo12', 'Rm9vMTI='],
|
||||
['Foo123', 'Rm9vMTIz'],
|
||||
].forEach(([src, b64]) => expect(toBase64String(src)).toEqual(b64));
|
||||
});
|
||||
});
|
||||
|
||||
describe('errors', () => {
|
||||
it('should throw when mappings are added out of order', () => {
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js')
|
||||
.addSource('in.js')
|
||||
.addLine()
|
||||
.addMapping(10, 'in.js', 0, 0)
|
||||
.addMapping(0, 'in.js', 0, 0);
|
||||
}).toThrowError('Mapping should be added in output order');
|
||||
});
|
||||
|
||||
it('should throw when adding segments before any line is created', () => {
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js').addSource('in.js').addMapping(0, 'in.js', 0, 0);
|
||||
}).toThrowError('A line must be added before mappings can be added');
|
||||
});
|
||||
|
||||
it('should throw when adding segments referencing unknown sources', () => {
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(
|
||||
0, 'in_.js', 0, 0);
|
||||
}).toThrowError('Unknown source file "in_.js"');
|
||||
});
|
||||
|
||||
it('should throw when adding segments without column', () => {
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(null);
|
||||
}).toThrowError('The column in the generated code must be provided');
|
||||
});
|
||||
|
||||
it('should throw when adding segments with a source url but no position', () => {
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(0, 'in.js');
|
||||
}).toThrowError('The source location must be provided when a source url is provided');
|
||||
expect(() => {
|
||||
new SourceMapGenerator('out.js').addSource('in.js').addLine().addMapping(0, 'in.js', 0);
|
||||
}).toThrowError('The source location must be provided when a source url is provided');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
71
packages/compiler/test/output/ts_emitter_node_only_spec.ts
Normal file
71
packages/compiler/test/output/ts_emitter_node_only_spec.ts
Normal file
@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ParseLocation, ParseSourceFile} from '@angular/compiler';
|
||||
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
||||
import {SourceMap} from '@angular/compiler/src/output/source_map';
|
||||
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
||||
import {ParseSourceSpan} from '@angular/compiler/src/parse_util';
|
||||
|
||||
import {extractSourceMap} from './abstract_emitter_node_only_spec';
|
||||
|
||||
const SourceMapConsumer = require('source-map').SourceMapConsumer;
|
||||
|
||||
const someModuleUrl = 'somePackage/somePath';
|
||||
|
||||
class SimpleJsImportGenerator implements ImportResolver {
|
||||
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
|
||||
return importedUrlStr;
|
||||
}
|
||||
getImportAs(symbol: StaticSymbol): StaticSymbol { return null; }
|
||||
getTypeArity(symbol: StaticSymbol): number /*|null*/ { return null; }
|
||||
}
|
||||
|
||||
export function main() {
|
||||
// Not supported features of our OutputAst in TS:
|
||||
// - real `const` like in Dart
|
||||
// - final fields
|
||||
|
||||
describe('TypeScriptEmitter', () => {
|
||||
let importResolver: ImportResolver;
|
||||
let emitter: TypeScriptEmitter;
|
||||
let someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
importResolver = new SimpleJsImportGenerator();
|
||||
emitter = new TypeScriptEmitter(importResolver);
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitSourceMap(
|
||||
stmt: o.Statement | o.Statement[], exportedVars: string[] = null): SourceMap {
|
||||
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
||||
const source = emitter.emitStatements(someModuleUrl, stmts, exportedVars || []);
|
||||
return extractSourceMap(source);
|
||||
}
|
||||
|
||||
describe('source maps', () => {
|
||||
it('should emit an inline source map', () => {
|
||||
const source = new ParseSourceFile(';;;var', 'in.js');
|
||||
const startLocation = new ParseLocation(source, 0, 0, 3);
|
||||
const endLocation = new ParseLocation(source, 7, 0, 6);
|
||||
const sourceSpan = new ParseSourceSpan(startLocation, endLocation);
|
||||
const someVar = o.variable('someVar', null, sourceSpan);
|
||||
const sm = emitSourceMap(someVar.toStmt());
|
||||
const smc = new SourceMapConsumer(sm);
|
||||
|
||||
expect(sm.sources).toEqual(['in.js']);
|
||||
expect(sm.sourcesContent).toEqual([';;;var']);
|
||||
expect(smc.originalPositionFor({line: 1, column: 0}))
|
||||
.toEqual({line: 1, column: 3, source: 'in.js', name: null});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
463
packages/compiler/test/output/ts_emitter_spec.ts
Normal file
463
packages/compiler/test/output/ts_emitter_spec.ts
Normal file
@ -0,0 +1,463 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
||||
import {CompileIdentifierMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import * as o from '@angular/compiler/src/output/output_ast';
|
||||
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
||||
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
||||
|
||||
import {stripSourceMap} from './abstract_emitter_spec';
|
||||
|
||||
const someModuleUrl = 'somePackage/somePath';
|
||||
const anotherModuleUrl = 'somePackage/someOtherPath';
|
||||
|
||||
const sameModuleIdentifier: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(someModuleUrl, 'someLocalId', [])
|
||||
};
|
||||
|
||||
const externalModuleIdentifier: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId', [])
|
||||
};
|
||||
|
||||
class SimpleJsImportGenerator implements ImportResolver {
|
||||
fileNameToModuleName(importedUrlStr: string, moduleUrlStr: string): string {
|
||||
return importedUrlStr;
|
||||
}
|
||||
getImportAs(symbol: StaticSymbol): StaticSymbol { return null; }
|
||||
getTypeArity(symbol: StaticSymbol): number /*|null*/ { return null; }
|
||||
}
|
||||
|
||||
export function main() {
|
||||
// Not supported features of our OutputAst in TS:
|
||||
// - real `const` like in Dart
|
||||
// - final fields
|
||||
|
||||
describe('TypeScriptEmitter', () => {
|
||||
let importResolver: ImportResolver;
|
||||
let emitter: TypeScriptEmitter;
|
||||
let someVar: o.ReadVarExpr;
|
||||
|
||||
beforeEach(() => {
|
||||
importResolver = new SimpleJsImportGenerator();
|
||||
emitter = new TypeScriptEmitter(importResolver);
|
||||
someVar = o.variable('someVar');
|
||||
});
|
||||
|
||||
function emitStmt(stmt: o.Statement | o.Statement[], exportedVars: string[] = null): string {
|
||||
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
||||
const source = emitter.emitStatements(someModuleUrl, stmts, exportedVars || []);
|
||||
return stripSourceMap(source);
|
||||
}
|
||||
|
||||
it('should declare variables', () => {
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt())).toEqual(`var someVar:any = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(null, [o.StmtModifier.Final])))
|
||||
.toEqual(`const someVar:any = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(), ['someVar']))
|
||||
.toEqual(`export var someVar:any = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INT_TYPE)))
|
||||
.toEqual(`var someVar:number = 1;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toDeclStmt(o.INFERRED_TYPE)))
|
||||
.toEqual(`var someVar = 1;`);
|
||||
});
|
||||
|
||||
describe('declare variables with ExternExpressions as values', () => {
|
||||
it('should create no reexport if the identifier is in the same module', () => {
|
||||
// identifier is in the same module -> no reexport
|
||||
expect(emitStmt(someVar.set(o.importExpr(sameModuleIdentifier)).toDeclStmt(), ['someVar']))
|
||||
.toEqual('export var someVar:any = someLocalId;');
|
||||
});
|
||||
|
||||
it('should create no reexport if the variable is not exported', () => {
|
||||
expect(emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt())).toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`var someVar:any = import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should create no reexport if the variable is typed', () => {
|
||||
expect(emitStmt(
|
||||
someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(o.DYNAMIC_TYPE),
|
||||
['someVar']))
|
||||
.toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`export var someVar:any = import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should create no reexport if the identifier has members', () => {
|
||||
const externalModuleIdentifierWithMembers: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId', ['a'])
|
||||
};
|
||||
expect(emitStmt(
|
||||
someVar.set(o.importExpr(externalModuleIdentifierWithMembers)).toDeclStmt(),
|
||||
['someVar']))
|
||||
.toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`export var someVar:any = import0.someExternalId.a;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should create a reexport', () => {
|
||||
expect(
|
||||
emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(), ['someVar']))
|
||||
.toEqual([
|
||||
`export {someExternalId as someVar} from 'somePackage/someOtherPath';`, ``
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should create multiple reexports from the same file', () => {
|
||||
const someVar2 = o.variable('someVar2');
|
||||
const externalModuleIdentifier2: CompileIdentifierMetadata = {
|
||||
reference: new StaticSymbol(anotherModuleUrl, 'someExternalId2', [])
|
||||
};
|
||||
expect(emitStmt(
|
||||
[
|
||||
someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(),
|
||||
someVar2.set(o.importExpr(externalModuleIdentifier2)).toDeclStmt()
|
||||
],
|
||||
['someVar', 'someVar2']))
|
||||
.toEqual([
|
||||
`export {someExternalId as someVar,someExternalId2 as someVar2} from 'somePackage/someOtherPath';`,
|
||||
``
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should use `importAs` for reexports', () => {
|
||||
spyOn(importResolver, 'getImportAs')
|
||||
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
||||
expect(
|
||||
emitStmt(someVar.set(o.importExpr(externalModuleIdentifier)).toDeclStmt(), ['someVar']))
|
||||
.toEqual([
|
||||
`export {importAsName as someVar} from 'somePackage/importAsModule';`, ``
|
||||
].join('\n'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should read and write variables', () => {
|
||||
expect(emitStmt(someVar.toStmt())).toEqual(`someVar;`);
|
||||
expect(emitStmt(someVar.set(o.literal(1)).toStmt())).toEqual(`someVar = 1;`);
|
||||
expect(emitStmt(someVar.set(o.variable('someOtherVar').set(o.literal(1))).toStmt()))
|
||||
.toEqual(`someVar = (someOtherVar = 1);`);
|
||||
});
|
||||
|
||||
it('should read and write keys', () => {
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).toStmt()))
|
||||
.toEqual(`someMap[someKey];`);
|
||||
expect(emitStmt(o.variable('someMap').key(o.variable('someKey')).set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someMap[someKey] = 1;`);
|
||||
});
|
||||
|
||||
it('should read and write properties', () => {
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').toStmt()))
|
||||
.toEqual(`someObj.someProp;`);
|
||||
expect(emitStmt(o.variable('someObj').prop('someProp').set(o.literal(1)).toStmt()))
|
||||
.toEqual(`someObj.someProp = 1;`);
|
||||
});
|
||||
|
||||
it('should invoke functions and methods and constructors', () => {
|
||||
expect(emitStmt(o.variable('someFn').callFn([o.literal(1)]).toStmt())).toEqual('someFn(1);');
|
||||
expect(emitStmt(o.variable('someObj').callMethod('someMethod', [o.literal(1)]).toStmt()))
|
||||
.toEqual('someObj.someMethod(1);');
|
||||
expect(emitStmt(o.variable('SomeClass').instantiate([o.literal(1)]).toStmt()))
|
||||
.toEqual('new SomeClass(1);');
|
||||
});
|
||||
|
||||
it('should support builtin methods', () => {
|
||||
expect(emitStmt(o.variable('arr1')
|
||||
.callMethod(o.BuiltinMethod.ConcatArray, [o.variable('arr2')])
|
||||
.toStmt()))
|
||||
.toEqual('arr1.concat(arr2);');
|
||||
|
||||
expect(emitStmt(o.variable('observable')
|
||||
.callMethod(o.BuiltinMethod.SubscribeObservable, [o.variable('listener')])
|
||||
.toStmt()))
|
||||
.toEqual('observable.subscribe(listener);');
|
||||
|
||||
expect(
|
||||
emitStmt(
|
||||
o.variable('fn').callMethod(o.BuiltinMethod.Bind, [o.variable('someObj')]).toStmt()))
|
||||
.toEqual('fn.bind(someObj);');
|
||||
});
|
||||
|
||||
it('should support literals', () => {
|
||||
expect(emitStmt(o.literal(0).toStmt())).toEqual('0;');
|
||||
expect(emitStmt(o.literal(true).toStmt())).toEqual('true;');
|
||||
expect(emitStmt(o.literal('someStr').toStmt())).toEqual(`'someStr';`);
|
||||
expect(emitStmt(o.literalArr([o.literal(1)]).toStmt())).toEqual(`[1];`);
|
||||
expect(emitStmt(o.literalMap([['someKey', o.literal(1)]]).toStmt())).toEqual(`{someKey: 1};`);
|
||||
});
|
||||
|
||||
it('should apply quotes to each entry within a map produced with literalMap when true', () => {
|
||||
expect(
|
||||
emitStmt(
|
||||
o.literalMap([['a', o.literal('a')], ['*', o.literal('star')]], null, true).toStmt())
|
||||
.replace(/\s+/gm, ''))
|
||||
.toEqual(`{'a':'a','*':'star'};`);
|
||||
});
|
||||
|
||||
it('should support blank literals', () => {
|
||||
expect(emitStmt(o.literal(null).toStmt())).toEqual('(null as any);');
|
||||
expect(emitStmt(o.literal(undefined).toStmt())).toEqual('(undefined as any);');
|
||||
expect(emitStmt(o.variable('a', null).isBlank().toStmt())).toEqual('(a == null);');
|
||||
});
|
||||
|
||||
it('should support external identifiers', () => {
|
||||
expect(emitStmt(o.importExpr(sameModuleIdentifier).toStmt())).toEqual('someLocalId;');
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`, `import0.someExternalId;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support `importAs` for external identifiers', () => {
|
||||
spyOn(importResolver, 'getImportAs')
|
||||
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
||||
expect(emitStmt(o.importExpr(externalModuleIdentifier).toStmt())).toEqual([
|
||||
`import * as import0 from 'somePackage/importAsModule';`, `import0.importAsName;`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support operators', () => {
|
||||
const lhs = o.variable('lhs');
|
||||
const rhs = o.variable('rhs');
|
||||
expect(emitStmt(someVar.cast(o.INT_TYPE).toStmt())).toEqual('(<number>someVar);');
|
||||
expect(emitStmt(o.not(someVar).toStmt())).toEqual('!someVar;');
|
||||
expect(
|
||||
emitStmt(someVar.conditional(o.variable('trueCase'), o.variable('falseCase')).toStmt()))
|
||||
.toEqual('(someVar? trueCase: falseCase);');
|
||||
|
||||
expect(emitStmt(lhs.equals(rhs).toStmt())).toEqual('(lhs == rhs);');
|
||||
expect(emitStmt(lhs.notEquals(rhs).toStmt())).toEqual('(lhs != rhs);');
|
||||
expect(emitStmt(lhs.identical(rhs).toStmt())).toEqual('(lhs === rhs);');
|
||||
expect(emitStmt(lhs.notIdentical(rhs).toStmt())).toEqual('(lhs !== rhs);');
|
||||
expect(emitStmt(lhs.minus(rhs).toStmt())).toEqual('(lhs - rhs);');
|
||||
expect(emitStmt(lhs.plus(rhs).toStmt())).toEqual('(lhs + rhs);');
|
||||
expect(emitStmt(lhs.divide(rhs).toStmt())).toEqual('(lhs / rhs);');
|
||||
expect(emitStmt(lhs.multiply(rhs).toStmt())).toEqual('(lhs * rhs);');
|
||||
expect(emitStmt(lhs.modulo(rhs).toStmt())).toEqual('(lhs % rhs);');
|
||||
expect(emitStmt(lhs.and(rhs).toStmt())).toEqual('(lhs && rhs);');
|
||||
expect(emitStmt(lhs.or(rhs).toStmt())).toEqual('(lhs || rhs);');
|
||||
expect(emitStmt(lhs.lower(rhs).toStmt())).toEqual('(lhs < rhs);');
|
||||
expect(emitStmt(lhs.lowerEquals(rhs).toStmt())).toEqual('(lhs <= rhs);');
|
||||
expect(emitStmt(lhs.bigger(rhs).toStmt())).toEqual('(lhs > rhs);');
|
||||
expect(emitStmt(lhs.biggerEquals(rhs).toStmt())).toEqual('(lhs >= rhs);');
|
||||
});
|
||||
|
||||
it('should support function expressions', () => {
|
||||
expect(emitStmt(o.fn([], []).toStmt())).toEqual(['():void => {', '};'].join('\n'));
|
||||
expect(emitStmt(o.fn([], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE).toStmt()))
|
||||
.toEqual(['():number => {', ' return 1;\n};'].join('\n'));
|
||||
expect(emitStmt(o.fn([new o.FnParam('param1', o.INT_TYPE)], []).toStmt())).toEqual([
|
||||
'(param1:number):void => {', '};'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support function statements', () => {
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], [
|
||||
]))).toEqual(['function someFn():void {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [], []), ['someFn'])).toEqual([
|
||||
'export function someFn():void {', '}'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt(
|
||||
'someFn', [], [new o.ReturnStatement(o.literal(1))], o.INT_TYPE)))
|
||||
.toEqual(['function someFn():number {', ' return 1;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.DeclareFunctionStmt('someFn', [new o.FnParam('param1', o.INT_TYPE)], [
|
||||
]))).toEqual(['function someFn(param1:number):void {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support comments', () => {
|
||||
expect(emitStmt(new o.CommentStmt('a\nb'))).toEqual(['// a', '// b'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support if stmt', () => {
|
||||
const trueCase = o.variable('trueCase').callFn([]).toStmt();
|
||||
const falseCase = o.variable('falseCase').callFn([]).toStmt();
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase]))).toEqual([
|
||||
'if (cond) { trueCase(); }'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.IfStmt(o.variable('cond'), [trueCase], [falseCase]))).toEqual([
|
||||
'if (cond) {', ' trueCase();', '} else {', ' falseCase();', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support try/catch', () => {
|
||||
const bodyStmt = o.variable('body').callFn([]).toStmt();
|
||||
const catchStmt =
|
||||
o.variable('catchFn').callFn([o.CATCH_ERROR_VAR, o.CATCH_STACK_VAR]).toStmt();
|
||||
expect(emitStmt(new o.TryCatchStmt([bodyStmt], [catchStmt]))).toEqual([
|
||||
'try {', ' body();', '} catch (error) {', ' const stack:any = error.stack;',
|
||||
' catchFn(error,stack);', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support support throwing',
|
||||
() => { expect(emitStmt(new o.ThrowStmt(someVar))).toEqual('throw someVar;'); });
|
||||
|
||||
describe('classes', () => {
|
||||
let callSomeMethod: o.Statement;
|
||||
|
||||
beforeEach(() => { callSomeMethod = o.THIS_EXPR.callMethod('someMethod', []).toStmt(); });
|
||||
|
||||
|
||||
it('should support declaring classes', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
||||
]))).toEqual(['class SomeClass {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, []), ['SomeClass']))
|
||||
.toEqual(['export class SomeClass {', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', o.variable('SomeSuperClass'), [], [], null, [
|
||||
]))).toEqual(['class SomeClass extends SomeSuperClass {', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring constructors', () => {
|
||||
const superCall = o.SUPER_EXPR.callFn([o.variable('someParam')]).toStmt();
|
||||
expect(emitStmt(
|
||||
new o.ClassStmt('SomeClass', null, [], [], new o.ClassMethod(null, [], []), [])))
|
||||
.toEqual(['class SomeClass {', ' constructor() {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [],
|
||||
new o.ClassMethod(null, [new o.FnParam('someParam', o.INT_TYPE)], []), [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' constructor(someParam:number) {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], new o.ClassMethod(null, [], [superCall]), [])))
|
||||
.toEqual([
|
||||
'class SomeClass {', ' constructor() {', ' super(someParam);', ' }', '}'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], new o.ClassMethod(null, [], [callSomeMethod]), [])))
|
||||
.toEqual([
|
||||
'class SomeClass {', ' constructor() {', ' this.someMethod();', ' }', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring fields', () => {
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [new o.ClassField('someField')], [], null, [])))
|
||||
.toEqual(['class SomeClass {', ' someField:any;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [new o.ClassField('someField', o.INT_TYPE)], [], null, [])))
|
||||
.toEqual(['class SomeClass {', ' someField:number;', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null,
|
||||
[new o.ClassField('someField', o.INT_TYPE, [o.StmtModifier.Private])], [], null,
|
||||
[])))
|
||||
.toEqual(['class SomeClass {', ' /*private*/ someField:number;', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support declaring getters', () => {
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [new o.ClassGetter('someGetter', [])], null, [])))
|
||||
.toEqual(['class SomeClass {', ' get someGetter():any {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [new o.ClassGetter('someGetter', [], o.INT_TYPE)], null,
|
||||
[])))
|
||||
.toEqual(['class SomeClass {', ' get someGetter():number {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [new o.ClassGetter('someGetter', [callSomeMethod])], null,
|
||||
[])))
|
||||
.toEqual([
|
||||
'class SomeClass {', ' get someGetter():any {', ' this.someMethod();', ' }', '}'
|
||||
].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [],
|
||||
[new o.ClassGetter('someGetter', [], null, [o.StmtModifier.Private])], null, [])))
|
||||
.toEqual(
|
||||
['class SomeClass {', ' private get someGetter():any {', ' }', '}'].join('\n'));
|
||||
});
|
||||
|
||||
it('should support methods', () => {
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
||||
new o.ClassMethod('someMethod', [], [])
|
||||
]))).toEqual(['class SomeClass {', ' someMethod():void {', ' }', '}'].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt('SomeClass', null, [], [], null, [
|
||||
new o.ClassMethod('someMethod', [], [], o.INT_TYPE)
|
||||
]))).toEqual(['class SomeClass {', ' someMethod():number {', ' }', '}'].join('\n'));
|
||||
expect(
|
||||
emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [new o.FnParam('someParam', o.INT_TYPE)], [])])))
|
||||
.toEqual([
|
||||
'class SomeClass {', ' someMethod(someParam:number):void {', ' }', '}'
|
||||
].join('\n'));
|
||||
expect(emitStmt(new o.ClassStmt(
|
||||
'SomeClass', null, [], [], null,
|
||||
[new o.ClassMethod('someMethod', [], [callSomeMethod])])))
|
||||
.toEqual([
|
||||
'class SomeClass {', ' someMethod():void {', ' this.someMethod();', ' }', '}'
|
||||
].join('\n'));
|
||||
});
|
||||
});
|
||||
|
||||
it('should support builtin types', () => {
|
||||
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.DYNAMIC_TYPE)))
|
||||
.toEqual('var a:any = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.BOOL_TYPE)))
|
||||
.toEqual('var a:boolean = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.INT_TYPE)))
|
||||
.toEqual('var a:number = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.NUMBER_TYPE)))
|
||||
.toEqual('var a:number = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.STRING_TYPE)))
|
||||
.toEqual('var a:string = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.FUNCTION_TYPE)))
|
||||
.toEqual('var a:Function = (null as any);');
|
||||
});
|
||||
|
||||
it('should support external types', () => {
|
||||
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(sameModuleIdentifier))))
|
||||
.toEqual('var a:someLocalId = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`var a:import0.someExternalId = (null as any);`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support `importAs` for external types', () => {
|
||||
spyOn(importResolver, 'getImportAs')
|
||||
.and.returnValue(new StaticSymbol('somePackage/importAsModule', 'importAsName', []));
|
||||
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(o.importType(externalModuleIdentifier)))).toEqual([
|
||||
`import * as import0 from 'somePackage/importAsModule';`,
|
||||
`var a:import0.importAsName = (null as any);`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support expression types', () => {
|
||||
expect(
|
||||
emitStmt(o.variable('a').set(o.NULL_EXPR).toDeclStmt(o.expressionType(o.variable('b')))))
|
||||
.toEqual('var a:b = (null as any);');
|
||||
});
|
||||
|
||||
it('should support expressions with type parameters', () => {
|
||||
expect(emitStmt(o.variable('a')
|
||||
.set(o.NULL_EXPR)
|
||||
.toDeclStmt(o.importType(externalModuleIdentifier, [o.STRING_TYPE]))))
|
||||
.toEqual([
|
||||
`import * as import0 from 'somePackage/someOtherPath';`,
|
||||
`var a:import0.someExternalId<string> = (null as any);`
|
||||
].join('\n'));
|
||||
});
|
||||
|
||||
it('should support combined types', () => {
|
||||
const writeVarExpr = o.variable('a').set(o.NULL_EXPR);
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(null))))
|
||||
.toEqual('var a:any[] = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.ArrayType(o.INT_TYPE))))
|
||||
.toEqual('var a:number[] = (null as any);');
|
||||
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(null))))
|
||||
.toEqual('var a:{[key: string]:any} = (null as any);');
|
||||
expect(emitStmt(writeVarExpr.toDeclStmt(new o.MapType(o.INT_TYPE))))
|
||||
.toEqual('var a:{[key: string]:number} = (null as any);');
|
||||
});
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user