@atlaspack/diagnostic 2.14.2-typescript-d6e6d169c.0 → 2.14.2-typescript-e769947a5.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.
@@ -1,12 +1,14 @@
1
- import assert from 'node:assert';
1
+ // @flow strict-local
2
+
3
+ import invariant from 'assert';
2
4
  import nullthrows from 'nullthrows';
3
- import * as jsonSourcemap from '@mischnic/json-sourcemap';
5
+ import {parse, type Mapping} from '@mischnic/json-sourcemap';
4
6
 
5
7
  /** These positions are 1-based (so <code>1</code> is the first line/column) */
6
- export type DiagnosticHighlightLocation = {
7
- line: number;
8
- column: number;
9
- };
8
+ export type DiagnosticHighlightLocation = {|
9
+ +line: number,
10
+ +column: number,
11
+ |};
10
12
 
11
13
  export type DiagnosticSeverity = 'error' | 'warn' | 'info';
12
14
 
@@ -14,21 +16,21 @@ export type DiagnosticSeverity = 'error' | 'warn' | 'info';
14
16
  * Note: A tab character is always counted as a single character
15
17
  * This is to prevent any mismatch of highlighting across machines
16
18
  */
17
- export type DiagnosticCodeHighlight = {
19
+ export type DiagnosticCodeHighlight = {|
18
20
  /** Location of the first character that should get highlighted for this highlight. */
19
- start: DiagnosticHighlightLocation;
21
+ start: DiagnosticHighlightLocation,
20
22
  /** Location of the last character that should get highlighted for this highlight. */
21
- end: DiagnosticHighlightLocation;
23
+ end: DiagnosticHighlightLocation,
22
24
  /** A message that should be displayed at this location in the code (optional). */
23
- message?: string;
24
- };
25
+ message?: string,
26
+ |};
25
27
 
26
28
  /**
27
29
  * Describes how to format a code frame.
28
30
  * A code frame is a visualization of a piece of code with a certain amount of
29
31
  * code highlights that point to certain chunk(s) inside the code.
30
32
  */
31
- export type DiagnosticCodeFrame = {
33
+ export type DiagnosticCodeFrame = {|
32
34
  /**
33
35
  * The contents of the source file.
34
36
  *
@@ -36,50 +38,50 @@ export type DiagnosticCodeFrame = {
36
38
  * the asset's current code could be different from the input contents.
37
39
  *
38
40
  */
39
- code?: string;
41
+ code?: string,
40
42
  /** Path to the file this code frame is about (optional, absolute or relative to the project root) */
41
- filePath?: string;
43
+ filePath?: string,
42
44
  /** Language of the file this code frame is about (optional) */
43
- language?: string;
44
- codeHighlights: Array<DiagnosticCodeHighlight>;
45
- };
45
+ language?: string,
46
+ codeHighlights: Array<DiagnosticCodeHighlight>,
47
+ |};
46
48
 
47
49
  /** A JSON object (as in "map") */
