@atlaspack/diagnostic 2.14.2-typescript-d6e6d169c.0 → 2.14.2-typescript-6de04fbae.0
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/lib/{index.d.ts → index.d.mts} +2 -1
- package/lib/index.js +267 -282
- package/lib/index.mjs +144 -142
- package/package.json +5 -4
- package/src/index.mts +1 -1
|
@@ -38,7 +38,7 @@ export type DiagnosticCodeFrame = {
|
|
|
38
38
|
codeHighlights: Array<DiagnosticCodeHighlight>;
|
|
39
39
|
};
|
|
40
40
|
/** A JSON object (as in "map") */
|
|
41
|
-
|
|
41
|
+
type JSONObject = {
|
|
42
42
|
[key: string]: any;
|
|
43
43
|
};
|
|
44
44
|
/**
|
|
@@ -160,3 +160,4 @@ export interface MdFunction {
|
|
|
160
160
|
strikethrough: (s: TemplateInput) => TemplateInput;
|
|
161
161
|
}
|
|
162
162
|
export declare const md: MdFunction;
|
|
163
|
+
export {};
|
package/lib/index.js
CHANGED
|
@@ -1,325 +1,310 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
4
|
});
|
|
5
5
|
function _export(target, all) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
get: all[name],
|
|
6
|
+
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: all[name]
|
|
10
9
|
});
|
|
11
10
|
}
|
|
12
11
|
_export(exports, {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
12
|
+
anyToDiagnostic: function() {
|
|
13
|
+
return anyToDiagnostic;
|
|
14
|
+
},
|
|
15
|
+
convertSourceLocationToHighlight: function() {
|
|
16
|
+
return convertSourceLocationToHighlight;
|
|
17
|
+
},
|
|
18
|
+
/**
|
|
19
|
+
* An error wrapper around a diagnostic that can be <code>throw</code>n (e.g. to signal a
|
|
20
|
+
* build error).
|
|
21
|
+
*/ default: function() {
|
|
22
|
+
return ThrowableDiagnostic;
|
|
23
|
+
},
|
|
24
|
+
encodeJSONKeyComponent: function() {
|
|
25
|
+
return encodeJSONKeyComponent;
|
|
26
|
+
},
|
|
27
|
+
errorToDiagnostic: function() {
|
|
28
|
+
return errorToDiagnostic;
|
|
29
|
+
},
|
|
30
|
+
escapeMarkdown: function() {
|
|
31
|
+
return escapeMarkdown;
|
|
32
|
+
},
|
|
33
|
+
generateJSONCodeHighlights: function() {
|
|
34
|
+
return generateJSONCodeHighlights;
|
|
35
|
+
},
|
|
36
|
+
getJSONHighlightLocation: function() {
|
|
37
|
+
return getJSONHighlightLocation;
|
|
38
|
+
},
|
|
39
|
+
getJSONSourceLocation: function() {
|
|
40
|
+
return getJSONSourceLocation;
|
|
41
|
+
},
|
|
42
|
+
md: function() {
|
|
43
|
+
return md;
|
|
44
|
+
}
|
|
46
45
|
});
|
|
47
|
-
const _nodeassert = /*#__PURE__*/ _interop_require_default(
|
|
48
|
-
|
|
49
|
-
);
|
|
50
|
-
const _nullthrows = /*#__PURE__*/ _interop_require_default(
|
|
51
|
-
require('nullthrows'),
|
|
52
|
-
);
|
|
53
|
-
const _jsonsourcemap = /*#__PURE__*/ _interop_require_wildcard(
|
|
54
|
-
require('@mischnic/json-sourcemap'),
|
|
55
|
-
);
|
|
46
|
+
const _nodeassert = /*#__PURE__*/ _interop_require_default(require("node:assert"));
|
|
47
|
+
const _nullthrows = /*#__PURE__*/ _interop_require_default(require("nullthrows"));
|
|
48
|
+
const _jsonsourcemap = /*#__PURE__*/ _interop_require_wildcard(require("@mischnic/json-sourcemap"));
|
|
56
49
|
function _interop_require_default(obj) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
default: obj,
|
|
61
|
-
};
|
|
50
|
+
return obj && obj.__esModule ? obj : {
|
|
51
|
+
default: obj
|
|
52
|
+
};
|
|
62
53
|
}
|
|
63
54
|
function _getRequireWildcardCache(nodeInterop) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
55
|
+
if (typeof WeakMap !== "function") return null;
|
|
56
|
+
var cacheBabelInterop = new WeakMap();
|
|
57
|
+
var cacheNodeInterop = new WeakMap();
|
|
58
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
59
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
60
|
+
})(nodeInterop);
|
|
70
61
|
}
|
|
71
62
|
function _interop_require_wildcard(obj, nodeInterop) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
63
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
64
|
+
return obj;
|
|
65
|
+
}
|
|
66
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
67
|
+
return {
|
|
68
|
+
default: obj
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
72
|
+
if (cache && cache.has(obj)) {
|
|
73
|
+
return cache.get(obj);
|
|
74
|
+
}
|
|
75
|
+
var newObj = {
|
|
76
|
+
__proto__: null
|
|
78
77
|
};
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
: null;
|
|
94
|
-
if (desc && (desc.get || desc.set)) {
|
|
95
|
-
Object.defineProperty(newObj, key, desc);
|
|
96
|
-
} else {
|
|
97
|
-
newObj[key] = obj[key];
|
|
98
|
-
}
|
|
78
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
79
|
+
for(var key in obj){
|
|
80
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
81
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
82
|
+
if (desc && (desc.get || desc.set)) {
|
|
83
|
+
Object.defineProperty(newObj, key, desc);
|
|
84
|
+
} else {
|
|
85
|
+
newObj[key] = obj[key];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
newObj.default = obj;
|
|
90
|
+
if (cache) {
|
|
91
|
+
cache.set(obj, newObj);
|
|
99
92
|
}
|
|
100
|
-
|
|
101
|
-
newObj.default = obj;
|
|
102
|
-
if (cache) {
|
|
103
|
-
cache.set(obj, newObj);
|
|
104
|
-
}
|
|
105
|
-
return newObj;
|
|
93
|
+
return newObj;
|
|
106
94
|
}
|
|
107
95
|
function anyToDiagnostic(input) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
96
|
+
if (Array.isArray(input)) {
|
|
97
|
+
return input.flatMap((e)=>anyToDiagnostic(e));
|
|
98
|
+
} else if (input instanceof ThrowableDiagnostic) {
|
|
99
|
+
return input.diagnostics;
|
|
100
|
+
} else if (input instanceof Error) {
|
|
101
|
+
return errorToDiagnostic(input);
|
|
102
|
+
} else if (typeof input === 'string') {
|
|
103
|
+
return [
|
|
104
|
+
{
|
|
105
|
+
message: input
|
|
106
|
+
}
|
|
107
|
+
];
|
|
108
|
+
} else if (typeof input === 'object') {
|
|
109
|
+
return [
|
|
110
|
+
input
|
|
111
|
+
];
|
|
112
|
+
} else {
|
|
113
|
+
return errorToDiagnostic(input);
|
|
114
|
+
}
|
|
125
115
|
}
|
|
126
116
|
function errorToDiagnostic(error, defaultValues) {
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
let codeFrames = undefined;
|
|
118
|
+
if (typeof error === 'string') {
|
|
119
|
+
return [
|
|
120
|
+
{
|
|
121
|
+
origin: defaultValues?.origin ?? 'Error',
|
|
122
|
+
message: escapeMarkdown(error)
|
|
123
|
+
}
|
|
124
|
+
];
|
|
125
|
+
}
|
|
126
|
+
if (error instanceof ThrowableDiagnostic) {
|
|
127
|
+
return error.diagnostics.map((d)=>{
|
|
128
|
+
return {
|
|
129
|
+
...d,
|
|
130
|
+
origin: d.origin ?? defaultValues?.origin ?? 'unknown'
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
if (error.loc && error.source != null) {
|
|
135
|
+
codeFrames = [
|
|
136
|
+
{
|
|
137
|
+
filePath: error.filePath ?? error.fileName ?? defaultValues?.filePath ?? undefined,
|
|
138
|
+
code: error.source,
|
|
139
|
+
codeHighlights: [
|
|
140
|
+
{
|
|
141
|
+
start: {
|
|
142
|
+
line: error.loc.line,
|
|
143
|
+
column: error.loc.column
|
|
144
|
+
},
|
|
145
|
+
end: {
|
|
146
|
+
line: error.loc.line,
|
|
147
|
+
column: error.loc.column
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
];
|
|
153
|
+
}
|
|
129
154
|
return [
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
{
|
|
156
|
+
origin: defaultValues?.origin ?? 'Error',
|
|
157
|
+
message: escapeMarkdown(error.message),
|
|
158
|
+
name: error.name,
|
|
159
|
+
stack: codeFrames == null ? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack : undefined,
|
|
160
|
+
codeFrames
|
|
161
|
+
}
|
|
134
162
|
];
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
163
|
+
}
|
|
164
|
+
class ThrowableDiagnostic extends Error {
|
|
165
|
+
diagnostics;
|
|
166
|
+
constructor(opts){
|
|
167
|
+
let diagnostics = Array.isArray(opts.diagnostic) ? opts.diagnostic : [
|
|
168
|
+
opts.diagnostic
|
|
169
|
+
];
|
|
170
|
+
// Construct error from diagnostics
|
|
171
|
+
super(diagnostics[0].message);
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
this.stack = diagnostics[0].stack ?? super.stack;
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
this.name = diagnostics[0].name ?? super.name;
|
|
176
|
+
this.diagnostics = diagnostics;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function generateJSONCodeHighlights(data, ids) {
|
|
180
|
+
let map = typeof data == 'string' ? _jsonsourcemap.default.parse(data, undefined, {
|
|
181
|
+
dialect: 'JSON5',
|
|
182
|
+
tabWidth: 1
|
|
183
|
+
}) : data;
|
|
184
|
+
return ids.map(({ key, type, message })=>{
|
|
185
|
+
let pos = _nullthrows.default.default(map.pointers[key]);
|
|
186
|
+
return {
|
|
187
|
+
...getJSONHighlightLocation(pos, type),
|
|
188
|
+
message
|
|
189
|
+
};
|
|
142
190
|
});
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
defaultValues?.filePath ??
|
|
151
|
-
undefined,
|
|
152
|
-
code: error.source,
|
|
153
|
-
codeHighlights: [
|
|
154
|
-
{
|
|
191
|
+
}
|
|
192
|
+
function getJSONHighlightLocation(pos, type) {
|
|
193
|
+
let key = 'key' in pos ? pos.key : undefined;
|
|
194
|
+
let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
|
|
195
|
+
if (!type && key && pos.value) {
|
|
196
|
+
// key and value
|
|
197
|
+
return {
|
|
155
198
|
start: {
|
|
156
|
-
|
|
157
|
-
|
|
199
|
+
line: key.line + 1,
|
|
200
|
+
column: key.column + 1
|
|
158
201
|
},
|
|
159
202
|
end: {
|
|
160
|
-
|
|
161
|
-
|
|
203
|
+
line: pos.valueEnd.line + 1,
|
|
204
|
+
column: pos.valueEnd.column
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
} else if (type == 'key' || !pos.value) {
|
|
208
|
+
(0, _nodeassert.default)(key && keyEnd);
|
|
209
|
+
return {
|
|
210
|
+
start: {
|
|
211
|
+
line: key.line + 1,
|
|
212
|
+
column: key.column + 1
|
|
162
213
|
},
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
class ThrowableDiagnostic extends Error {
|
|
182
|
-
diagnostics;
|
|
183
|
-
constructor(opts) {
|
|
184
|
-
let diagnostics = Array.isArray(opts.diagnostic)
|
|
185
|
-
? opts.diagnostic
|
|
186
|
-
: [opts.diagnostic];
|
|
187
|
-
// Construct error from diagnostics
|
|
188
|
-
super(diagnostics[0].message);
|
|
189
|
-
// @ts-ignore
|
|
190
|
-
this.stack = diagnostics[0].stack ?? super.stack;
|
|
191
|
-
// @ts-ignore
|
|
192
|
-
this.name = diagnostics[0].name ?? super.name;
|
|
193
|
-
this.diagnostics = diagnostics;
|
|
194
|
-
}
|
|
214
|
+
end: {
|
|
215
|
+
line: keyEnd.line + 1,
|
|
216
|
+
column: keyEnd.column
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
} else {
|
|
220
|
+
return {
|
|
221
|
+
start: {
|
|
222
|
+
line: pos.value.line + 1,
|
|
223
|
+
column: pos.value.column + 1
|
|
224
|
+
},
|
|
225
|
+
end: {
|
|
226
|
+
line: pos.valueEnd.line + 1,
|
|
227
|
+
column: pos.valueEnd.column
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
195
231
|
}
|
|
196
|
-
function
|
|
197
|
-
|
|
198
|
-
typeof data == 'string'
|
|
199
|
-
? _jsonsourcemap.default.parse(data, undefined, {
|
|
200
|
-
dialect: 'JSON5',
|
|
201
|
-
tabWidth: 1,
|
|
202
|
-
})
|
|
203
|
-
: data;
|
|
204
|
-
return ids.map(({key, type, message}) => {
|
|
205
|
-
let pos = _nullthrows.default.default(map.pointers[key]);
|
|
232
|
+
function getJSONSourceLocation(pos, type) {
|
|
233
|
+
let v = getJSONHighlightLocation(pos, type);
|
|
206
234
|
return {
|
|
207
|
-
|
|
208
|
-
|
|
235
|
+
start: v.start,
|
|
236
|
+
end: {
|
|
237
|
+
line: v.end.line,
|
|
238
|
+
column: v.end.column + 1
|
|
239
|
+
}
|
|
209
240
|
};
|
|
210
|
-
});
|
|
211
241
|
}
|
|
212
|
-
function
|
|
213
|
-
let key = 'key' in pos ? pos.key : undefined;
|
|
214
|
-
let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
|
|
215
|
-
if (!type && key && pos.value) {
|
|
216
|
-
// key and value
|
|
217
|
-
return {
|
|
218
|
-
start: {
|
|
219
|
-
line: key.line + 1,
|
|
220
|
-
column: key.column + 1,
|
|
221
|
-
},
|
|
222
|
-
end: {
|
|
223
|
-
line: pos.valueEnd.line + 1,
|
|
224
|
-
column: pos.valueEnd.column,
|
|
225
|
-
},
|
|
226
|
-
};
|
|
227
|
-
} else if (type == 'key' || !pos.value) {
|
|
228
|
-
(0, _nodeassert.default)(key && keyEnd);
|
|
229
|
-
return {
|
|
230
|
-
start: {
|
|
231
|
-
line: key.line + 1,
|
|
232
|
-
column: key.column + 1,
|
|
233
|
-
},
|
|
234
|
-
end: {
|
|
235
|
-
line: keyEnd.line + 1,
|
|
236
|
-
column: keyEnd.column,
|
|
237
|
-
},
|
|
238
|
-
};
|
|
239
|
-
} else {
|
|
242
|
+
function convertSourceLocationToHighlight({ start, end }, message) {
|
|
240
243
|
return {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
column: pos.valueEnd.column,
|
|
248
|
-
},
|
|
244
|
+
message,
|
|
245
|
+
start,
|
|
246
|
+
end: {
|
|
247
|
+
line: end.line,
|
|
248
|
+
column: end.column - 1
|
|
249
|
+
}
|
|
249
250
|
};
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
function getJSONSourceLocation(pos, type) {
|
|
253
|
-
let v = getJSONHighlightLocation(pos, type);
|
|
254
|
-
return {
|
|
255
|
-
start: v.start,
|
|
256
|
-
end: {
|
|
257
|
-
line: v.end.line,
|
|
258
|
-
column: v.end.column + 1,
|
|
259
|
-
},
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
function convertSourceLocationToHighlight({start, end}, message) {
|
|
263
|
-
return {
|
|
264
|
-
message,
|
|
265
|
-
start,
|
|
266
|
-
end: {
|
|
267
|
-
line: end.line,
|
|
268
|
-
column: end.column - 1,
|
|
269
|
-
},
|
|
270
|
-
};
|
|
271
251
|
}
|
|
272
252
|
function encodeJSONKeyComponent(component) {
|
|
273
|
-
|
|
253
|
+
return component.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
274
254
|
}
|
|
275
|
-
const escapeCharacters = [
|
|
255
|
+
const escapeCharacters = [
|
|
256
|
+
'\\',
|
|
257
|
+
'*',
|
|
258
|
+
'_',
|
|
259
|
+
'~'
|
|
260
|
+
];
|
|
276
261
|
function escapeMarkdown(s) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
262
|
+
let result = s;
|
|
263
|
+
for (const char of escapeCharacters){
|
|
264
|
+
result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
282
267
|
}
|
|
283
268
|
const mdVerbatim = Symbol();
|
|
284
|
-
const md = function
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
269
|
+
const md = function(strings, ...params) {
|
|
270
|
+
let result = [];
|
|
271
|
+
for(let i = 0; i < params.length; i++){
|
|
272
|
+
result.push(strings[i]);
|
|
273
|
+
let param = params[i];
|
|
274
|
+
if (Array.isArray(param)) {
|
|
275
|
+
for(let j = 0; j < param.length; j++){
|
|
276
|
+
result.push(param[j]?.[mdVerbatim] ?? escapeMarkdown(`${param[j]}`));
|
|
277
|
+
if (j < param.length - 1) {
|
|
278
|
+
result.push(', ');
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
282
|
+
result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
|
|
294
283
|
}
|
|
295
|
-
}
|
|
296
|
-
} else {
|
|
297
|
-
result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
|
|
298
284
|
}
|
|
299
|
-
|
|
300
|
-
return result.join('') + strings[strings.length - 1];
|
|
285
|
+
return result.join('') + strings[strings.length - 1];
|
|
301
286
|
};
|
|
302
|
-
md.bold = function
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
287
|
+
md.bold = function(s) {
|
|
288
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
289
|
+
return {
|
|
290
|
+
[mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**'
|
|
291
|
+
};
|
|
307
292
|
};
|
|
308
|
-
md.italic = function
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
293
|
+
md.italic = function(s) {
|
|
294
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
295
|
+
return {
|
|
296
|
+
[mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_'
|
|
297
|
+
};
|
|
313
298
|
};
|
|
314
|
-
md.underline = function
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
299
|
+
md.underline = function(s) {
|
|
300
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
301
|
+
return {
|
|
302
|
+
[mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__'
|
|
303
|
+
};
|
|
319
304
|
};
|
|
320
|
-
md.strikethrough = function
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
305
|
+
md.strikethrough = function(s) {
|
|
306
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
307
|
+
return {
|
|
308
|
+
[mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~'
|
|
309
|
+
};
|
|
325
310
|
};
|
package/lib/index.mjs
CHANGED
|
@@ -3,94 +3,97 @@ import nullthrows from 'nullthrows';
|
|
|
3
3
|
import * as jsonSourcemap from '@mischnic/json-sourcemap';
|
|
4
4
|
/** Normalize the given value into a diagnostic. */
|
|
5
5
|
export function anyToDiagnostic(input) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
if (Array.isArray(input)) {
|
|
7
|
+
return input.flatMap((e) => anyToDiagnostic(e));
|
|
8
|
+
}
|
|
9
|
+
else if (input instanceof ThrowableDiagnostic) {
|
|
10
|
+
return input.diagnostics;
|
|
11
|
+
}
|
|
12
|
+
else if (input instanceof Error) {
|
|
13
|
+
return errorToDiagnostic(input);
|
|
14
|
+
}
|
|
15
|
+
else if (typeof input === 'string') {
|
|
16
|
+
return [{ message: input }];
|
|
17
|
+
}
|
|
18
|
+
else if (typeof input === 'object') {
|
|
19
|
+
return [input];
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
return errorToDiagnostic(input);
|
|
23
|
+
}
|
|
19
24
|
}
|
|
20
25
|
/** Normalize the given error into a diagnostic. */
|
|
21
26
|
export function errorToDiagnostic(error, defaultValues) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
},
|
|
29
|
-
];
|
|
30
|
-
}
|
|
31
|
-
if (error instanceof ThrowableDiagnostic) {
|
|
32
|
-
return error.diagnostics.map((d) => {
|
|
33
|
-
return {
|
|
34
|
-
...d,
|
|
35
|
-
origin: d.origin ?? defaultValues?.origin ?? 'unknown',
|
|
36
|
-
};
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
if (error.loc && error.source != null) {
|
|
40
|
-
codeFrames = [
|
|
41
|
-
{
|
|
42
|
-
filePath:
|
|
43
|
-
error.filePath ??
|
|
44
|
-
error.fileName ??
|
|
45
|
-
defaultValues?.filePath ??
|
|
46
|
-
undefined,
|
|
47
|
-
code: error.source,
|
|
48
|
-
codeHighlights: [
|
|
49
|
-
{
|
|
50
|
-
start: {
|
|
51
|
-
line: error.loc.line,
|
|
52
|
-
column: error.loc.column,
|
|
27
|
+
let codeFrames = undefined;
|
|
28
|
+
if (typeof error === 'string') {
|
|
29
|
+
return [
|
|
30
|
+
{
|
|
31
|
+
origin: defaultValues?.origin ?? 'Error',
|
|
32
|
+
message: escapeMarkdown(error),
|
|
53
33
|
},
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
if (error instanceof ThrowableDiagnostic) {
|
|
37
|
+
return error.diagnostics.map((d) => {
|
|
38
|
+
return {
|
|
39
|
+
...d,
|
|
40
|
+
origin: d.origin ?? defaultValues?.origin ?? 'unknown',
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (error.loc && error.source != null) {
|
|
45
|
+
codeFrames = [
|
|
46
|
+
{
|
|
47
|
+
filePath: error.filePath ??
|
|
48
|
+
error.fileName ??
|
|
49
|
+
defaultValues?.filePath ??
|
|
50
|
+
undefined,
|
|
51
|
+
code: error.source,
|
|
52
|
+
codeHighlights: [
|
|
53
|
+
{
|
|
54
|
+
start: {
|
|
55
|
+
line: error.loc.line,
|
|
56
|
+
column: error.loc.column,
|
|
57
|
+
},
|
|
58
|
+
end: {
|
|
59
|
+
line: error.loc.line,
|
|
60
|
+
column: error.loc.column,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
57
64
|
},
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
origin: defaultValues?.origin ?? 'Error',
|
|
70
|
+
message: escapeMarkdown(error.message),
|
|
71
|
+
name: error.name,
|
|
72
|
+
stack: codeFrames == null
|
|
73
|
+
? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack
|
|
74
|
+
: undefined,
|
|
75
|
+
codeFrames,
|
|
76
|
+
},
|
|
61
77
|
];
|
|
62
|
-
}
|
|
63
|
-
return [
|
|
64
|
-
{
|
|
65
|
-
origin: defaultValues?.origin ?? 'Error',
|
|
66
|
-
message: escapeMarkdown(error.message),
|
|
67
|
-
name: error.name,
|
|
68
|
-
stack:
|
|
69
|
-
codeFrames == null
|
|
70
|
-
? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack
|
|
71
|
-
: undefined,
|
|
72
|
-
codeFrames,
|
|
73
|
-
},
|
|
74
|
-
];
|
|
75
78
|
}
|
|
76
79
|
/**
|
|
77
80
|
* An error wrapper around a diagnostic that can be <code>throw</code>n (e.g. to signal a
|
|
78
81
|
* build error).
|
|
79
82
|
*/
|
|
80
83
|
export default class ThrowableDiagnostic extends Error {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
84
|
+
diagnostics;
|
|
85
|
+
constructor(opts) {
|
|
86
|
+
let diagnostics = Array.isArray(opts.diagnostic)
|
|
87
|
+
? opts.diagnostic
|
|
88
|
+
: [opts.diagnostic];
|
|
89
|
+
// Construct error from diagnostics
|
|
90
|
+
super(diagnostics[0].message);
|
|
91
|
+
// @ts-ignore
|
|
92
|
+
this.stack = diagnostics[0].stack ?? super.stack;
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
this.name = diagnostics[0].name ?? super.name;
|
|
95
|
+
this.diagnostics = diagnostics;
|
|
96
|
+
}
|
|
94
97
|
}
|
|
95
98
|
/**
|
|
96
99
|
* Turns a list of positions in a JSON5 file with messages into a list of diagnostics.
|
|
@@ -101,99 +104,98 @@ export default class ThrowableDiagnostic extends Error {
|
|
|
101
104
|
* <code>type</code> signifies whether the key of the value in a JSON object should be highlighted.
|
|
102
105
|
*/
|
|
103
106
|
export function generateJSONCodeHighlights(data, ids) {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
...getJSONHighlightLocation(pos, type),
|
|
115
|
-
message,
|
|
116
|
-
};
|
|
117
|
-
});
|
|
107
|
+
let map = typeof data == 'string'
|
|
108
|
+
? jsonSourcemap.default.parse(data, undefined, { dialect: 'JSON5', tabWidth: 1 })
|
|
109
|
+
: data;
|
|
110
|
+
return ids.map(({ key, type, message }) => {
|
|
111
|
+
let pos = nullthrows.default(map.pointers[key]);
|
|
112
|
+
return {
|
|
113
|
+
...getJSONHighlightLocation(pos, type),
|
|
114
|
+
message,
|
|
115
|
+
};
|
|
116
|
+
});
|
|
118
117
|
}
|
|
119
118
|
/**
|
|
120
119
|
* Converts entries in <a href="https://github.com/mischnic/json-sourcemap">@mischnic/json-sourcemap</a>'s
|
|
121
120
|
* <code>result.pointers</code> array.
|
|
122
121
|
*/
|
|
123
122
|
export function getJSONHighlightLocation(pos, type) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
123
|
+
let key = 'key' in pos ? pos.key : undefined;
|
|
124
|
+
let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
|
|
125
|
+
if (!type && key && pos.value) {
|
|
126
|
+
// key and value
|
|
127
|
+
return {
|
|
128
|
+
start: { line: key.line + 1, column: key.column + 1 },
|
|
129
|
+
end: { line: pos.valueEnd.line + 1, column: pos.valueEnd.column },
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
else if (type == 'key' || !pos.value) {
|
|
133
|
+
assert(key && keyEnd);
|
|
134
|
+
return {
|
|
135
|
+
start: { line: key.line + 1, column: key.column + 1 },
|
|
136
|
+
end: { line: keyEnd.line + 1, column: keyEnd.column },
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
return {
|
|
141
|
+
start: { line: pos.value.line + 1, column: pos.value.column + 1 },
|
|
142
|
+
end: { line: pos.valueEnd.line + 1, column: pos.valueEnd.column },
|
|
143
|
+
};
|
|
144
|
+
}
|
|
144
145
|
}
|
|
145
146
|
/** Result is 1-based, but end is exclusive */
|
|
146
147
|
export function getJSONSourceLocation(pos, type) {
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
let v = getJSONHighlightLocation(pos, type);
|
|
149
|
+
return { start: v.start, end: { line: v.end.line, column: v.end.column + 1 } };
|
|
149
150
|
}
|
|
150
|
-
export function convertSourceLocationToHighlight({start, end}, message) {
|
|
151
|
-
|
|
151
|
+
export function convertSourceLocationToHighlight({ start, end }, message) {
|
|
152
|
+
return { message, start, end: { line: end.line, column: end.column - 1 } };
|
|
152
153
|
}
|
|
153
154
|
/** Sanitizes object keys before using them as <code>key</code> in generateJSONCodeHighlights */
|
|
154
155
|
export function encodeJSONKeyComponent(component) {
|
|
155
|
-
|
|
156
|
+
return component.replace(/~/g, '~0').replace(/\//g, '~1');
|
|
156
157
|
}
|
|
157
158
|
const escapeCharacters = ['\\', '*', '_', '~'];
|
|
158
159
|
export function escapeMarkdown(s) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
160
|
+
let result = s;
|
|
161
|
+
for (const char of escapeCharacters) {
|
|
162
|
+
result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
164
165
|
}
|
|
165
166
|
const mdVerbatim = Symbol();
|
|
166
167
|
export const md = function (strings, ...params) {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
168
|
+
let result = [];
|
|
169
|
+
for (let i = 0; i < params.length; i++) {
|
|
170
|
+
result.push(strings[i]);
|
|
171
|
+
let param = params[i];
|
|
172
|
+
if (Array.isArray(param)) {
|
|
173
|
+
for (let j = 0; j < param.length; j++) {
|
|
174
|
+
result.push(param[j]?.[mdVerbatim] ?? escapeMarkdown(`${param[j]}`));
|
|
175
|
+
if (j < param.length - 1) {
|
|
176
|
+
result.push(', ');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
|
|
176
182
|
}
|
|
177
|
-
}
|
|
178
|
-
} else {
|
|
179
|
-
result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
|
|
180
183
|
}
|
|
181
|
-
|
|
182
|
-
return result.join('') + strings[strings.length - 1];
|
|
184
|
+
return result.join('') + strings[strings.length - 1];
|
|
183
185
|
};
|
|
184
186
|
md.bold = function (s) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
188
|
+
return { [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**' };
|
|
187
189
|
};
|
|
188
190
|
md.italic = function (s) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
192
|
+
return { [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_' };
|
|
191
193
|
};
|
|
192
194
|
md.underline = function (s) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
196
|
+
return { [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__' };
|
|
195
197
|
};
|
|
196
198
|
md.strikethrough = function (s) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
+
// $FlowFixMe[invalid-computed-prop]
|
|
200
|
+
return { [mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~' };
|
|
199
201
|
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/diagnostic",
|
|
3
|
-
"version": "2.14.2-typescript-
|
|
3
|
+
"version": "2.14.2-typescript-6de04fbae.0",
|
|
4
4
|
"description": "Types and utilities for printing source-code located errors, warning and information messages.",
|
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
|
6
|
+
"type": "commonjs",
|
|
6
7
|
"publishConfig": {
|
|
7
8
|
"access": "public"
|
|
8
9
|
},
|
|
@@ -11,12 +12,12 @@
|
|
|
11
12
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
12
13
|
},
|
|
13
14
|
"main": "lib/index.js",
|
|
14
|
-
"types": "lib/index.d.
|
|
15
|
+
"types": "lib/index.d.mts",
|
|
15
16
|
"exports": {
|
|
16
17
|
".": {
|
|
17
18
|
"atlaspack::sources": "./src/index.mts",
|
|
18
19
|
"types": [
|
|
19
|
-
"./lib/index.d.
|
|
20
|
+
"./lib/index.d.mts",
|
|
20
21
|
"./src/index.mts"
|
|
21
22
|
],
|
|
22
23
|
"import": "./lib/index.mjs",
|
|
@@ -35,5 +36,5 @@
|
|
|
35
36
|
"@mischnic/json-sourcemap": "^0.1.0",
|
|
36
37
|
"nullthrows": "^1.1.1"
|
|
37
38
|
},
|
|
38
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "6de04fbaeccfba1e7832c737f6318cbd603fc74d"
|
|
39
40
|
}
|