@atlaspack/diagnostic 2.14.2-typescript-8a6ec6c8b.0 → 2.14.2-typescript-d6e6d169c.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,14 +1,12 @@
1
- // @flow strict-local
2
-
3
- import invariant from 'assert';
1
+ import assert from 'node:assert';
4
2
  import nullthrows from 'nullthrows';
5
- import {parse, type Mapping} from '@mischnic/json-sourcemap';
3
+ import * as jsonSourcemap from '@mischnic/json-sourcemap';
6
4
 
7
5
  /** These positions are 1-based (so <code>1</code> is the first line/column) */
8
- export type DiagnosticHighlightLocation = {|
9
- +line: number,
10
- +column: number,
11
- |};
6
+ export type DiagnosticHighlightLocation = {
7
+ line: number;
8
+ column: number;
9
+ };
12
10
 
13
11
  export type DiagnosticSeverity = 'error' | 'warn' | 'info';
14
12
 
@@ -16,21 +14,21 @@ export type DiagnosticSeverity = 'error' | 'warn' | 'info';
16
14
  * Note: A tab character is always counted as a single character
17
15
  * This is to prevent any mismatch of highlighting across machines
18
16
  */
19
- export type DiagnosticCodeHighlight = {|
17
+ export type DiagnosticCodeHighlight = {
20
18
  /** Location of the first character that should get highlighted for this highlight. */
21
- start: DiagnosticHighlightLocation,
19
+ start: DiagnosticHighlightLocation;
22
20
  /** Location of the last character that should get highlighted for this highlight. */
23
- end: DiagnosticHighlightLocation,
21
+ end: DiagnosticHighlightLocation;
24
22
  /** A message that should be displayed at this location in the code (optional). */
25
- message?: string,
26
- |};
23
+ message?: string;
24
+ };
27
25
 
28
26
  /**
29
27
  * Describes how to format a code frame.
30
28
  * A code frame is a visualization of a piece of code with a certain amount of
31
29
  * code highlights that point to certain chunk(s) inside the code.
32
30
  */
33
- export type DiagnosticCodeFrame = {|
31
+ export type DiagnosticCodeFrame = {
34
32
  /**
35
33
  * The contents of the source file.
36
34
  *
@@ -38,50 +36,50 @@ export type DiagnosticCodeFrame = {|
38
36
  * the asset's current code could be different from the input contents.
39
37
  *
40
38
  */
41
- code?: string,
39
+ code?: string;
42
40
  /** Path to the file this code frame is about (optional, absolute or relative to the project root) */
43
- filePath?: string,
41
+ filePath?: string;
44
42
  /** Language of the file this code frame is about (optional) */
45
- language?: string,
46
- codeHighlights: Array<DiagnosticCodeHighlight>,
47
- |};
43
+ language?: string;
44
+ codeHighlights: Array<DiagnosticCodeHighlight>;
45
+ };
48
46
 
49
47
  /** A JSON object (as in "map") */
