source: trip-planner-front/node_modules/source-list-map/lib/SourceListMap.js@ 6a80231

Last change on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5"use strict";
6
7const CodeNode = require("./CodeNode");
8const SourceNode = require("./SourceNode");
9const MappingsContext = require("./MappingsContext");
10const getNumberOfLines = require("./helpers").getNumberOfLines;
11
12class SourceListMap {
13
14 constructor(generatedCode, source, originalSource) {
15 if(Array.isArray(generatedCode)) {
16 this.children = generatedCode;
17 } else {
18 this.children = [];
19 if(generatedCode || source)
20 this.add(generatedCode, source, originalSource);
21 }
22 }
23
24 add(generatedCode, source, originalSource) {
25 if(typeof generatedCode === "string") {
26 if(source) {
27 this.children.push(new SourceNode(generatedCode, source, originalSource));
28 } else if(this.children.length > 0 && this.children[this.children.length - 1] instanceof CodeNode) {
29 this.children[this.children.length - 1].addGeneratedCode(generatedCode);
30 } else {
31 this.children.push(new CodeNode(generatedCode));
32 }
33 } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
34 this.children.push(generatedCode);
35 } else if(generatedCode.children) {
36 generatedCode.children.forEach(function(sln) {
37 this.children.push(sln);
38 }, this);
39 } else {
40 throw new Error("Invalid arguments to SourceListMap.protfotype.add: Expected string, Node or SourceListMap");
41 }
42 };
43
44 preprend(generatedCode, source, originalSource) {
45 if(typeof generatedCode === "string") {
46 if(source) {
47 this.children.unshift(new SourceNode(generatedCode, source, originalSource));
48 } else if(this.children.length > 0 && this.children[this.children.length - 1].preprendGeneratedCode) {
49 this.children[this.children.length - 1].preprendGeneratedCode(generatedCode);
50 } else {
51 this.children.unshift(new CodeNode(generatedCode));
52 }
53 } else if(generatedCode.getMappings && generatedCode.getGeneratedCode) {
54 this.children.unshift(generatedCode);
55 } else if(generatedCode.children) {
56 generatedCode.children.slice().reverse().forEach(function(sln) {
57 this.children.unshift(sln);
58 }, this);
59 } else {
60 throw new Error("Invalid arguments to SourceListMap.protfotype.prerend: Expected string, Node or SourceListMap");
61 }
62 };
63
64 mapGeneratedCode(fn) {
65 const normalizedNodes = [];
66 this.children.forEach(function(sln) {
67 sln.getNormalizedNodes().forEach(function(newNode) {
68 normalizedNodes.push(newNode);
69 });
70 });
71 const optimizedNodes = [];
72 normalizedNodes.forEach(function(sln) {
73 sln = sln.mapGeneratedCode(fn);
74 if(optimizedNodes.length === 0) {
75 optimizedNodes.push(sln);
76 } else {
77 const last = optimizedNodes[optimizedNodes.length - 1];
78 const mergedNode = last.merge(sln);
79 if(mergedNode) {
80 optimizedNodes[optimizedNodes.length - 1] = mergedNode;
81 } else {
82 optimizedNodes.push(sln);
83 }
84 }
85 });
86 return new SourceListMap(optimizedNodes);
87 };
88
89 toString() {
90 return this.children.map(function(sln) {
91 return sln.getGeneratedCode();
92 }).join("");
93 };
94
95 toStringWithSourceMap(options) {
96 const mappingsContext = new MappingsContext();
97 const source = this.children.map(function(sln) {
98 return sln.getGeneratedCode();
99 }).join("");
100 const mappings = this.children.map(function(sln) {
101 return sln.getMappings(mappingsContext);
102 }).join("");
103 const arrays = mappingsContext.getArrays();
104 return {
105 source,
106 map: {
107 version: 3,
108 file: options && options.file,
109 sources: arrays.sources,
110 sourcesContent: mappingsContext.hasSourceContent ? arrays.sourcesContent : undefined,
111 mappings: mappings
112 }
113 };
114 }
115}
116
117module.exports = SourceListMap;
Note: See TracBrowser for help on using the repository browser.