"Fossies" - the Fresh Open Source Software Archive 
Member "angular-13.3.9/packages/compiler/test/output/abstract_emitter_node_only_spec.ts" (18 May 2022, 5167 Bytes) of package /linux/www/angular-13.3.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8
9 import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '@angular/compiler';
10 import {EmitterVisitorContext} from '@angular/compiler/src/output/abstract_emitter';
11 import {SourceMap} from '@angular/compiler/src/output/source_map';
12
13 import {extractSourceMap, originalPositionFor} from './source_map_util';
14
15 {
16 describe('AbstractEmitter', () => {
17 describe('EmitterVisitorContext', () => {
18 const fileA = new ParseSourceFile('a0a1a2a3a4a5a6a7a8a9', 'a.js');
19 const fileB = new ParseSourceFile('b0b1b2b3b4b5b6b7b8b9', 'b.js');
20 let ctx: EmitterVisitorContext;
21
22 beforeEach(() => {
23 ctx = EmitterVisitorContext.createRoot();
24 });
25
26 it('should add source files to the source map', () => {
27 ctx.print(createSourceSpan(fileA, 0), 'o0');
28 ctx.print(createSourceSpan(fileA, 1), 'o1');
29 ctx.print(createSourceSpan(fileB, 0), 'o2');
30 ctx.print(createSourceSpan(fileB, 1), 'o3');
31 const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!;
32 expect(sm.sources).toEqual([fileA.url, fileB.url]);
33 expect(sm.sourcesContent).toEqual([fileA.content, fileB.content]);
34 });
35
36 it('should generate a valid mapping', () => {
37 ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
38 ctx.println(createSourceSpan(fileB, 1), 'fileB-1');
39 ctx.print(createSourceSpan(fileA, 2), 'fileA-2');
40
41 expectMap(ctx, 0, 0, 'a.js', 0, 0);
42 expectMap(ctx, 0, 7, 'b.js', 0, 2);
43 expectMap(ctx, 1, 0, 'a.js', 0, 4);
44 });
45
46 it('should be able to shift the content', () => {
47 ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
48
49 const sm = ctx.toSourceMapGenerator('o.ts', 10).toJSON()!;
50 expect(originalPositionFor(sm, {line: 11, column: 0})).toEqual({
51 line: 1,
52 column: 0,
53 source: 'a.js',
54 });
55 });
56
57 it('should use the default source file for the first character', () => {
58 ctx.print(null, 'fileA-0');
59 expectMap(ctx, 0, 0, 'o.ts', 0, 0);
60 });
61
62 it('should use an explicit mapping for the first character', () => {
63 ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
64 expectMap(ctx, 0, 0, 'a.js', 0, 0);
65 });
66
67 it('should map leading segment without span', () => {
68 ctx.print(null, '....');
69 ctx.print(createSourceSpan(fileA, 0), 'fileA-0');
70
71 expectMap(ctx, 0, 0, 'o.ts', 0, 0);
72 expectMap(ctx, 0, 4, 'a.js', 0, 0);
73 expect(nbSegmentsPerLine(ctx)).toEqual([2]);
74 });
75
76 it('should handle indent', () => {
77 ctx.incIndent();
78 ctx.println(createSourceSpan(fileA, 0), 'fileA-0');
79 ctx.incIndent();
80 ctx.println(createSourceSpan(fileA, 1), 'fileA-1');
81 ctx.decIndent();
82 ctx.println(createSourceSpan(fileA, 2), 'fileA-2');
83
84 expectMap(ctx, 0, 0, 'o.ts', 0, 0);
85 expectMap(ctx, 0, 2, 'a.js', 0, 0);
86 expectMap(ctx, 1, 0);
87 expectMap(ctx, 1, 2);
88 expectMap(ctx, 1, 4, 'a.js', 0, 2);
89 expectMap(ctx, 2, 0);
90 expectMap(ctx, 2, 2, 'a.js', 0, 4);
91
92 expect(nbSegmentsPerLine(ctx)).toEqual([2, 1, 1]);
93 });
94
95 it('should coalesce identical span', () => {
96 const span = createSourceSpan(fileA, 0);
97 ctx.print(span, 'fileA-0');
98 ctx.print(null, '...');
99 ctx.print(span, 'fileA-0');
100 ctx.print(createSourceSpan(fileB, 0), 'fileB-0');
101
102 expectMap(ctx, 0, 0, 'a.js', 0, 0);
103 expectMap(ctx, 0, 7, 'a.js', 0, 0);
104 expectMap(ctx, 0, 10, 'a.js', 0, 0);
105 expectMap(ctx, 0, 17, 'b.js', 0, 0);
106
107 expect(nbSegmentsPerLine(ctx)).toEqual([2]);
108 });
109 });
110 });
111 }
112
113
114 // All lines / columns indexes are 0-based
115 // Note: source-map line indexes are 1-based, column 0-based
116 function expectMap(
117 ctx: EmitterVisitorContext, genLine: number, genCol: number, source: string|null = null,
118 srcLine: number|null = null, srcCol: number|null = null) {
119 const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!;
120 const genPosition = {line: genLine + 1, column: genCol};
121 const origPosition = originalPositionFor(sm, genPosition);
122 // TODO: Review use of `any` here (#19904)
123 expect(origPosition.source as any).toEqual(source);
124 expect(origPosition.line as any).toEqual(srcLine === null ? null : srcLine + 1);
125 expect(origPosition.column as any).toEqual(srcCol);
126 }
127
128 // returns the number of segments per line
129 function nbSegmentsPerLine(ctx: EmitterVisitorContext) {
130 const sm = ctx.toSourceMapGenerator('o.ts').toJSON()!;
131 const lines = sm.mappings.split(';');
132 return lines.map(l => {
133 const m = l.match(/,/g);
134 return m === null ? 1 : m.length + 1;
135 });
136 }
137
138 function createSourceSpan(file: ParseSourceFile, idx: number) {
139 const col = 2 * idx;
140 const start = new ParseLocation(file, col, 0, col);
141 const end = new ParseLocation(file, col + 2, 0, col + 2);
142 const sourceSpan = new ParseSourceSpan(start, end);
143 return {sourceSpan};
144 }