2016-12-12 13:49:17 -05:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
import {ParseLocation, ParseSourceFile} from '@angular/compiler';
|
|
|
|
import {StaticSymbol} from '@angular/compiler/src/aot/static_symbol';
|
2016-12-12 13:49:17 -05:00
|
|
|
import * as o from '@angular/compiler/src/output/output_ast';
|
|
|
|
import {ImportResolver} from '@angular/compiler/src/output/path_util';
|
2017-01-27 17:23:12 -05:00
|
|
|
import {SourceMap} from '@angular/compiler/src/output/source_map';
|
2016-12-12 13:49:17 -05:00
|
|
|
import {TypeScriptEmitter} from '@angular/compiler/src/output/ts_emitter';
|
2017-01-27 17:23:12 -05:00
|
|
|
import {ParseSourceSpan} from '@angular/compiler/src/parse_util';
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-03-14 12:16:15 -04:00
|
|
|
import {extractSourceMap, originalPositionFor} from './source_map_util';
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
const someModuleUrl = 'somePackage/somePath';
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
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; }
|
|
|
|
}
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
export function main() {
|
|
|
|
// Not supported features of our OutputAst in TS:
|
|
|
|
// - real `const` like in Dart
|
|
|
|
// - final fields
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
describe('TypeScriptEmitter', () => {
|
|
|
|
let importResolver: ImportResolver;
|
|
|
|
let emitter: TypeScriptEmitter;
|
|
|
|
let someVar: o.ReadVarExpr;
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
beforeEach(() => {
|
|
|
|
importResolver = new SimpleJsImportGenerator();
|
|
|
|
emitter = new TypeScriptEmitter(importResolver);
|
|
|
|
someVar = o.variable('someVar');
|
|
|
|
});
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
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);
|
|
|
|
}
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
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());
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
expect(sm.sources).toEqual(['in.js']);
|
|
|
|
expect(sm.sourcesContent).toEqual([';;;var']);
|
2017-03-14 12:16:15 -04:00
|
|
|
expect(originalPositionFor(sm, {line: 1, column: 0}))
|
|
|
|
.toEqual({line: 1, column: 3, source: 'in.js'});
|
2017-01-27 17:23:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-15 12:12:40 -05:00
|
|
|
}
|