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';
|
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-12-16 14:35:47 -05:00
|
|
|
import {extractSourceMap, originalPositionFor} from '@angular/compiler/testing/src/output/source_map_util';
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-03-15 18:50:30 -04:00
|
|
|
const someGenFilePath = 'somePackage/someGenFile';
|
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 emitter: TypeScriptEmitter;
|
|
|
|
let someVar: o.ReadVarExpr;
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-01-27 17:23:12 -05:00
|
|
|
beforeEach(() => {
|
2017-05-16 19:30:37 -04:00
|
|
|
emitter = new TypeScriptEmitter();
|
2017-01-27 17:23:12 -05:00
|
|
|
someVar = o.variable('someVar');
|
|
|
|
});
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-05-16 19:30:37 -04:00
|
|
|
function emitSourceMap(stmt: o.Statement | o.Statement[], preamble?: string): SourceMap {
|
2017-01-27 17:23:12 -05:00
|
|
|
const stmts = Array.isArray(stmt) ? stmt : [stmt];
|
2017-10-04 16:37:27 -04:00
|
|
|
const source = emitter.emitStatements(someGenFilePath, stmts, 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-05-16 19:30:37 -04:00
|
|
|
const sm = emitSourceMap(someVar.toStmt(), '/* MyPreamble \n */');
|
2016-12-12 13:49:17 -05:00
|
|
|
|
2017-10-04 16:37:27 -04:00
|
|
|
expect(sm.sources).toEqual([someGenFilePath, '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
|
|
|
}
|