@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,10 +1,10 @@
1
- import * as jsonSourcemap from '@mischnic/json-sourcemap';
1
+ import type { Mapping } from "@mischnic/json-sourcemap";
2
2
  /** These positions are 1-based (so <code>1</code> is the first line/column) */
3
3
  export type DiagnosticHighlightLocation = {
4
- line: number;
5
- column: number;
4
+ readonly line: number;
5
+ readonly column: number;
6
6
  };
7
- export type DiagnosticSeverity = 'error' | 'warn' | 'info';
7
+ export type DiagnosticSeverity = "error" | "warn" | "info";
8
8
  /**
9
9
  * Note: A tab character is always counted as a single character
10
10
  * This is to prevent any mismatch of highlighting across machines
@@ -38,9 +38,7 @@ export type DiagnosticCodeFrame = {
38
38
  codeHighlights: Array<DiagnosticCodeHighlight>;
39
39
  };
40
40
  /** A JSON object (as in "map") */
41
- export type JSONObject = {
42
- [key: string]: any;
43
- };
41
+ type JSONObject = Record<string, any>;
44
42
  /**
45
43
  * A style agnostic way of emitting errors, warnings and info.
46
44
  * Reporters are responsible for rendering the message, codeframes, hints, ...
@@ -55,7 +53,7 @@ export type Diagnostic = {
55
53
  /** Name of the error (optional) */
56
54
  name?: string;
57
55
  /** A code frame points to a certain location(s) in the file this diagnostic is linked to (optional) */
58
- codeFrames?: Array<DiagnosticCodeFrame>;
56
+ codeFrames?: Array<DiagnosticCodeFrame> | null | undefined;
59
57
  /** An optional list of strings that suggest ways to resolve this issue */
60
58
  hints?: Array<string>;
61
59
  /** @private */
@@ -73,7 +71,7 @@ export interface PrintableError extends Error {
73
71
  loc?: {
74
72
  column: number;
75
73
  line: number;
76
- };
74
+ } | null | undefined;
77
75
  source?: string;
78
76
  }
