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