angular-cn/packages/compiler/test/output/abstract_emitter_spec.ts
Tobias Bosch cdc882bd36 feat: introduce source maps for templates (#15011)
The main use case for the generated source maps is to give
errors a meaningful context in terms of the original source
that the user wrote.

Related changes that are included in this commit:

* renamed virtual folders used for jit:
  * ng://<module type>/module.ngfactory.js
  * ng://<module type>/<comp type>.ngfactory.js
  * ng://<module type>/<comp type>.html (for inline templates)
* error logging:
  * all errors that happen in templates are logged
    from the place of the nearest element.
  * instead of logging error messages and stacks separately,
    we log the actual error. This is needed so that browsers apply
    source maps to the stack correctly.
  * error type and error is logged as one log entry.

Note that long-stack-trace zone has a bug that 
disables source maps for stack traces,
see https://github.com/angular/zone.js/issues/661.

BREAKING CHANGE:

- DebugNode.source no more returns the source location of a node.  

Closes 14013
2017-03-14 09:16:15 -07:00

43 lines
1.6 KiB
TypeScript

/**
* @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 stripSourceMapAndNewLine(source: string): string {
if (source.endsWith('\n')) {
source = source.substring(0, source.length - 1);
}
const smi = source.lastIndexOf('\n//#');
if (smi == -1) return source;
return source.slice(0, smi);
}