79
77
  export type DiagnosticWithoutOrigin = Diagnostic & {
@@ -85,10 +83,10 @@ export type Diagnostifiable = Diagnostic | Array<Diagnostic> | ThrowableDiagnost
85
83
  export declare function anyToDiagnostic(input: Diagnostifiable): Array<Diagnostic>;
86
84
  /** Normalize the given error into a diagnostic. */
87
85
  export declare function errorToDiagnostic(error: ThrowableDiagnostic | PrintableError | string, defaultValues?: {
88
- origin?: string;
89
- filePath?: string;
86
+ origin?: string | null | undefined;
87
+ filePath?: string | null | undefined;
90
88
  }): Array<Diagnostic>;
91
- export type ThrowableDiagnosticOpts = {
89
+ type ThrowableDiagnosticOpts = {
92
90
  diagnostic: Diagnostic | Array<Diagnostic>;
93
91
  };
94
92
  /**
@@ -108,55 +106,53 @@ export default class ThrowableDiagnostic extends Error {
108
106
  * <code>type</code> signifies whether the key of the value in a JSON object should be highlighted.
109
107
  */
110
108
  export declare function generateJSONCodeHighlights(data: string | {
111
- data: any;
112
- pointers: {
113
- [key: string]: jsonSourcemap.Mapping;
114
- };
109
+ data: unknown;
110
+ pointers: Record<string, Mapping>;
115
111
  }, ids: Array<{
116
112
  key: string;
117
- type?: 'key' | 'value';
113
+ type?: ("key" | null | undefined) | "value";
118
114
  message?: string;
119
115
  }>): Array<DiagnosticCodeHighlight>;
120
116
  /**
121
117
  * Converts entries in <a href="https://github.com/mischnic/json-sourcemap">@mischnic/json-sourcemap</a>'s
122
118
  * <code>result.pointers</code> array.
123
119
  */
124
- export declare function getJSONHighlightLocation(pos: jsonSourcemap.Mapping, type?: 'key' | 'value'): {
120
+ export declare function getJSONHighlightLocation(pos: Mapping, type?: ("key" | null | undefined) | "value"): {
125
121
  start: DiagnosticHighlightLocation;
126
122
  end: DiagnosticHighlightLocation;
127
123
  };
128
124
  /** Result is 1-based, but end is exclusive */
129
- export declare function getJSONSourceLocation(pos: jsonSourcemap.Mapping, type?: 'key' | 'value'): {
125
+ export declare function getJSONSourceLocation(pos: Mapping, type?: ("key" | null | undefined) | "value"): {
130
126
  start: {
131
- line: number;
132
- column: number;
127
+ readonly line: number;
128
+ readonly column: number;
133
129
  };
134
130
  end: {
135
- line: number;
136
- column: number;
131
+ readonly line: number;
132
+ readonly column: number;
137
133
  };
138
134
  };
139
135
  export declare function convertSourceLocationToHighlight<Location extends {
140
136
  /** 1-based, inclusive */
141
- start: {
142
- line: number;
143
- column: number;
137
+ readonly start: {
138
+ readonly line: number;
139
+ readonly column: number;
144
140
  };
145
141
  /** 1-based, exclusive */
146
- end: {
147
- line: number;
148
- column: number;
142
+ readonly end: {
143
+ readonly line: number;
144
+ readonly column: number;
149
145
  };
150
146
  }>({ start, end }: Location, message?: string): DiagnosticCodeHighlight;
151
147
  /** Sanitizes object keys before using them as <code>key</code> in generateJSONCodeHighlights */
152
148
  export declare function encodeJSONKeyComponent(component: string): string;
153
149
  export declare function escapeMarkdown(s: string): string;
154
- export type TemplateInput = any;
155
- export interface MdFunction {
156
- (strings: TemplateStringsArray, ...params: Array<TemplateInput>): string;
157
- bold: (s: TemplateInput) => TemplateInput;
158
- italic: (s: TemplateInput) => TemplateInput;
159
- underline: (s: TemplateInput) => TemplateInput;
160
- strikethrough: (s: TemplateInput) => TemplateInput;
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;
161
157
  }
162
- export declare const md: MdFunction;
158
+ export {};
@@ -0,0 +1,285 @@
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/diagnostic",
3
- "version": "2.14.2-typescript-d6e6d169c.0",
3
+ "version": "2.14.2-typescript-e769947a5.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": {
@@ -10,30 +10,20 @@
10
10
  "type": "git",
11
11
  "url": "https://github.com/atlassian-labs/atlaspack.git"
12
12
  },
13
- "main": "lib/index.js",
14
- "types": "lib/index.d.ts",
15
- "exports": {
16
- ".": {
17
- "atlaspack::sources": "./src/index.mts",
18
- "types": [
19
- "./lib/index.d.ts",
20
- "./src/index.mts"
21
- ],
22
- "import": "./lib/index.mjs",
23
- "require": "./lib/index.js",
24
- "default": "./lib/index.js"
25
- },
26
- "./*": "./*"
27
- },
13
+ "main": "lib/diagnostic.js",
14
+ "source": "src/diagnostic.js",
15
+ "types": "lib/diagnostic.d.ts",
28
16
  "engines": {
29
17
  "node": ">= 16.0.0"
30
18
  },
31
19
  "scripts": {
32
- "build-tsc": "node ../../../scripts/build-tsc.mjs"
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"
33
22
  },
34
23
  "dependencies": {
35
24
  "@mischnic/json-sourcemap": "^0.1.0",
36
25
  "nullthrows": "^1.1.1"
37
26
  },
38
- "gitHead": "d6e6d169cb16a0bbc98d7743646a7097b82c7fe2"
27
+ "type": "commonjs",
28
+ "gitHead": "e769947a5b0c22d9477211ac1ce6614e6bf9def3"
39
29
  }