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-03-15 18:50:30 -04:00
|
|
|
const someGenFilePath = 'somePackage/someGenFile';
|
|
|
|
const someSourceFilePath = 'somePackage/someSourceFile';
|
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;
|
|
|
|
}
|
2017-03-24 12:59:58 -04:00
|
|
|
getImportAs(symbol: StaticSymbol): StaticSymbol|null { return null; }
|
|
|
|
getTypeArity(symbol: StaticSymbol): number|null { return null; }
|
2017-01-27 17:23:12 -05:00
|
|
|
}
|
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(
|
2017-03-24 12:59:58 -04:00
|
|
|
stmt: o.Statement | o.Statement[], exportedVars: string[] | null = null,
|
2017-03-15 18:50:30 -04:00
|
|
|
preamble?: string): SourceMap {
|
2017-01-27 17:23:12 -05:00
|
|
|
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
2017-03-15 18:50:30 -04:00
|
|
|
const source = emitter.emitStatements(
|
|
|
|
someSourceFilePath, someGenFilePath, stmts, exportedVars || [], preamble);
|
2017-03-24 12:59:58 -04:00
|
|
|
return extractSourceMap(source) !;
|
2017-01-27 17:23:12 -05:00
|
|
|
}
|
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);
|
2017-03-15 18:50:30 -04:00
|
|
|
const sm = emitSourceMap(someVar.toStmt(), [], '/* MyPreamble \n */');
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-03-15 18:50:30 -04:00
|
|
|
expect(sm.sources).toEqual([someSourceFilePath, 'in.js']);
|
2017-03-17 12:44:27 -04:00
|
|
|
expect(sm.sourcesContent).toEqual([' ', ';;;var']);
|
2017-03-15 18:50:30 -04:00
|
|
|
expect(originalPositionFor(sm, {line: 3, column: 0}))
|
2017-03-14 12:16:15 -04:00
|
|
|
.toEqual({line: 1, column: 3, source: 'in.js'});
|
2017-01-27 17:23:12 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-12-15 12:12:40 -05:00
|
|
|
}
|