@parcel/diagnostic 2.12.0 → 2.13.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -37,6 +37,8 @@ export type DiagnosticCodeFrame = {
37
37
  language?: string;
38
38
  codeHighlights: Array<DiagnosticCodeHighlight>;
39
39
  };
40
+ /** A JSON object (as in "map") */
41
+ type JSONObject = Record<string, any>;
40
42
  /**
41
43
  * A style agnostic way of emitting errors, warnings and info.
42
44
  * Reporters are responsible for rendering the message, codeframes, hints, ...
@@ -58,6 +60,8 @@ export type Diagnostic = {
58
60
  skipFormatting?: boolean;
59
61
  /** A URL to documentation to learn more about the diagnostic. */
60
62
  documentationURL?: string;
63
+ /** Diagnostic specific metadata (optional) */
64
+ meta?: JSONObject;
61
65
  };
62
66
  export interface PrintableError extends Error {
63
67
  fileName?: string;
package/lib/diagnostic.js CHANGED
@@ -34,7 +34,7 @@ function _jsonSourcemap() {
34
34
  };
35
35
  return data;
36
36
  }
37
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
38
38
  /** These positions are 1-based (so <code>1</code> is the first line/column) */
39
39
  /**
40
40
  * Note: A tab character is always counted as a single character
@@ -45,6 +45,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
45
45
  * A code frame is a visualization of a piece of code with a certain amount of
46
46
  * code highlights that point to certain chunk(s) inside the code.
47
47
  */
48
+ /** A JSON object (as in "map") */
48
49
  /**
49
50
  * A style agnostic way of emitting errors, warnings and info.
50
51
  * Reporters are responsible for rendering the message, codeframes, hints, ...
@@ -54,7 +55,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
54
55
  /** Normalize the given value into a diagnostic. */
