1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | var SourceNode = require("source-map").SourceNode;
|
---|
8 | var SourceMapConsumer = require("source-map").SourceMapConsumer;
|
---|
9 | var SourceListMap = require("source-list-map").SourceListMap;
|
---|
10 | var Source = require("./Source");
|
---|
11 |
|
---|
12 | var SPLIT_REGEX = /(?!$)[^\n\r;{}]*[\n\r;{}]*/g;
|
---|
13 |
|
---|
14 | function _splitCode(code) {
|
---|
15 | return code.match(SPLIT_REGEX) || [];
|
---|
16 | }
|
---|
17 |
|
---|
18 | class OriginalSource extends Source {
|
---|
19 | constructor(value, name) {
|
---|
20 | super();
|
---|
21 | this._value = value;
|
---|
22 | this._name = name;
|
---|
23 | }
|
---|
24 |
|
---|
25 | source() {
|
---|
26 | return this._value;
|
---|
27 | }
|
---|
28 |
|
---|
29 | node(options) {
|
---|
30 | options = options || {};
|
---|
31 | var sourceMap = this._sourceMap;
|
---|
32 | var value = this._value;
|
---|
33 | var name = this._name;
|
---|
34 | var lines = value.split("\n");
|
---|
35 | var node = new SourceNode(null, null, null,
|
---|
36 | lines.map(function(line, idx) {
|
---|
37 | var pos = 0;
|
---|
38 | if(options.columns === false) {
|
---|
39 | var content = line + (idx != lines.length - 1 ? "\n" : "");
|
---|
40 | return new SourceNode(idx + 1, 0, name, content);
|
---|
41 | }
|
---|
42 | return new SourceNode(null, null, null,
|
---|
43 | _splitCode(line + (idx != lines.length - 1 ? "\n" : "")).map(function(item) {
|
---|
44 | if(/^\s*$/.test(item)) {
|
---|
45 | pos += item.length;
|
---|
46 | return item;
|
---|
47 | }
|
---|
48 | var res = new SourceNode(idx + 1, pos, name, item);
|
---|
49 | pos += item.length;
|
---|
50 | return res;
|
---|
51 | })
|
---|
52 | );
|
---|
53 | })
|
---|
54 | );
|
---|
55 | node.setSourceContent(name, value);
|
---|
56 | return node;
|
---|
57 | }
|
---|
58 |
|
---|
59 | listMap(options) {
|
---|
60 | return new SourceListMap(this._value, this._name, this._value)
|
---|
61 | }
|
---|
62 |
|
---|
63 | updateHash(hash) {
|
---|
64 | hash.update(this._value);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | require("./SourceAndMapMixin")(OriginalSource.prototype);
|
---|
69 |
|
---|
70 | module.exports = OriginalSource;
|
---|