48
- export type JSONObject = {
50
+ type JSONObject = {
49
51
  // $FlowFixMe
50
- [key: string]: any;
52
+ [key: string]: any,
51
53
  };
52
54
 
53
55
  /**
54
56
  * A style agnostic way of emitting errors, warnings and info.
55
57
  * Reporters are responsible for rendering the message, codeframes, hints, ...
56
58
  */
57
- export type Diagnostic = {
59
+ export type Diagnostic = {|
58
60
  /** This is the message you want to log. */
59
- message: string;
61
+ message: string,
60
62
  /** Name of plugin or file that threw this error */
61
- origin?: string;
63
+ origin?: string,
62
64
 
63
65
  /** A stacktrace of the error (optional) */
64
- stack?: string;
66
+ stack?: string,
65
67
  /** Name of the error (optional) */
66
- name?: string;
68
+ name?: string,
67
69
 
68
70
  /** A code frame points to a certain location(s) in the file this diagnostic is linked to (optional) */
69
- codeFrames?: Array<DiagnosticCodeFrame>;
71
+ codeFrames?: ?Array<DiagnosticCodeFrame>,
70
72
 
71
73
  /** An optional list of strings that suggest ways to resolve this issue */
72
- hints?: Array<string>;
74
+ hints?: Array<string>,
73
75
 
74
76
  /** @private */
75
- skipFormatting?: boolean;
77
+ skipFormatting?: boolean,
76
78
 
77
79
  /** A URL to documentation to learn more about the diagnostic. */
78
- documentationURL?: string;
80
+ documentationURL?: string,
79
81
 
80
82
  /** Diagnostic specific metadata (optional) */
81
- meta?: JSONObject;
82
- };
83
+ meta?: JSONObject,
84
+ |};
83
85
 
84
86
  // This type should represent all error formats Atlaspack can encounter...
85
87
  export interface PrintableError extends Error {
@@ -87,16 +89,18 @@ export interface PrintableError extends Error {
87
89
  filePath?: string;
88
90
  codeFrame?: string;
89
91
  highlightedCodeFrame?: string;
90
- loc?: {
91
- column: number;
92
- line: number;
92
+ loc?: ?{
93
+ column: number,
94
+ line: number,
95
+ ...
93
96
  };
94
97
  source?: string;
95
98
  }
96
99
 
97
- export type DiagnosticWithoutOrigin = Diagnostic & {
98
- origin?: string;
99
- };
100
+ export type DiagnosticWithoutOrigin = {|
101
+ ...Diagnostic,
102
+ origin?: string,
103
+ |};
100
104
 
101
105
  /** Something that can be turned into a diagnostic. */
102
106
  export type Diagnostifiable =
@@ -127,12 +131,12 @@ export function anyToDiagnostic(input: Diagnostifiable): Array<Diagnostic> {
127
131
  /** Normalize the given error into a diagnostic. */
128
132
  export function errorToDiagnostic(
129
133
  error: ThrowableDiagnostic | PrintableError | string,
130
- defaultValues?: {
131
- origin?: string;
132
- filePath?: string;
133
- },
134
+ defaultValues?: {|
135
+ origin?: ?string,
136
+ filePath?: ?string,
137
+ |},
134
138
  ): Array<Diagnostic> {
135
- let codeFrames: Array<DiagnosticCodeFrame> | undefined = undefined;
139
+ let codeFrames: ?Array<DiagnosticCodeFrame> = undefined;
136
140
 
137
141
  if (typeof error === 'string') {
138
142
  return [
@@ -191,8 +195,9 @@ export function errorToDiagnostic(
191
195
  ];
192
196
  }
193
197
 
194
- export type ThrowableDiagnosticOpts = {
195
- diagnostic: Diagnostic | Array<Diagnostic>;
198
+ type ThrowableDiagnosticOpts = {
199
+ diagnostic: Diagnostic | Array<Diagnostic>,
200
+ ...
196
201
  };
197
202
 
198
203
  /**
@@ -229,18 +234,18 @@ export default class ThrowableDiagnostic extends Error {
229
234
  export function generateJSONCodeHighlights(
230
235
  data:
231
236
  | string
232
- | {
233
- data: any;
234
- pointers: {[key: string]: jsonSourcemap.Mapping};
235
- },
236
- ids: Array<{key: string; type?: 'key' | 'value'; message?: string}>,
237
+ | {|
238
+ data: mixed,
239
+ pointers: {|[key: string]: Mapping|},
240
+ |},
241
+ ids: Array<{|key: string, type?: ?'key' | 'value', message?: string|}>,
237
242
  ): Array<DiagnosticCodeHighlight> {
238
243
  let map =
239
244
  typeof data == 'string'
240
- ? jsonSourcemap.default.parse(data, undefined, {dialect: 'JSON5', tabWidth: 1})
245
+ ? parse(data, undefined, {dialect: 'JSON5', tabWidth: 1})
241
246
  : data;
242
247
  return ids.map(({key, type, message}) => {
243
- let pos = nullthrows.default(map.pointers[key]);
248
+ let pos = nullthrows(map.pointers[key]);
244
249
  return {
245
250
  ...getJSONHighlightLocation(pos, type),
246
251
  message,
@@ -253,12 +258,12 @@ export function generateJSONCodeHighlights(
253
258
  * <code>result.pointers</code> array.
254
259
  */
255
260
  export function getJSONHighlightLocation(
256
- pos: jsonSourcemap.Mapping,
257
- type?: 'key' | 'value',
258
- ): {
259
- start: DiagnosticHighlightLocation;
260
- end: DiagnosticHighlightLocation;
261
- } {
261
+ pos: Mapping,
262
+ type?: ?'key' | 'value',
263
+ ): {|
264
+ start: DiagnosticHighlightLocation,
265
+ end: DiagnosticHighlightLocation,
266
+ |} {
262
267
  let key = 'key' in pos ? pos.key : undefined;
263
268
  let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
264
269
  if (!type && key && pos.value) {
@@ -268,7 +273,7 @@ export function getJSONHighlightLocation(
268
273
  end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
269
274
  };
270
275
  } else if (type == 'key' || !pos.value) {
271
- assert(key && keyEnd);
276
+ invariant(key && keyEnd);
272
277
  return {
273
278
  start: {line: key.line + 1, column: key.column + 1},
274
279
  end: {line: keyEnd.line + 1, column: keyEnd.column},
@@ -283,34 +288,35 @@ export function getJSONHighlightLocation(
283
288
 
284
289
  /** Result is 1-based, but end is exclusive */
285
290
  export function getJSONSourceLocation(
286
- pos: jsonSourcemap.Mapping,
287
- type?: 'key' | 'value',
288
- ): {
289
- start: {
290
- line: number;
291
- column: number;
292
- };
293
- end: {
294
- line: number;
295
- column: number;
296
- };
297
- } {
291
+ pos: Mapping,
292
+ type?: ?'key' | 'value',
293
+ ): {|
294
+ start: {|
295
+ +line: number,
296
+ +column: number,
297
+ |},
298
+ end: {|
299
+ +line: number,
300
+ +column: number,
301
+ |},
302
+ |} {
298
303
  let v = getJSONHighlightLocation(pos, type);
299
304
  return {start: v.start, end: {line: v.end.line, column: v.end.column + 1}};
300
305
  }
301
306
 
302
307
  export function convertSourceLocationToHighlight<
303
- Location extends {
308
+ Location: {
304
309
  /** 1-based, inclusive */
305
- start: {
306
- line: number;
307
- column: number;
308
- };
310
+ +start: {|
311
+ +line: number,
312
+ +column: number,
313
+ |},
309
314
  /** 1-based, exclusive */
310
- end: {
311
- line: number;
312
- column: number;
313
- };
315
+ +end: {|
316
+ +line: number,
317
+ +column: number,
318
+ |},
319
+ ...
314
320
  },
315
321
  >({start, end}: Location, message?: string): DiagnosticCodeHighlight {
316
322
  return {message, start, end: {line: end.line, column: end.column - 1}};
@@ -332,20 +338,11 @@ export function escapeMarkdown(s: string): string {
332
338
  return result;
333
339
  }
334
340
 
335
- export type TemplateInput = any;
341
+ type TemplateInput = $FlowFixMe;
336
342
 
337
343
  const mdVerbatim = Symbol();
338
-
339
- export interface MdFunction {
340
- (strings: TemplateStringsArray, ...params: Array<TemplateInput>): string;
341
- bold: (s: TemplateInput) => TemplateInput
342
- italic: (s: TemplateInput) => TemplateInput
343
- underline: (s: TemplateInput) => TemplateInput
344
- strikethrough: (s: TemplateInput) => TemplateInput
345
- }
346
-
347
- export const md: MdFunction = function (
348
- strings: TemplateStringsArray,
344
+ export function md(
345
+ strings: Array<string>,
349
346
  ...params: Array<TemplateInput>
350
347
  ): string {
351
348
  let result = [];
@@ -365,7 +362,7 @@ export const md: MdFunction = function (
365
362
  }
366
363
  }
367
364
  return result.join('') + strings[strings.length - 1];
368
- };
365
+ }
369
366
 
370
367
  md.bold = function (s: TemplateInput): TemplateInput {
371
368
  // $FlowFixMe[invalid-computed-prop]
@@ -1,6 +1,7 @@
1
+ // @flow strict-local
1
2
  import assert from 'assert';
2
3
 
3
- import {generateJSONCodeHighlights} from '../src/index.mts';
4
+ import {generateJSONCodeHighlights} from '../src/diagnostic';
4
5
 
5
6
  describe('generateJSONCodeHighlights', () => {
6
7
  it('returns an escaped string 01', () => {
@@ -1,6 +1,7 @@
1
+ // @flow
1
2
  import assert from 'assert';
2
3
 
3
- import {escapeMarkdown, md} from '../src/index.mts';
4
+ import {escapeMarkdown, md} from '../src/diagnostic';
4
5
 
5
6
  describe('escapeMarkdown', () => {
6
7
  it('returns an escaped string 01', () => {
package/lib/index.js DELETED
@@ -1,325 +0,0 @@
1
- 'use strict';
2
- Object.defineProperty(exports, '__esModule', {
3
- value: true,
4
- });
5
- function _export(target, all) {
6
- for (var name in all)
7
- Object.defineProperty(target, name, {
8
- enumerable: true,
9
- get: all[name],
10
- });
11
- }
12
- _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
- },
46
- });
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
- );
56
- function _interop_require_default(obj) {
57
- return obj && obj.__esModule
58
- ? obj
59
- : {
60
- default: obj,
61
- };
62
- }
63
- 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);
70
- }
71
- 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,
78
- };
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
- }
99
- }
100
- }
101
- newObj.default = obj;
102
- if (cache) {
103
- cache.set(obj, newObj);
104
- }
105
- return newObj;
106
- }
107
- 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
- }
125
- }
126
- function errorToDiagnostic(error, defaultValues) {
127
- let codeFrames = undefined;
128
- if (typeof error === 'string') {
129
- return [
130
- {
131
- origin: defaultValues?.origin ?? 'Error',
132
- message: escapeMarkdown(error),
133
- },
134
- ];
135
- }
136
- if (error instanceof ThrowableDiagnostic) {
137
- return error.diagnostics.map((d) => {
138
- return {
139
- ...d,
140
- origin: d.origin ?? defaultValues?.origin ?? 'unknown',
141
- };
142
- });
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
- {
155
- start: {
156
- line: error.loc.line,
157
- column: error.loc.column,
158
- },
159
- end: {
160
- line: error.loc.line,
161
- column: error.loc.column,
162
- },
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
- }
195
- }
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]);
206
- return {
207
- ...getJSONHighlightLocation(pos, type),
208
- message,
209
- };
210
- });
211
- }
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 {
240
- 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
- },
249
- };
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
- }
272
- function encodeJSONKeyComponent(component) {
273
- return component.replace(/~/g, '~0').replace(/\//g, '~1');
274
- }
275
- const escapeCharacters = ['\\', '*', '_', '~'];
276
- 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;
282
- }
283
- 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(', ');
294
- }
295
- }
296
- } else {
297
- result.push(param?.[mdVerbatim] ?? escapeMarkdown(`${param}`));
298
- }
299
- }
300
- return result.join('') + strings[strings.length - 1];
301
- };
302
- md.bold = function (s) {
303
- // $FlowFixMe[invalid-computed-prop]
304
- return {
305
- [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**',
306
- };
307
- };
308
- md.italic = function (s) {
309
- // $FlowFixMe[invalid-computed-prop]
310
- return {
311
- [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_',
312
- };
313
- };
314
- md.underline = function (s) {
315
- // $FlowFixMe[invalid-computed-prop]
316
- return {
317
- [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__',
318
- };
319
- };
320
- md.strikethrough = function (s) {
321
- // $FlowFixMe[invalid-computed-prop]
322
- return {
323
- [mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~',
324
- };
325
- };