@jsenv/urls 2.2.1 → 2.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/main.js +1 -5
- package/src/url_trace.js +4 -187
package/package.json
CHANGED
package/src/main.js
CHANGED
package/src/url_trace.js
CHANGED
|
@@ -3,41 +3,11 @@
|
|
|
3
3
|
// https://github.com/postcss/postcss/blob/fd30d3df5abc0954a0ec642a3cdc644ab2aacf9c/lib/terminal-highlight.js#L50
|
|
4
4
|
// https://github.com/babel/babel/blob/eea156b2cb8deecfcf82d52aa1b71ba4995c7d68/packages/babel-code-frame/src/index.js#L1
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
let string = "";
|
|
8
|
-
trace.forEach(({ type, url, line, column, source }, index) => {
|
|
9
|
-
if (index === 0) {
|
|
10
|
-
string += stringifyUrlSite({ url, line, column, source });
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
if (type === "import") {
|
|
15
|
-
string += `\n imported by ${stringifyUrlSite({
|
|
16
|
-
url,
|
|
17
|
-
line,
|
|
18
|
-
column,
|
|
19
|
-
})}`;
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
string += `\n referenced by ${stringifyUrlSite({
|
|
24
|
-
url,
|
|
25
|
-
line,
|
|
26
|
-
column,
|
|
27
|
-
})}`;
|
|
28
|
-
}, "");
|
|
29
|
-
|
|
30
|
-
return string;
|
|
31
|
-
};
|
|
6
|
+
import { inspectFileContent } from "@jsenv/inspect";
|
|
32
7
|
|
|
33
8
|
export const stringifyUrlSite = (
|
|
34
9
|
{ url, line, column, content },
|
|
35
|
-
{
|
|
36
|
-
showCodeFrame = true,
|
|
37
|
-
numberOfSurroundingLinesToShow,
|
|
38
|
-
lineMaxLength,
|
|
39
|
-
color,
|
|
40
|
-
} = {},
|
|
10
|
+
{ showCodeFrame = true, ...params } = {},
|
|
41
11
|
) => {
|
|
42
12
|
let string = url;
|
|
43
13
|
|
|
@@ -52,165 +22,12 @@ export const stringifyUrlSite = (
|
|
|
52
22
|
return string;
|
|
53
23
|
}
|
|
54
24
|
|
|
55
|
-
const sourceLoc =
|
|
25
|
+
const sourceLoc = inspectFileContent({
|
|
56
26
|
content,
|
|
57
27
|
line,
|
|
58
28
|
column,
|
|
59
|
-
|
|
60
|
-
lineMaxLength,
|
|
61
|
-
color,
|
|
29
|
+
params,
|
|
62
30
|
});
|
|
63
|
-
|
|
64
31
|
return `${string}
|
|
65
32
|
${sourceLoc}`;
|
|
66
33
|
};
|
|
67
|
-
|
|
68
|
-
export const showSourceLocation = ({
|
|
69
|
-
content,
|
|
70
|
-
line,
|
|
71
|
-
column,
|
|
72
|
-
numberOfSurroundingLinesToShow = 1,
|
|
73
|
-
lineMaxLength = 120,
|
|
74
|
-
} = {}) => {
|
|
75
|
-
let mark = (string) => string;
|
|
76
|
-
let aside = (string) => string;
|
|
77
|
-
// if (color) {
|
|
78
|
-
// mark = (string) => ANSI.color(string, ANSI.RED)
|
|
79
|
-
// aside = (string) => ANSI.color(string, ANSI.GREY)
|
|
80
|
-
// }
|
|
81
|
-
|
|
82
|
-
const lines = content.split(/\r?\n/);
|
|
83
|
-
if (line === 0) line = 1;
|
|
84
|
-
let lineRange = {
|
|
85
|
-
start: line - 1,
|
|
86
|
-
end: line,
|
|
87
|
-
};
|
|
88
|
-
lineRange = moveLineRangeUp(lineRange, numberOfSurroundingLinesToShow);
|
|
89
|
-
lineRange = moveLineRangeDown(lineRange, numberOfSurroundingLinesToShow);
|
|
90
|
-
lineRange = lineRangeWithinLines(lineRange, lines);
|
|
91
|
-
const linesToShow = lines.slice(lineRange.start, lineRange.end);
|
|
92
|
-
const endLineNumber = lineRange.end;
|
|
93
|
-
const lineNumberMaxWidth = String(endLineNumber).length;
|
|
94
|
-
|
|
95
|
-
if (column === 0) column = 1;
|
|
96
|
-
|
|
97
|
-
const columnRange = {};
|
|
98
|
-
if (column === undefined) {
|
|
99
|
-
columnRange.start = 0;
|
|
100
|
-
columnRange.end = lineMaxLength;
|
|
101
|
-
} else if (column > lineMaxLength) {
|
|
102
|
-
columnRange.start = column - Math.floor(lineMaxLength / 2);
|
|
103
|
-
columnRange.end = column + Math.ceil(lineMaxLength / 2);
|
|
104
|
-
} else {
|
|
105
|
-
columnRange.start = 0;
|
|
106
|
-
columnRange.end = lineMaxLength;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return linesToShow.map((lineSource, index) => {
|
|
110
|
-
const lineNumber = lineRange.start + index + 1;
|
|
111
|
-
const isMainLine = lineNumber === line;
|
|
112
|
-
const lineSourceTruncated = applyColumnRange(columnRange, lineSource);
|
|
113
|
-
const lineNumberWidth = String(lineNumber).length;
|
|
114
|
-
// ensure if line moves from 7,8,9 to 10 the display is still great
|
|
115
|
-
const lineNumberRightSpacing = " ".repeat(
|
|
116
|
-
lineNumberMaxWidth - lineNumberWidth,
|
|
117
|
-
);
|
|
118
|
-
const asideSource = `${lineNumber}${lineNumberRightSpacing} |`;
|
|
119
|
-
const lineFormatted = `${aside(asideSource)} ${lineSourceTruncated}`;
|
|
120
|
-
if (isMainLine) {
|
|
121
|
-
if (column === undefined) {
|
|
122
|
-
return `${mark(">")} ${lineFormatted}`;
|
|
123
|
-
}
|
|
124
|
-
const spacing = stringToSpaces(
|
|
125
|
-
`${asideSource} ${lineSourceTruncated.slice(
|
|
126
|
-
0,
|
|
127
|
-
column - columnRange.start - 1,
|
|
128
|
-
)}`,
|
|
129
|
-
);
|
|
130
|
-
return `${mark(">")} ${lineFormatted}
|
|
131
|
-
${spacing}${mark("^")}`;
|
|
132
|
-
}
|
|
133
|
-
return ` ${lineFormatted}`;
|
|
134
|
-
}).join(`
|
|
135
|
-
`);
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
const applyColumnRange = ({ start, end }, line) => {
|
|
139
|
-
if (typeof start !== "number") {
|
|
140
|
-
throw new TypeError(`start must be a number, received ${start}`);
|
|
141
|
-
}
|
|
142
|
-
if (typeof end !== "number") {
|
|
143
|
-
throw new TypeError(`end must be a number, received ${end}`);
|
|
144
|
-
}
|
|
145
|
-
if (end < start) {
|
|
146
|
-
throw new Error(
|
|
147
|
-
`end must be greater than start, but ${end} is smaller than ${start}`,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
const prefix = "…";
|
|
152
|
-
const suffix = "…";
|
|
153
|
-
const lastIndex = line.length;
|
|
154
|
-
|
|
155
|
-
if (line.length === 0) {
|
|
156
|
-
// don't show any ellipsis if the line is empty
|
|
157
|
-
// because it's not truncated in that case
|
|
158
|
-
return "";
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const startTruncated = start > 0;
|
|
162
|
-
const endTruncated = lastIndex > end;
|
|
163
|
-
|
|
164
|
-
let from = startTruncated ? start + prefix.length : start;
|
|
165
|
-
let to = endTruncated ? end - suffix.length : end;
|
|
166
|
-
if (to > lastIndex) to = lastIndex;
|
|
167
|
-
|
|
168
|
-
if (start >= lastIndex || from === to) {
|
|
169
|
-
return "";
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
let result = "";
|
|
173
|
-
while (from < to) {
|
|
174
|
-
result += line[from];
|
|
175
|
-
from++;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (result.length === 0) {
|
|
179
|
-
return "";
|
|
180
|
-
}
|
|
181
|
-
if (startTruncated && endTruncated) {
|
|
182
|
-
return `${prefix}${result}${suffix}`;
|
|
183
|
-
}
|
|
184
|
-
if (startTruncated) {
|
|
185
|
-
return `${prefix}${result}`;
|
|
186
|
-
}
|
|
187
|
-
if (endTruncated) {
|
|
188
|
-
return `${result}${suffix}`;
|
|
189
|
-
}
|
|
190
|
-
return result;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
const stringToSpaces = (string) => string.replace(/[^\t]/g, " ");
|
|
194
|
-
|
|
195
|
-
// const getLineRangeLength = ({ start, end }) => end - start
|
|
196
|
-
|
|
197
|
-
const moveLineRangeUp = ({ start, end }, number) => {
|
|
198
|
-
return {
|
|
199
|
-
start: start - number,
|
|
200
|
-
end,
|
|
201
|
-
};
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const moveLineRangeDown = ({ start, end }, number) => {
|
|
205
|
-
return {
|
|
206
|
-
start,
|
|
207
|
-
end: end + number,
|
|
208
|
-
};
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
const lineRangeWithinLines = ({ start, end }, lines) => {
|
|
212
|
-
return {
|
|
213
|
-
start: start < 0 ? 0 : start,
|
|
214
|
-
end: end > lines.length ? lines.length : end,
|
|
215
|
-
};
|
|
216
|
-
};
|