55
56
  function anyToDiagnostic(input) {
56
57
  if (Array.isArray(input)) {
57
- return input;
58
+ return input.flatMap(e => anyToDiagnostic(e));
58
59
  } else if (input instanceof ThrowableDiagnostic) {
59
60
  return input.diagnostics;
60
61
  } else if (input instanceof Error) {
@@ -72,28 +73,24 @@ function anyToDiagnostic(input) {
72
73
 
73
74
  /** Normalize the given error into a diagnostic. */
74
75
  function errorToDiagnostic(error, defaultValues) {
75
- var _defaultValues$origin2, _ref4, _error$highlightedCod;
76
76
  let codeFrames = undefined;
77
77
  if (typeof error === 'string') {
78
- var _defaultValues$origin;
79
78
  return [{
80
- origin: (_defaultValues$origin = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) !== null && _defaultValues$origin !== void 0 ? _defaultValues$origin : 'Error',
79
+ origin: (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'Error',
81
80
  message: escapeMarkdown(error)
82
81
  }];
83
82
  }
84
83
  if (error instanceof ThrowableDiagnostic) {
85
84
  return error.diagnostics.map(d => {
86
- var _ref, _d$origin;
87
85
  return {
88
86
  ...d,
89
- origin: (_ref = (_d$origin = d.origin) !== null && _d$origin !== void 0 ? _d$origin : defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) !== null && _ref !== void 0 ? _ref : 'unknown'
87
+ origin: d.origin ?? (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'unknown'
90
88
  };
91
89
  });
92
90
  }
93
91
  if (error.loc && error.source != null) {
94
- var _ref2, _ref3, _error$filePath;
95
92
  codeFrames = [{
96
- filePath: (_ref2 = (_ref3 = (_error$filePath = error.filePath) !== null && _error$filePath !== void 0 ? _error$filePath : error.fileName) !== null && _ref3 !== void 0 ? _ref3 : defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.filePath) !== null && _ref2 !== void 0 ? _ref2 : undefined,
93
+ filePath: error.filePath ?? error.fileName ?? (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.filePath) ?? undefined,
97
94
  code: error.source,
98
95
  codeHighlights: [{
99
96
  start: {
@@ -108,10 +105,10 @@ function errorToDiagnostic(error, defaultValues) {
108
105
  }];
109
106
  }
110
107
  return [{
111
- origin: (_defaultValues$origin2 = defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) !== null && _defaultValues$origin2 !== void 0 ? _defaultValues$origin2 : 'Error',
108
+ origin: (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'Error',
112
109
  message: escapeMarkdown(error.message),
113
110
  name: error.name,
114
- stack: codeFrames == null ? (_ref4 = (_error$highlightedCod = error.highlightedCodeFrame) !== null && _error$highlightedCod !== void 0 ? _error$highlightedCod : error.codeFrame) !== null && _ref4 !== void 0 ? _ref4 : error.stack : undefined,
111
+ stack: codeFrames == null ? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack : undefined,
115
112
  codeFrames
116
113
  }];
117
114
  }
@@ -121,15 +118,14 @@ function errorToDiagnostic(error, defaultValues) {
121
118
  */
122
119
  class ThrowableDiagnostic extends Error {
123
120
  constructor(opts) {
124
- var _diagnostics$0$stack, _diagnostics$0$name;
125
121
  let diagnostics = Array.isArray(opts.diagnostic) ? opts.diagnostic : [opts.diagnostic];
126
122
 
127
123
  // Construct error from diagnostics
128
124
  super(diagnostics[0].message);
129
125
  // @ts-ignore
130
- this.stack = (_diagnostics$0$stack = diagnostics[0].stack) !== null && _diagnostics$0$stack !== void 0 ? _diagnostics$0$stack : super.stack;
126
+ this.stack = diagnostics[0].stack ?? super.stack;
131
127
  // @ts-ignore
132
- this.name = (_diagnostics$0$name = diagnostics[0].name) !== null && _diagnostics$0$name !== void 0 ? _diagnostics$0$name : super.name;
128
+ this.name = diagnostics[0].name ?? super.name;
133
129
  this.diagnostics = diagnostics;
134
130
  }
135
131
  }
@@ -251,15 +247,14 @@ function md(strings, ...params) {
251
247
  let param = params[i];
252
248
  if (Array.isArray(param)) {
253
249
  for (let j = 0; j < param.length; j++) {
254
- var _param$j$mdVerbatim, _param$j;
255
- result.push((_param$j$mdVerbatim = (_param$j = param[j]) === null || _param$j === void 0 ? void 0 : _param$j[mdVerbatim]) !== null && _param$j$mdVerbatim !== void 0 ? _param$j$mdVerbatim : escapeMarkdown(`${param[j]}`));
250
+ var _param$j;
251
+ result.push(((_param$j = param[j]) === null || _param$j === void 0 ? void 0 : _param$j[mdVerbatim]) ?? escapeMarkdown(`${param[j]}`));
256
252
  if (j < param.length - 1) {
257
253
  result.push(', ');
258
254
  }
259
255
  }
260
256
  } else {
261
- var _param$mdVerbatim;
262
- result.push((_param$mdVerbatim = param === null || param === void 0 ? void 0 : param[mdVerbatim]) !== null && _param$mdVerbatim !== void 0 ? _param$mdVerbatim : escapeMarkdown(`${param}`));
257
+ result.push((param === null || param === void 0 ? void 0 : param[mdVerbatim]) ?? escapeMarkdown(`${param}`));
263
258
  }
264
259
  }
265
260
  return result.join('') + strings[strings.length - 1];
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@parcel/diagnostic",
3
- "version": "2.12.0",
3
+ "version": "2.13.1",
4
+ "description": "Types and utilities for printing source-code located errors, warning and information messages.",
4
5
  "license": "MIT",
5
6
  "publishConfig": {
6
7
  "access": "public"
@@ -17,7 +18,7 @@
17
18
  "source": "src/diagnostic.js",
18
19
  "types": "lib/diagnostic.d.ts",
19
20
  "engines": {
20
- "node": ">= 12.0.0"
21
+ "node": ">= 16.0.0"
21
22
  },
22
23
  "scripts": {
23
24
  "build-ts": "flow-to-ts src/*.js --write && tsc --emitDeclarationOnly --declaration --esModuleInterop src/*.ts && mkdir -p lib && mv src/*.d.ts lib/. && rm src/*.ts",
@@ -27,5 +28,5 @@
27
28
  "@mischnic/json-sourcemap": "^0.1.0",
28
29
  "nullthrows": "^1.1.1"
29
30
  },
30
- "gitHead": "2059029ee91e5f03a273b0954d3e629d7375f986"
31
+ "gitHead": "4a297f79db3eec74437f4c5133127c98bf303703"
31
32
  }
package/src/diagnostic.js CHANGED
@@ -46,6 +46,12 @@ export type DiagnosticCodeFrame = {|
46
46
  codeHighlights: Array<DiagnosticCodeHighlight>,
47
47
  |};
48
48
 
49
+ /** A JSON object (as in "map") */
50
+ type JSONObject = {
51
+ // $FlowFixMe
52
+ [key: string]: any,
53
+ };
54
+
49
55
  /**
50
56
  * A style agnostic way of emitting errors, warnings and info.
51
57
  * Reporters are responsible for rendering the message, codeframes, hints, ...
@@ -72,6 +78,9 @@ export type Diagnostic = {|
72
78
 
73
79
  /** A URL to documentation to learn more about the diagnostic. */
74
80
  documentationURL?: string,
81
+
82
+ /** Diagnostic specific metadata (optional) */
83
+ meta?: JSONObject,
75
84
  |};
76
85
 
77
86
  // This type should represent all error formats Parcel can encounter...
@@ -105,7 +114,7 @@ export type Diagnostifiable =
105
114
  /** Normalize the given value into a diagnostic. */
106
115
  export function anyToDiagnostic(input: Diagnostifiable): Array<Diagnostic> {
107
116
  if (Array.isArray(input)) {
108
- return input;
117
+ return input.flatMap(e => anyToDiagnostic(e));
109
118
  } else if (input instanceof ThrowableDiagnostic) {
110
119
  return input.diagnostics;
111
120
  } else if (input instanceof Error) {