50
- type JSONObject = {
48
+ export type JSONObject = {
51
49
  // $FlowFixMe
52
- [key: string]: any,
50
+ [key: string]: any;
53
51
  };
54
52
 
55
53
  /**
56
54
  * A style agnostic way of emitting errors, warnings and info.
57
55
  * Reporters are responsible for rendering the message, codeframes, hints, ...
58
56
  */
59
- export type Diagnostic = {|
57
+ export type Diagnostic = {
60
58
  /** This is the message you want to log. */
61
- message: string,
59
+ message: string;
62
60
  /** Name of plugin or file that threw this error */
63
- origin?: string,
61
+ origin?: string;
64
62
 
65
63
  /** A stacktrace of the error (optional) */
66
- stack?: string,
64
+ stack?: string;
67
65
  /** Name of the error (optional) */
68
- name?: string,
66
+ name?: string;
69
67
 
70
68
  /** A code frame points to a certain location(s) in the file this diagnostic is linked to (optional) */
71
- codeFrames?: ?Array<DiagnosticCodeFrame>,
69
+ codeFrames?: Array<DiagnosticCodeFrame>;
72
70
 
73
71
  /** An optional list of strings that suggest ways to resolve this issue */
74
- hints?: Array<string>,
72
+ hints?: Array<string>;
75
73
 
76
74
  /** @private */
77
- skipFormatting?: boolean,
75
+ skipFormatting?: boolean;
78
76
 
79
77
  /** A URL to documentation to learn more about the diagnostic. */
80
- documentationURL?: string,
78
+ documentationURL?: string;
81
79
 
82
80
  /** Diagnostic specific metadata (optional) */
83
- meta?: JSONObject,
84
- |};
81
+ meta?: JSONObject;
82
+ };
85
83
 
86
84
  // This type should represent all error formats Atlaspack can encounter...
87
85
  export interface PrintableError extends Error {
@@ -89,18 +87,16 @@ export interface PrintableError extends Error {
89
87
  filePath?: string;
90
88
  codeFrame?: string;
91
89
  highlightedCodeFrame?: string;
92
- loc?: ?{
93
- column: number,
94
- line: number,
95
- ...
90
+ loc?: {
91
+ column: number;
92
+ line: number;
96
93
  };
97
94
  source?: string;
98
95
  }
99
96
 
100
- export type DiagnosticWithoutOrigin = {|
101
- ...Diagnostic,
102
- origin?: string,
103
- |};
97
+ export type DiagnosticWithoutOrigin = Diagnostic & {
98
+ origin?: string;
99
+ };
104
100
 
105
101
  /** Something that can be turned into a diagnostic. */
106
102
  export type Diagnostifiable =
@@ -131,12 +127,12 @@ export function anyToDiagnostic(input: Diagnostifiable): Array<Diagnostic> {
131
127
  /** Normalize the given error into a diagnostic. */
132
128
  export function errorToDiagnostic(
133
129
  error: ThrowableDiagnostic | PrintableError | string,
134
- defaultValues?: {|
135
- origin?: ?string,
136
- filePath?: ?string,
137
- |},
130
+ defaultValues?: {
131
+ origin?: string;
132
+ filePath?: string;
133
+ },
138
134
  ): Array<Diagnostic> {
139
- let codeFrames: ?Array<DiagnosticCodeFrame> = undefined;
135
+ let codeFrames: Array<DiagnosticCodeFrame> | undefined = undefined;
140
136
 
141
137
  if (typeof error === 'string') {
142
138
  return [
@@ -195,9 +191,8 @@ export function errorToDiagnostic(
195
191
  ];
196
192
  }
197
193
 
198
- type ThrowableDiagnosticOpts = {
199
- diagnostic: Diagnostic | Array<Diagnostic>,
200
- ...
194
+ export type ThrowableDiagnosticOpts = {
195
+ diagnostic: Diagnostic | Array<Diagnostic>;
201
196
  };
202
197
 
203
198
  /**
@@ -234,18 +229,18 @@ export default class ThrowableDiagnostic extends Error {
234
229
  export function generateJSONCodeHighlights(
235
230
  data:
236
231
  | string
237
- | {|
238
- data: mixed,
239
- pointers: {|[key: string]: Mapping|},
240
- |},
241
- ids: Array<{|key: string, type?: ?'key' | 'value', message?: string|}>,
232
+ | {
233
+ data: any;
234
+ pointers: {[key: string]: jsonSourcemap.Mapping};
235
+ },
236
+ ids: Array<{key: string; type?: 'key' | 'value'; message?: string}>,
242
237
  ): Array<DiagnosticCodeHighlight> {
243
238
  let map =
244
239
  typeof data == 'string'
245
- ? parse(data, undefined, {dialect: 'JSON5', tabWidth: 1})
240
+ ? jsonSourcemap.default.parse(data, undefined, {dialect: 'JSON5', tabWidth: 1})
246
241
  : data;
247
242
  return ids.map(({key, type, message}) => {
248
- let pos = nullthrows(map.pointers[key]);
243
+ let pos = nullthrows.default(map.pointers[key]);
249
244
  return {
250
245
  ...getJSONHighlightLocation(pos, type),
251
246
  message,
@@ -258,12 +253,12 @@ export function generateJSONCodeHighlights(
258
253
  * <code>result.pointers</code> array.
259
254
  */
260
255
  export function getJSONHighlightLocation(
261
- pos: Mapping,
262
- type?: ?'key' | 'value',
263
- ): {|
264
- start: DiagnosticHighlightLocation,
265
- end: DiagnosticHighlightLocation,
266
- |} {
256
+ pos: jsonSourcemap.Mapping,
257
+ type?: 'key' | 'value',
258
+ ): {
259
+ start: DiagnosticHighlightLocation;
260
+ end: DiagnosticHighlightLocation;
261
+ } {
267
262
  let key = 'key' in pos ? pos.key : undefined;
268
263
  let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
269
264
  if (!type && key && pos.value) {
@@ -273,7 +268,7 @@ export function getJSONHighlightLocation(
273
268
  end: {line: pos.valueEnd.line + 1, column: pos.valueEnd.column},
274
269
  };
275
270
  } else if (type == 'key' || !pos.value) {
276
- invariant(key && keyEnd);
271
+ assert(key && keyEnd);
277
272
  return {
278
273
  start: {line: key.line + 1, column: key.column + 1},
279
274
  end: {line: keyEnd.line + 1, column: keyEnd.column},
@@ -288,35 +283,34 @@ export function getJSONHighlightLocation(
288
283
 
289
284
  /** Result is 1-based, but end is exclusive */
290
285
  export function getJSONSourceLocation(
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
- |} {
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
+ } {
303
298
  let v = getJSONHighlightLocation(pos, type);
304
299
  return {start: v.start, end: {line: v.end.line, column: v.end.column + 1}};
305
300
  }
306
301
 
307
302
  export function convertSourceLocationToHighlight<
308
- Location: {
303
+ Location extends {
309
304
  /** 1-based, inclusive */
310
- +start: {|
311
- +line: number,
312
- +column: number,
313
- |},
305
+ start: {
306
+ line: number;
307
+ column: number;
308
+ };
314
309
  /** 1-based, exclusive */
315
- +end: {|
316
- +line: number,
317
- +column: number,
318
- |},
319
- ...
310
+ end: {
311
+ line: number;
312
+ column: number;
313
+ };
320
314
  },
321
315
  >({start, end}: Location, message?: string): DiagnosticCodeHighlight {
322
316
  return {message, start, end: {line: end.line, column: end.column - 1}};
@@ -338,11 +332,20 @@ export function escapeMarkdown(s: string): string {
338
332
  return result;
339
333
  }
340
334
 
341
- type TemplateInput = $FlowFixMe;
335
+ export type TemplateInput = any;
342
336
 
343
337
  const mdVerbatim = Symbol();
344
- export function md(
345
- strings: Array<string>,
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,
346
349
  ...params: Array<TemplateInput>
347
350
  ): string {
348
351
  let result = [];
@@ -362,7 +365,7 @@ export function md(
362
365
  }
363
366
  }
364
367
  return result.join('') + strings[strings.length - 1];
365
- }
368
+ };
366
369
 
367
370
  md.bold = function (s: TemplateInput): TemplateInput {
368
371
  // $FlowFixMe[invalid-computed-prop]
@@ -1,7 +1,6 @@
1
- // @flow strict-local
2
1
  import assert from 'assert';
3
2
 
4
- import {generateJSONCodeHighlights} from '../src/diagnostic';
3
+ import {generateJSONCodeHighlights} from '../src/index.mts';
5
4
 
6
5
  describe('generateJSONCodeHighlights', () => {
7
6
  it('returns an escaped string 01', () => {
@@ -1,7 +1,6 @@
1
- // @flow
2
1
  import assert from 'assert';
3
2
 
4
- import {escapeMarkdown, md} from '../src/diagnostic';
3
+ import {escapeMarkdown, md} from '../src/index.mts';
5
4
 
6
5
  describe('escapeMarkdown', () => {
7
6
  it('returns an escaped string 01', () => {
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../tsconfig.json",
3
+ "include": ["src"]
4
+ }
package/lib/diagnostic.js DELETED
@@ -1,285 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.anyToDiagnostic = anyToDiagnostic;
7
- exports.convertSourceLocationToHighlight = convertSourceLocationToHighlight;
8
- exports.default = void 0;
9
- exports.encodeJSONKeyComponent = encodeJSONKeyComponent;
10
- exports.errorToDiagnostic = errorToDiagnostic;
11
- exports.escapeMarkdown = escapeMarkdown;
12
- exports.generateJSONCodeHighlights = generateJSONCodeHighlights;
13
- exports.getJSONHighlightLocation = getJSONHighlightLocation;
14
- exports.getJSONSourceLocation = getJSONSourceLocation;
15
- exports.md = md;
16
- function _assert() {
17
- const data = _interopRequireDefault(require("assert"));
18
- _assert = function () {
19
- return data;
20
- };
21
- return data;
22
- }
23
- function _nullthrows() {
24
- const data = _interopRequireDefault(require("nullthrows"));
25
- _nullthrows = function () {
26
- return data;
27
- };
28
- return data;
29
- }
30
- function _jsonSourcemap() {
31
- const data = require("@mischnic/json-sourcemap");
32
- _jsonSourcemap = function () {
33
- return data;
34
- };
35
- return data;
36
- }
37
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38
- /** These positions are 1-based (so <code>1</code> is the first line/column) */
39
- /**
40
- * Note: A tab character is always counted as a single character
41
- * This is to prevent any mismatch of highlighting across machines
42
- */
43
- /**
44
- * Describes how to format a code frame.
45
- * A code frame is a visualization of a piece of code with a certain amount of
46
- * code highlights that point to certain chunk(s) inside the code.
47
- */
48
- /** A JSON object (as in "map") */
49
- /**
50
- * A style agnostic way of emitting errors, warnings and info.
51
- * Reporters are responsible for rendering the message, codeframes, hints, ...
52
- */
53
- // This type should represent all error formats Atlaspack can encounter...
54
- /** Something that can be turned into a diagnostic. */
55
- /** Normalize the given value into a diagnostic. */
56
- function anyToDiagnostic(input) {
57
- if (Array.isArray(input)) {
58
- return input.flatMap(e => anyToDiagnostic(e));
59
- } else if (input instanceof ThrowableDiagnostic) {
60
- return input.diagnostics;
61
- } else if (input instanceof Error) {
62
- return errorToDiagnostic(input);
63
- } else if (typeof input === 'string') {
64
- return [{
65
- message: input
66
- }];
67
- } else if (typeof input === 'object') {
68
- return [input];
69
- } else {
70
- return errorToDiagnostic(input);
71
- }
72
- }
73
-
74
- /** Normalize the given error into a diagnostic. */
75
- function errorToDiagnostic(error, defaultValues) {
76
- let codeFrames = undefined;
77
- if (typeof error === 'string') {
78
- return [{
79
- origin: (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'Error',
80
- message: escapeMarkdown(error)
81
- }];
82
- }
83
- if (error instanceof ThrowableDiagnostic) {
84
- return error.diagnostics.map(d => {
85
- return {
86
- ...d,
87
- origin: d.origin ?? (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'unknown'
88
- };
89
- });
90
- }
91
- if (error.loc && error.source != null) {
92
- codeFrames = [{
93
- filePath: error.filePath ?? error.fileName ?? (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.filePath) ?? undefined,
94
- code: error.source,
95
- codeHighlights: [{
96
- start: {
97
- line: error.loc.line,
98
- column: error.loc.column
99
- },
100
- end: {
101
- line: error.loc.line,
102
- column: error.loc.column
103
- }
104
- }]
105
- }];
106
- }
107
- return [{
108
- origin: (defaultValues === null || defaultValues === void 0 ? void 0 : defaultValues.origin) ?? 'Error',
109
- message: escapeMarkdown(error.message),
110
- name: error.name,
111
- stack: codeFrames == null ? error.highlightedCodeFrame ?? error.codeFrame ?? error.stack : undefined,
112
- codeFrames
113
- }];
114
- }
115
- /**
116
- * An error wrapper around a diagnostic that can be <code>throw</code>n (e.g. to signal a
117
- * build error).
118
- */
119
- class ThrowableDiagnostic extends Error {
120
- constructor(opts) {
121
- let diagnostics = Array.isArray(opts.diagnostic) ? opts.diagnostic : [opts.diagnostic];
122
-
123
- // Construct error from diagnostics
124
- super(diagnostics[0].message);
125
- // @ts-ignore
126
- this.stack = diagnostics[0].stack ?? super.stack;
127
- // @ts-ignore
128
- this.name = diagnostics[0].name ?? super.name;
129
- this.diagnostics = diagnostics;
130
- }
131
- }
132
-
133
- /**
134
- * Turns a list of positions in a JSON5 file with messages into a list of diagnostics.
135
- * Uses <a href="https://github.com/mischnic/json-sourcemap">@mischnic/json-sourcemap</a>.
136
- *
137
- * @param code the JSON code
138
- * @param ids A list of JSON keypaths (<code>key: "/some/parent/child"</code>) with corresponding messages, \
139
- * <code>type</code> signifies whether the key of the value in a JSON object should be highlighted.
140
- */
141
- exports.default = ThrowableDiagnostic;
142
- function generateJSONCodeHighlights(data, ids) {
143
- let map = typeof data == 'string' ? (0, _jsonSourcemap().parse)(data, undefined, {
144
- dialect: 'JSON5',
145
- tabWidth: 1
146
- }) : data;
147
- return ids.map(({
148
- key,
149
- type,
150
- message
151
- }) => {
152
- let pos = (0, _nullthrows().default)(map.pointers[key]);
153
- return {
154
- ...getJSONHighlightLocation(pos, type),
155
- message
156
- };
157
- });
158
- }
159
-
160
- /**
161
- * Converts entries in <a href="https://github.com/mischnic/json-sourcemap">@mischnic/json-sourcemap</a>'s
162
- * <code>result.pointers</code> array.
163
- */
164
- function getJSONHighlightLocation(pos, type) {
165
- let key = 'key' in pos ? pos.key : undefined;
166
- let keyEnd = 'keyEnd' in pos ? pos.keyEnd : undefined;
167
- if (!type && key && pos.value) {
168
- // key and value
169
- return {
170
- start: {
171
- line: key.line + 1,
172
- column: key.column + 1
173
- },
174
- end: {
175
- line: pos.valueEnd.line + 1,
176
- column: pos.valueEnd.column
177
- }
178
- };
179
- } else if (type == 'key' || !pos.value) {
180
- (0, _assert().default)(key && keyEnd);
181
- return {
182
- start: {
183
- line: key.line + 1,
184
- column: key.column + 1
185
- },
186
- end: {
187
- line: keyEnd.line + 1,
188
- column: keyEnd.column
189
- }
190
- };
191
- } else {
192
- return {
193
- start: {
194
- line: pos.value.line + 1,
195
- column: pos.value.column + 1
196
- },
197
- end: {
198
- line: pos.valueEnd.line + 1,
199
- column: pos.valueEnd.column
200
- }
201
- };
202
- }
203
- }
204
-
205
- /** Result is 1-based, but end is exclusive */
206
- function getJSONSourceLocation(pos, type) {
207
- let v = getJSONHighlightLocation(pos, type);
208
- return {
209
- start: v.start,
210
- end: {
211
- line: v.end.line,
212
- column: v.end.column + 1
213
- }
214
- };
215
- }
216
- function convertSourceLocationToHighlight({
217
- start,
218
- end
219
- }, message) {
220
- return {
221
- message,
222
- start,
223
- end: {
224
- line: end.line,
225
- column: end.column - 1
226
- }
227
- };
228
- }
229
-
230
- /** Sanitizes object keys before using them as <code>key</code> in generateJSONCodeHighlights */
231
- function encodeJSONKeyComponent(component) {
232
- return component.replace(/~/g, '~0').replace(/\//g, '~1');
233
- }
234
- const escapeCharacters = ['\\', '*', '_', '~'];
235
- function escapeMarkdown(s) {
236
- let result = s;
237
- for (const char of escapeCharacters) {
238
- result = result.replace(new RegExp(`\\${char}`, 'g'), `\\${char}`);
239
- }
240
- return result;
241
- }
242
- const mdVerbatim = Symbol();
243
- function md(strings, ...params) {
244
- let result = [];
245
- for (let i = 0; i < params.length; i++) {
246
- result.push(strings[i]);
247
- let param = params[i];
248
- if (Array.isArray(param)) {
249
- for (let j = 0; j < param.length; 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]}`));
252
- if (j < param.length - 1) {
253
- result.push(', ');
254
- }
255
- }
256
- } else {
257
- result.push((param === null || param === void 0 ? void 0 : param[mdVerbatim]) ?? escapeMarkdown(`${param}`));
258
- }
259
- }
260
- return result.join('') + strings[strings.length - 1];
261
- }
262
- md.bold = function (s) {
263
- // $FlowFixMe[invalid-computed-prop]
264
- return {
265
- [mdVerbatim]: '**' + escapeMarkdown(`${s}`) + '**'
266
- };
267
- };
268
- md.italic = function (s) {
269
- // $FlowFixMe[invalid-computed-prop]
270
- return {
271
- [mdVerbatim]: '_' + escapeMarkdown(`${s}`) + '_'
272
- };
273
- };
274
- md.underline = function (s) {
275
- // $FlowFixMe[invalid-computed-prop]
276
- return {
277
- [mdVerbatim]: '__' + escapeMarkdown(`${s}`) + '__'
278
- };
279
- };
280
- md.strikethrough = function (s) {
281
- // $FlowFixMe[invalid-computed-prop]
282
- return {
283
- [mdVerbatim]: '~~' + escapeMarkdown(`${s}`) + '~~'
284
- };
285
- };