@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.
@@ -38,7 +38,7 @@ export type DiagnosticCodeFrame = {
38
38
  codeHighlights: Array<DiagnosticCodeHighlight>;
39
39
  };
40
40
  /** A JSON object (as in "map") */
41
- export type JSONObject = {
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
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {
3
- value: true,
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
4
  });
5
5
  function _export(target, all) {
6
- for (var name in all)
7
- Object.defineProperty(target, name, {
8
- enumerable: true,
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
- anyToDiagnostic: function () {
14
- return anyToDiagnostic;
15
- },
16
- convertSourceLocationToHighlight: function () {
17
- return convertSourceLocationToHighlight;
18
- },
19
- /**
20
- * An error wrapper around a diagnostic that can be <code>throw</code>n (e.g. to signal a
21
- * build error).
22
- */ default: function () {
23
- return ThrowableDiagnostic;
24
- },
25
- encodeJSONKeyComponent: function () {
26
- return encodeJSONKeyComponent;
27
- },
28
- errorToDiagnostic: function () {
29
- return errorToDiagnostic;
30
- },
31
- escapeMarkdown: function () {
32
- return escapeMarkdown;
33
- },
34
- generateJSONCodeHighlights: function () {
35
- return generateJSONCodeHighlights;
36
- },
37
- getJSONHighlightLocation: function () {
38
- return getJSONHighlightLocation;
39
- },
40
- getJSONSourceLocation: function () {
41
- return getJSONSourceLocation;
42
- },
43
- md: function () {
44
- return md;
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
- require('node:assert'),
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
- return obj && obj.__esModule
58
- ? obj
59
- : {
60
- default: obj,
61
- };
50
+ return obj && obj.__esModule ? obj : {
51
+ default: obj
52
+ };
62
53
  }
63
54
  function _getRequireWildcardCache(nodeInterop) {
64
- if (typeof WeakMap !== 'function') return null;
65
- var cacheBabelInterop = new WeakMap();
66
- var cacheNodeInterop = new WeakMap();
67
- return (_getRequireWildcardCache = function (nodeInterop) {
68
- return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
69
- })(nodeInterop);
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
- if (!nodeInterop && obj && obj.__esModule) {
73
- return obj;
74
- }
75
- if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
76
- return {
77
- default: obj,
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
- var cache = _getRequireWildcardCache(nodeInterop);
81
- if (cache && cache.has(obj)) {
82
- return cache.get(obj);
83
- }
84
- var newObj = {
85
- __proto__: null,
86
- };
87
- var hasPropertyDescriptor =
88
- Object.defineProperty && Object.getOwnPropertyDescriptor;
89
- for (var key in obj) {
90
- if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
91
- var desc = hasPropertyDescriptor
92
- ? Object.getOwnPropertyDescriptor(obj, key)
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
- if (Array.isArray(input)) {
109
- return input.flatMap((e) => anyToDiagnostic(e));
110
- } else if (input instanceof ThrowableDiagnostic) {
111
- return input.diagnostics;
112
- } else if (input instanceof Error) {
113
- return errorToDiagnostic(input);
114
- } else if (typeof input === 'string') {
115
- return [
116
- {
117
- message: input,
118
- },
119
- ];
120
- } else if (typeof input === 'object') {
121
- return [input];
122
- } else {
123
- return errorToDiagnostic(input);
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
- let codeFrames = undefined;
128
- if (typeof error === 'string') {
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
- origin: defaultValues?.origin ?? 'Error',
132
- message: escapeMarkdown(error),
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
- if (error instanceof ThrowableDiagnostic) {
137
- return error.diagnostics.map((d) => {
138
- return {
139
- ...d,
140
- origin: d.origin ?? defaultValues?.origin ?? 'unknown',
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
- if (error.loc && error.source != null) {
145
- codeFrames = [
146
- {
147
- filePath:
148
- error.filePath ??
149
- error.fileName ??
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
- line: error.loc.line,
157
- column: error.loc.column,
199
+ line: key.line + 1,
200
+ column: key.column + 1
158
201
  },
159
202
  end: {
160
- line: error.loc.line,
161
- column: error.loc.column,
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
- return [
169
- {
170
- origin: defaultValues?.origin ?? 'Error',
171
- message: escapeMarkdown(error.message),
172
- name: error.name,
173
- stack:
174
- codeFrames == null
175
- ? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack
176
- : undefined,
177
- codeFrames,
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 generateJSONCodeHighlights(data, ids) {
197
- let map =
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
- ...getJSONHighlightLocation(pos, type),
208
- message,
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 getJSONHighlightLocation(pos, type) {
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
- start: {
242
- line: pos.value.line + 1,
243
- column: pos.value.column + 1,
244
- },
245
- end: {
246
- line: pos.valueEnd.line + 1,
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
- return component.replace(/~/g, '~0').replace(/\//g, '~1');
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
- let result = s;
278
- for (const char of escapeCharacters) {
279
- result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
280
- }
281
- return result;
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 (strings, ...params) {
285
- let result = [];
286
- for (let i = 0; i < params.length; i++) {
287
- result.push(strings[i]);
288
- let param = params[i];
289
- if (Array.isArray(param)) {
290
- for (let j = 0; j < param.length; j++) {
291
- result.push(param[j]?.[mdVerbatim] ?? escapeMarkdown(`${param[j]}`));
292
- if (j < param.length - 1) {
293
- result.push(', ');
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 (s) {
303
- // $FlowFixMe[invalid-computed-prop]
304
- return {
305
- [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**',
306
- };
287
+ md.bold = function(s) {
288
+ // $FlowFixMe[invalid-computed-prop]
289
+ return {
290
+ [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**'
291
+ };
307
292
  };
308
- md.italic = function (s) {
309
- // $FlowFixMe[invalid-computed-prop]
310
- return {
311
- [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_',
312
- };
293
+ md.italic = function(s) {
294
+ // $FlowFixMe[invalid-computed-prop]
295
+ return {
296
+ [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_'
297
+ };
313
298
  };
314
- md.underline = function (s) {
315
- // $FlowFixMe[invalid-computed-prop]
316
- return {
317
- [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__',
318
- };
299
+ md.underline = function(s) {
300
+ // $FlowFixMe[invalid-computed-prop]
301
+ return {
302
+ [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__'
303
+ };
319
304
  };
320
- md.strikethrough = function (s) {
321
- // $FlowFixMe[invalid-computed-prop]
322
- return {
323
- [mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~',
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
- if (Array.isArray(input)) {
7
- return input.flatMap((e) => anyToDiagnostic(e));
8
- } else if (input instanceof ThrowableDiagnostic) {
9
- return input.diagnostics;
10
- } else if (input instanceof Error) {
11
- return errorToDiagnostic(input);
12
- } else if (typeof input === 'string') {
13
- return [{message: input}];
14
- } else if (typeof input === 'object') {
15
- return [input];
16
- } else {
17
- return errorToDiagnostic(input);
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
- let codeFrames = undefined;
23
- if (typeof error === 'string') {
24
- return [
25
- {
26
- origin: defaultValues?.origin ?? 'Error',
27
- message: escapeMarkdown(error),
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
- end: {
55
- line: error.loc.line,
56
- column: error.loc.column,
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
- diagnostics;
82
- constructor(opts) {
83
- let diagnostics = Array.isArray(opts.diagnostic)
84
- ? opts.diagnostic
85
- : [opts.diagnostic];
86
- // Construct error from diagnostics
87
- super(diagnostics[0].message);
88
- // @ts-ignore
89
- this.stack = diagnostics[0].stack ?? super.stack;
90
- // @ts-ignore
91
- this.name = diagnostics[0].name ?? super.name;
92
- this.diagnostics = diagnostics;
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
- let map =
105
- typeof data == 'string'
106
- ? jsonSourcemap.default.parse(data, undefined, {
107
- dialect: 'JSON5',
108
- tabWidth: 1,
109
- })
110
- : data;
111
- return ids.map(({key, type, message}) => {
112
- let pos = nullthrows.default(map.pointers[key]);
113
- return {
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
- let key = 'key' in pos ? pos.key : undefined;
125
- let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
126
- if (!type && key && pos.value) {
127
- // key and value
128
- return {
129
- start: {line: key.line + 1, column: key.column + 1},
130
- end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
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
- } else {
139
- return {
140
- start: {line: pos.value.line + 1, column: pos.value.column + 1},
141
- end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
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
- let v = getJSONHighlightLocation(pos, type);
148
- return {start: v.start, end: {line: v.end.line, column: v.end.column + 1}};
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
- return {message, start, end: {line: end.line, column: end.column - 1}};
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
- return component.replace(/~/g, '~0').replace(/\//g, '~1');
156
+ return component.replace(/~/g, '~0').replace(/\//g, '~1');
156
157
  }
157
158
  const escapeCharacters = ['\\', '*', '_', '~'];
158
159
  export function escapeMarkdown(s) {
159
- let result = s;
160
- for (const char of escapeCharacters) {
161
- result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
162
- }
163
- return result;
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
- let result = [];
168
- for (let i = 0; i < params.length; i++) {
169
- result.push(strings[i]);
170
- let param = params[i];
171
- if (Array.isArray(param)) {
172
- for (let j = 0; j < param.length; j++) {
173
- result.push(param[j]?.[mdVerbatim] ?? escapeMarkdown(`${param[j]}`));
174
- if (j < param.length - 1) {
175
- result.push(', ');
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
- // $FlowFixMe[invalid-computed-prop]
186
- return {[mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**'};
187
+ // $FlowFixMe[invalid-computed-prop]
188
+ return { [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**' };
187
189
  };
188
190
  md.italic = function (s) {
189
- // $FlowFixMe[invalid-computed-prop]
190
- return {[mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_'};
191
+ // $FlowFixMe[invalid-computed-prop]
192
+ return { [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_' };
191
193
  };
192
194
  md.underline = function (s) {
193
- // $FlowFixMe[invalid-computed-prop]
194
- return {[mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__'};
195
+ // $FlowFixMe[invalid-computed-prop]
196
+ return { [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__' };
195
197
  };
196
198
  md.strikethrough = function (s) {
197
- // $FlowFixMe[invalid-computed-prop]
198
- return {[mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~'};
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-d6e6d169c.0",
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.ts",
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.ts",
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": "d6e6d169cb16a0bbc98d7743646a7097b82c7fe2"
39
+ "gitHead": "6de04fbaeccfba1e7832c737f6318cbd603fc74d"
39
40
  }
package/src/index.mts CHANGED
@@ -45,7 +45,7 @@ export type DiagnosticCodeFrame = {
45
45
  };
46
46
 
47
47
  /** A JSON object (as in "map") */
48
- export type JSONObject = {
48
+ type JSONObject = {
49
49
  // $FlowFixMe
50
50
  [key: string]: any;
51
51
  };