@parcel/utils 2.0.0-nightly.122 → 2.0.0-nightly.1221
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/.eslintrc.js +6 -6
- package/lib/index.js +36742 -307
- package/lib/index.js.map +1 -0
- package/package.json +43 -20
- package/src/DefaultMap.js +1 -1
- package/src/PromiseQueue.js +16 -12
- package/src/alternatives.js +143 -0
- package/src/ansi-html.js +2 -2
- package/src/blob.js +2 -1
- package/src/bundle-url.js +1 -1
- package/src/collection.js +26 -14
- package/src/config.js +101 -36
- package/src/countLines.js +5 -2
- package/src/debounce.js +1 -1
- package/src/dependency-location.js +11 -6
- package/src/generateBuildMetrics.js +158 -0
- package/src/generateCertificate.js +1 -1
- package/src/getCertificate.js +1 -1
- package/src/getExisting.js +1 -4
- package/src/getModuleParts.js +23 -0
- package/src/getRootDir.js +1 -2
- package/src/glob.js +39 -10
- package/src/hash.js +34 -0
- package/src/http-server.js +29 -19
- package/src/index.js +55 -21
- package/src/is-url.js +1 -1
- package/src/isDirectoryInside.js +11 -0
- package/src/openInBrowser.js +64 -0
- package/src/path.js +38 -6
- package/src/prettyDiagnostic.js +70 -24
- package/src/progress-message.js +22 -0
- package/src/relativeBundlePath.js +8 -13
- package/src/replaceBundleReferences.js +85 -41
- package/src/schema.js +101 -44
- package/src/shared-buffer.js +24 -0
- package/src/sourcemap.js +135 -0
- package/src/stream.js +31 -1
- package/src/urlJoin.js +3 -1
- package/test/DefaultMap.test.js +7 -4
- package/test/config.test.js +98 -0
- package/test/input/config/.testrc +3 -0
- package/test/input/config/config.cjs +3 -0
- package/test/input/config/config.js +3 -0
- package/test/input/config/config.json +3 -0
- package/test/input/config/empty.json +0 -0
- package/test/input/config/empty.toml +0 -0
- package/test/input/sourcemap/referenced-min.js +2 -0
- package/test/input/sourcemap/referenced-min.js.map +6 -0
- package/test/input/sourcemap/source-root.js +2 -0
- package/test/input/sourcemap/source-root.js.map +7 -0
- package/test/replaceBundleReferences.test.js +268 -0
- package/test/sourcemap.test.js +207 -0
- package/test/throttle.test.js +1 -2
- package/test/urlJoin.test.js +37 -0
- package/lib/DefaultMap.js +0 -64
- package/lib/Deferred.js +0 -26
- package/lib/PromiseQueue.js +0 -133
- package/lib/TapStream.js +0 -41
- package/lib/ansi-html.js +0 -16
- package/lib/blob.js +0 -31
- package/lib/bundle-url.js +0 -43
- package/lib/collection.js +0 -62
- package/lib/config.js +0 -88
- package/lib/countLines.js +0 -18
- package/lib/debounce.js +0 -20
- package/lib/dependency-location.js +0 -21
- package/lib/escape-html.js +0 -24
- package/lib/generateBundleReport.js +0 -38
- package/lib/generateCertificate.js +0 -124
- package/lib/getCertificate.js +0 -19
- package/lib/getExisting.js +0 -23
- package/lib/getRootDir.js +0 -55
- package/lib/glob.js +0 -76
- package/lib/http-server.js +0 -64
- package/lib/is-url.js +0 -17
- package/lib/loadSourceMapUrl.js +0 -33
- package/lib/md5.js +0 -35
- package/lib/objectHash.js +0 -26
- package/lib/parseCSSImport.js +0 -16
- package/lib/path.js +0 -22
- package/lib/prettifyTime.js +0 -10
- package/lib/prettyDiagnostic.js +0 -57
- package/lib/promisify.js +0 -13
- package/lib/relativeBundlePath.js +0 -24
- package/lib/relativeUrl.js +0 -16
- package/lib/replaceBundleReferences.js +0 -151
- package/lib/resolve.js +0 -93
- package/lib/schema.js +0 -320
- package/lib/serializeObject.js +0 -28
- package/lib/stream.js +0 -51
- package/lib/throttle.js +0 -16
- package/lib/urlJoin.js +0 -27
- package/src/.babelrc +0 -3
- package/src/generateBundleReport.js +0 -51
- package/src/loadSourceMapUrl.js +0 -33
- package/src/md5.js +0 -44
- package/src/promisify.js +0 -13
- package/src/resolve.js +0 -135
- package/src/serializeObject.js +0 -22
- package/test/input/sourcemap/referenced.js +0 -7
- package/test/loadSourceMapUrl.test.js +0 -37
package/src/prettyDiagnostic.js
CHANGED
|
@@ -1,61 +1,101 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
import type {Diagnostic} from '@parcel/diagnostic';
|
|
3
|
+
import type {PluginOptions} from '@parcel/types';
|
|
3
4
|
|
|
4
5
|
import formatCodeFrame from '@parcel/codeframe';
|
|
5
6
|
import mdAnsi from '@parcel/markdown-ansi';
|
|
6
7
|
import chalk from 'chalk';
|
|
7
8
|
import path from 'path';
|
|
9
|
+
// $FlowFixMe
|
|
10
|
+
import terminalLink from 'terminal-link';
|
|
11
|
+
|
|
12
|
+
export type FormattedCodeFrame = {|
|
|
13
|
+
location: string,
|
|
14
|
+
code: string,
|
|
15
|
+
|};
|
|
8
16
|
|
|
9
17
|
export type AnsiDiagnosticResult = {|
|
|
10
18
|
message: string,
|
|
11
19
|
stack: string,
|
|
20
|
+
/** A formatted string containing all code frames, including their file locations. */
|
|
12
21
|
codeframe: string,
|
|
22
|
+
/** A list of code frames with highlighted code and file locations separately. */
|
|
23
|
+
frames: Array<FormattedCodeFrame>,
|
|
13
24
|
hints: Array<string>,
|
|
25
|
+
documentation: string,
|
|
14
26
|
|};
|
|
15
27
|
|
|
16
|
-
export default function prettyDiagnostic(
|
|
28
|
+
export default async function prettyDiagnostic(
|
|
17
29
|
diagnostic: Diagnostic,
|
|
18
|
-
|
|
30
|
+
options?: PluginOptions,
|
|
31
|
+
terminalWidth?: number,
|
|
32
|
+
): Promise<AnsiDiagnosticResult> {
|
|
19
33
|
let {
|
|
20
34
|
origin,
|
|
21
35
|
message,
|
|
22
36
|
stack,
|
|
23
|
-
|
|
37
|
+
codeFrames,
|
|
24
38
|
hints,
|
|
25
|
-
filePath,
|
|
26
|
-
language,
|
|
27
39
|
skipFormatting,
|
|
40
|
+
documentationURL,
|
|
28
41
|
} = diagnostic;
|
|
29
42
|
|
|
30
43
|
let result = {
|
|
31
44
|
message:
|
|
32
45
|
mdAnsi(`**${origin ?? 'unknown'}**: `) +
|
|
33
46
|
(skipFormatting ? message : mdAnsi(message)),
|
|
34
|
-
stack:
|
|
47
|
+
stack: '',
|
|
35
48
|
codeframe: '',
|
|
49
|
+
frames: [],
|
|
36
50
|
hints: [],
|
|
51
|
+
documentation: '',
|
|
37
52
|
};
|
|
38
53
|
|
|
39
|
-
if (
|
|
40
|
-
let
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
if (codeFrames != null) {
|
|
55
|
+
for (let codeFrame of codeFrames) {
|
|
56
|
+
let filePath = codeFrame.filePath;
|
|
57
|
+
if (filePath != null && options && !path.isAbsolute(filePath)) {
|
|
58
|
+
filePath = path.join(options.projectRoot, filePath);
|
|
59
|
+
}
|
|
43
60
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
61
|
+
let highlights = codeFrame.codeHighlights;
|
|
62
|
+
let code = codeFrame.code;
|
|
63
|
+
if (code == null && options && filePath != null) {
|
|
64
|
+
code = await options.inputFS.readFile(filePath, 'utf8');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let formattedCodeFrame = '';
|
|
68
|
+
if (code != null) {
|
|
69
|
+
formattedCodeFrame = formatCodeFrame(code, highlights, {
|
|
70
|
+
useColor: true,
|
|
71
|
+
syntaxHighlighting: true,
|
|
72
|
+
language:
|
|
73
|
+
// $FlowFixMe sketchy null checks do not matter here...
|
|
74
|
+
codeFrame.language ||
|
|
75
|
+
(filePath != null ? path.extname(filePath).substr(1) : undefined),
|
|
76
|
+
terminalWidth,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let location =
|
|
81
|
+
typeof filePath !== 'string'
|
|
82
|
+
? ''
|
|
83
|
+
: `${filePath}:${highlights[0].start.line}:${highlights[0].start.column}`;
|
|
84
|
+
result.codeframe += location ? chalk.gray.underline(location) + '\n' : '';
|
|
85
|
+
result.codeframe += formattedCodeFrame;
|
|
86
|
+
if (codeFrame !== codeFrames[codeFrames.length - 1]) {
|
|
87
|
+
result.codeframe += '\n\n';
|
|
88
|
+
}
|
|
51
89
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
90
|
+
result.frames.push({
|
|
91
|
+
location,
|
|
92
|
+
code: formattedCodeFrame,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (stack != null) {
|
|
98
|
+
result.stack = stack;
|
|
59
99
|
}
|
|
60
100
|
|
|
61
101
|
if (Array.isArray(hints) && hints.length) {
|
|
@@ -64,5 +104,11 @@ export default function prettyDiagnostic(
|
|
|
64
104
|
});
|
|
65
105
|
}
|
|
66
106
|
|
|
107
|
+
if (documentationURL != null) {
|
|
108
|
+
result.documentation = terminalLink('Learn more', documentationURL, {
|
|
109
|
+
fallback: (text, url) => `${text}: ${url}`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
67
113
|
return result;
|
|
68
114
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// @flow strict-local
|
|
2
|
+
import type {BuildProgressEvent} from '@parcel/types';
|
|
3
|
+
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
export function getProgressMessage(event: BuildProgressEvent): ?string {
|
|
7
|
+
switch (event.phase) {
|
|
8
|
+
case 'transforming':
|
|
9
|
+
return `Building ${path.basename(event.filePath)}...`;
|
|
10
|
+
|
|
11
|
+
case 'bundling':
|
|
12
|
+
return 'Bundling...';
|
|
13
|
+
|
|
14
|
+
case 'packaging':
|
|
15
|
+
return `Packaging ${event.bundle.displayName}...`;
|
|
16
|
+
|
|
17
|
+
case 'optimizing':
|
|
18
|
+
return `Optimizing ${event.bundle.displayName}...`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
|
|
3
|
-
import type {
|
|
3
|
+
import type {FilePath, NamedBundle} from '@parcel/types';
|
|
4
4
|
|
|
5
5
|
import path from 'path';
|
|
6
|
-
import
|
|
6
|
+
import {relativePath} from './path';
|
|
7
7
|
|
|
8
8
|
export function relativeBundlePath(
|
|
9
|
-
from:
|
|
10
|
-
to:
|
|
9
|
+
from: NamedBundle,
|
|
10
|
+
to: NamedBundle,
|
|
11
11
|
opts: {|leadingDotSlash: boolean|} = {leadingDotSlash: true},
|
|
12
|
-
) {
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (opts.leadingDotSlash && p[0] !== '.') {
|
|
17
|
-
p = './' + p;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return p;
|
|
12
|
+
): FilePath {
|
|
13
|
+
let fromPath = path.join(from.target.distDir, from.name);
|
|
14
|
+
let toPath = path.join(to.target.distDir, to.name);
|
|
15
|
+
return relativePath(path.dirname(fromPath), toPath, opts.leadingDotSlash);
|
|
21
16
|
}
|
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
|
|
3
3
|
import type SourceMap from '@parcel/source-map';
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
Async,
|
|
6
|
+
Blob,
|
|
7
|
+
Bundle,
|
|
8
|
+
BundleGraph,
|
|
9
|
+
Dependency,
|
|
10
|
+
NamedBundle,
|
|
11
|
+
} from '@parcel/types';
|
|
5
12
|
|
|
6
13
|
import {Readable} from 'stream';
|
|
7
14
|
import nullthrows from 'nullthrows';
|
|
15
|
+
import invariant from 'assert';
|
|
8
16
|
import URL from 'url';
|
|
9
|
-
import {bufferStream, relativeBundlePath, urlJoin} from '
|
|
17
|
+
import {bufferStream, relativeBundlePath, urlJoin} from './';
|
|
10
18
|
|
|
11
19
|
type ReplacementMap = Map<
|
|
12
20
|
string /* dependency id */,
|
|
@@ -15,7 +23,7 @@ type ReplacementMap = Map<
|
|
|
15
23
|
|
|
16
24
|
/*
|
|
17
25
|
* Replaces references to dependency ids for URL dependencies with:
|
|
18
|
-
* - in the case of an unresolvable url dependency, the original
|
|
26
|
+
* - in the case of an unresolvable url dependency, the original specifier.
|
|
19
27
|
* These are external requests that Parcel did not bundle.
|
|
20
28
|
* - in the case of a reference to another bundle, the relative url to that
|
|
21
29
|
* bundle from the current bundle.
|
|
@@ -25,44 +33,55 @@ export function replaceURLReferences({
|
|
|
25
33
|
bundleGraph,
|
|
26
34
|
contents,
|
|
27
35
|
map,
|
|
36
|
+
getReplacement = s => s,
|
|
28
37
|
relative = true,
|
|
29
38
|
}: {|
|
|
30
|
-
bundle:
|
|
31
|
-
bundleGraph: BundleGraph
|
|
39
|
+
bundle: NamedBundle,
|
|
40
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
32
41
|
contents: string,
|
|
33
42
|
relative?: boolean,
|
|
34
43
|
map?: ?SourceMap,
|
|
44
|
+
getReplacement?: string => string,
|
|
35
45
|
|}): {|+contents: string, +map: ?SourceMap|} {
|
|
36
46
|
let replacements = new Map();
|
|
47
|
+
let urlDependencies = [];
|
|
48
|
+
bundle.traverse(node => {
|
|
49
|
+
if (node.type === 'dependency' && node.value.specifierType === 'url') {
|
|
50
|
+
urlDependencies.push(node.value);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
37
53
|
|
|
38
|
-
for (let dependency of
|
|
39
|
-
if (
|
|
54
|
+
for (let dependency of urlDependencies) {
|
|
55
|
+
if (dependency.specifierType !== 'url') {
|
|
40
56
|
continue;
|
|
41
57
|
}
|
|
42
58
|
|
|
43
|
-
let
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
60
|
+
invariant(typeof placeholder === 'string');
|
|
61
|
+
|
|
62
|
+
let resolved = bundleGraph.getReferencedBundle(dependency, bundle);
|
|
63
|
+
if (resolved == null) {
|
|
64
|
+
replacements.set(placeholder, {
|
|
65
|
+
from: placeholder,
|
|
66
|
+
to: getReplacement(dependency.specifier),
|
|
48
67
|
});
|
|
49
68
|
continue;
|
|
50
69
|
}
|
|
51
70
|
|
|
52
|
-
|
|
53
|
-
if (entryBundle.isInline) {
|
|
71
|
+
if (resolved.bundleBehavior === 'inline') {
|
|
54
72
|
// If a bundle is inline, it should be replaced with inline contents,
|
|
55
73
|
// not a URL.
|
|
56
74
|
continue;
|
|
57
75
|
}
|
|
58
76
|
|
|
59
77
|
replacements.set(
|
|
60
|
-
|
|
78
|
+
placeholder,
|
|
61
79
|
getURLReplacement({
|
|
62
80
|
dependency,
|
|
63
81
|
fromBundle: bundle,
|
|
64
|
-
toBundle:
|
|
82
|
+
toBundle: resolved,
|
|
65
83
|
relative,
|
|
84
|
+
getReplacement,
|
|
66
85
|
}),
|
|
67
86
|
);
|
|
68
87
|
}
|
|
@@ -83,7 +102,7 @@ export async function replaceInlineReferences({
|
|
|
83
102
|
getInlineBundleContents,
|
|
84
103
|
}: {|
|
|
85
104
|
bundle: Bundle,
|
|
86
|
-
bundleGraph: BundleGraph
|
|
105
|
+
bundleGraph: BundleGraph<NamedBundle>,
|
|
87
106
|
contents: string,
|
|
88
107
|
getInlineReplacement: (
|
|
89
108
|
Dependency,
|
|
@@ -92,20 +111,22 @@ export async function replaceInlineReferences({
|
|
|
92
111
|
) => {|from: string, to: string|},
|
|
93
112
|
getInlineBundleContents: (
|
|
94
113
|
Bundle,
|
|
95
|
-
BundleGraph
|
|
96
|
-
) => Async<{|contents: Blob
|
|
114
|
+
BundleGraph<NamedBundle>,
|
|
115
|
+
) => Async<{|contents: Blob|}>,
|
|
97
116
|
map?: ?SourceMap,
|
|
98
117
|
|}): Promise<{|+contents: string, +map: ?SourceMap|}> {
|
|
99
118
|
let replacements = new Map();
|
|
100
119
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
120
|
+
let dependencies = [];
|
|
121
|
+
bundle.traverse(node => {
|
|
122
|
+
if (node.type === 'dependency') {
|
|
123
|
+
dependencies.push(node.value);
|
|
105
124
|
}
|
|
125
|
+
});
|
|
106
126
|
|
|
107
|
-
|
|
108
|
-
|
|
127
|
+
for (let dependency of dependencies) {
|
|
128
|
+
let entryBundle = bundleGraph.getReferencedBundle(dependency, bundle);
|
|
129
|
+
if (entryBundle?.bundleBehavior !== 'inline') {
|
|
109
130
|
continue;
|
|
110
131
|
}
|
|
111
132
|
|
|
@@ -113,15 +134,18 @@ export async function replaceInlineReferences({
|
|
|
113
134
|
entryBundle,
|
|
114
135
|
bundleGraph,
|
|
115
136
|
);
|
|
116
|
-
let packagedContents = (
|
|
117
|
-
|
|
118
|
-
|
|
137
|
+
let packagedContents = (
|
|
138
|
+
packagedBundle.contents instanceof Readable
|
|
139
|
+
? await bufferStream(packagedBundle.contents)
|
|
140
|
+
: packagedBundle.contents
|
|
119
141
|
).toString();
|
|
120
142
|
|
|
121
143
|
let inlineType = nullthrows(entryBundle.getMainEntry()).meta.inlineType;
|
|
122
144
|
if (inlineType == null || inlineType === 'string') {
|
|
145
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
146
|
+
invariant(typeof placeholder === 'string');
|
|
123
147
|
replacements.set(
|
|
124
|
-
|
|
148
|
+
placeholder,
|
|
125
149
|
getInlineReplacement(dependency, inlineType, packagedContents),
|
|
126
150
|
);
|
|
127
151
|
}
|
|
@@ -130,32 +154,52 @@ export async function replaceInlineReferences({
|
|
|
130
154
|
return performReplacement(replacements, contents, map);
|
|
131
155
|
}
|
|
132
156
|
|
|
133
|
-
function getURLReplacement({
|
|
157
|
+
export function getURLReplacement({
|
|
134
158
|
dependency,
|
|
135
159
|
fromBundle,
|
|
136
160
|
toBundle,
|
|
137
161
|
relative,
|
|
162
|
+
getReplacement,
|
|
138
163
|
}: {|
|
|
139
164
|
dependency: Dependency,
|
|
140
|
-
fromBundle:
|
|
141
|
-
toBundle:
|
|
165
|
+
fromBundle: NamedBundle,
|
|
166
|
+
toBundle: NamedBundle,
|
|
142
167
|
relative: boolean,
|
|
143
|
-
|
|
144
|
-
|
|
168
|
+
getReplacement?: string => string,
|
|
169
|
+
|}): {|from: string, to: string|} {
|
|
145
170
|
let to;
|
|
171
|
+
|
|
172
|
+
let orig = URL.parse(dependency.specifier);
|
|
173
|
+
|
|
146
174
|
if (relative) {
|
|
147
|
-
|
|
148
|
-
|
|
175
|
+
to = URL.format({
|
|
176
|
+
pathname: relativeBundlePath(fromBundle, toBundle, {
|
|
177
|
+
leadingDotSlash: false,
|
|
178
|
+
}),
|
|
179
|
+
hash: orig.hash,
|
|
149
180
|
});
|
|
150
|
-
|
|
181
|
+
|
|
182
|
+
// If the resulting path includes a colon character and doesn't start with a ./ or ../
|
|
183
|
+
// we need to add one so that the first part before the colon isn't parsed as a URL protocol.
|
|
184
|
+
if (to.includes(':') && !to.startsWith('./') && !to.startsWith('../')) {
|
|
185
|
+
to = './' + to;
|
|
186
|
+
}
|
|
151
187
|
} else {
|
|
152
|
-
|
|
153
|
-
|
|
188
|
+
to = urlJoin(
|
|
189
|
+
toBundle.target.publicUrl,
|
|
190
|
+
URL.format({
|
|
191
|
+
pathname: nullthrows(toBundle.name),
|
|
192
|
+
hash: orig.hash,
|
|
193
|
+
}),
|
|
194
|
+
);
|
|
154
195
|
}
|
|
155
196
|
|
|
197
|
+
let placeholder = dependency.meta?.placeholder ?? dependency.id;
|
|
198
|
+
invariant(typeof placeholder === 'string');
|
|
199
|
+
|
|
156
200
|
return {
|
|
157
|
-
from:
|
|
158
|
-
to,
|
|
201
|
+
from: placeholder,
|
|
202
|
+
to: getReplacement ? getReplacement(to) : to,
|
|
159
203
|
};
|
|
160
204
|
}
|
|
161
205
|
|
package/src/schema.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
1
|
// @flow strict-local
|
|
2
2
|
import ThrowableDiagnostic, {
|
|
3
3
|
generateJSONCodeHighlights,
|
|
4
|
+
escapeMarkdown,
|
|
5
|
+
encodeJSONKeyComponent,
|
|
4
6
|
} from '@parcel/diagnostic';
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
+
import type {Mapping} from '@mischnic/json-sourcemap';
|
|
8
|
+
import nullthrows from 'nullthrows';
|
|
9
|
+
// flowlint-next-line untyped-import:off
|
|
10
|
+
import levenshtein from 'fastest-levenshtein';
|
|
7
11
|
|
|
8
12
|
export type SchemaEntity =
|
|
9
13
|
| SchemaObject
|
|
10
14
|
| SchemaArray
|
|
11
15
|
| SchemaBoolean
|
|
12
16
|
| SchemaString
|
|
17
|
+
| SchemaNumber
|
|
13
18
|
| SchemaEnum
|
|
14
19
|
| SchemaOneOf
|
|
15
20
|
| SchemaAllOf
|
|
@@ -40,6 +45,11 @@ export type SchemaString = {|
|
|
|
40
45
|
__validate?: (val: string) => ?string,
|
|
41
46
|
__type?: string,
|
|
42
47
|
|};
|
|
48
|
+
export type SchemaNumber = {|
|
|
49
|
+
type: 'number',
|
|
50
|
+
enum?: Array<number>,
|
|
51
|
+
__type?: string,
|
|
52
|
+
|};
|
|
43
53
|
export type SchemaEnum = {|
|
|
44
54
|
enum: Array<mixed>,
|
|
45
55
|
|};
|
|
@@ -88,7 +98,7 @@ export type SchemaError =
|
|
|
88
98
|
prop: string,
|
|
89
99
|
expectedProps: Array<string>,
|
|
90
100
|
actualProps: Array<string>,
|
|
91
|
-
dataType:
|
|
101
|
+
dataType: 'key' | 'value',
|
|
92
102
|
|
|
93
103
|
dataPath: string,
|
|
94
104
|
ancestors: Array<SchemaEntity>,
|
|
@@ -99,7 +109,6 @@ export type SchemaError =
|
|
|
99
109
|
actualValue: mixed,
|
|
100
110
|
dataType: ?'key' | 'value',
|
|
101
111
|
message?: string,
|
|
102
|
-
|
|
103
112
|
dataPath: string,
|
|
104
113
|
ancestors: Array<SchemaEntity>,
|
|
105
114
|
|};
|
|
@@ -159,8 +168,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
159
168
|
}
|
|
160
169
|
} else if (schemaNode.__validate) {
|
|
161
170
|
let validationError = schemaNode.__validate(value);
|
|
162
|
-
|
|
163
|
-
if (validationError) {
|
|
171
|
+
if (typeof validationError == 'string') {
|
|
164
172
|
return {
|
|
165
173
|
type: 'other',
|
|
166
174
|
dataType: 'value',
|
|
@@ -173,6 +181,23 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
173
181
|
}
|
|
174
182
|
break;
|
|
175
183
|
}
|
|
184
|
+
case 'number': {
|
|
185
|
+
// $FlowFixMe type was already checked
|
|
186
|
+
let value: number = dataNode;
|
|
187
|
+
if (schemaNode.enum) {
|
|
188
|
+
if (!schemaNode.enum.includes(value)) {
|
|
189
|
+
return {
|
|
190
|
+
type: 'enum',
|
|
191
|
+
dataType: 'value',
|
|
192
|
+
dataPath,
|
|
193
|
+
expectedValues: schemaNode.enum,
|
|
194
|
+
actualValue: value,
|
|
195
|
+
ancestors: schemaAncestors,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
176
201
|
case 'object': {
|
|
177
202
|
let results: Array<Array<SchemaError> | SchemaError> = [];
|
|
178
203
|
let invalidProps;
|
|
@@ -187,7 +212,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
187
212
|
k =>
|
|
188
213
|
({
|
|
189
214
|
type: 'forbidden-prop',
|
|
190
|
-
dataPath: dataPath + '/' + k,
|
|
215
|
+
dataPath: dataPath + '/' + encodeJSONKeyComponent(k),
|
|
191
216
|
dataType: 'key',
|
|
192
217
|
prop: k,
|
|
193
218
|
expectedProps: Object.keys(schemaNode.properties),
|
|
@@ -209,7 +234,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
209
234
|
({
|
|
210
235
|
type: 'missing-prop',
|
|
211
236
|
dataPath,
|
|
212
|
-
dataType:
|
|
237
|
+
dataType: 'value',
|
|
213
238
|
prop: k,
|
|
214
239
|
expectedProps: schemaNode.required,
|
|
215
240
|
actualProps: keys,
|
|
@@ -230,7 +255,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
230
255
|
[schemaNode.properties[k]].concat(schemaAncestors),
|
|
231
256
|
// $FlowFixMe type was already checked
|
|
232
257
|
dataNode[k],
|
|
233
|
-
dataPath + '/' + k,
|
|
258
|
+
dataPath + '/' + encodeJSONKeyComponent(k),
|
|
234
259
|
);
|
|
235
260
|
if (result) results.push(result);
|
|
236
261
|
} else {
|
|
@@ -239,7 +264,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
239
264
|
results.push({
|
|
240
265
|
type: 'enum',
|
|
241
266
|
dataType: 'key',
|
|
242
|
-
dataPath: dataPath + '/' + k,
|
|
267
|
+
dataPath: dataPath + '/' + encodeJSONKeyComponent(k),
|
|
243
268
|
expectedValues: Object.keys(
|
|
244
269
|
schemaNode.properties,
|
|
245
270
|
).filter(
|
|
@@ -256,7 +281,7 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
256
281
|
[additionalProperties].concat(schemaAncestors),
|
|
257
282
|
// $FlowFixMe type was already checked
|
|
258
283
|
dataNode[k],
|
|
259
|
-
dataPath + '/' + k,
|
|
284
|
+
dataPath + '/' + encodeJSONKeyComponent(k),
|
|
260
285
|
);
|
|
261
286
|
if (result) results.push(result);
|
|
262
287
|
}
|
|
@@ -339,9 +364,12 @@ function validateSchema(schema: SchemaEntity, data: mixed): Array<SchemaError> {
|
|
|
339
364
|
}
|
|
340
365
|
export default validateSchema;
|
|
341
366
|
|
|
342
|
-
function fuzzySearch(
|
|
367
|
+
export function fuzzySearch(
|
|
368
|
+
expectedValues: Array<string>,
|
|
369
|
+
actualValue: string,
|
|
370
|
+
): Array<string> {
|
|
343
371
|
let result = expectedValues
|
|
344
|
-
.map(exp => [exp,
|
|
372
|
+
.map(exp => [exp, levenshtein.distance(exp, actualValue)])
|
|
345
373
|
.filter(
|
|
346
374
|
// Remove if more than half of the string would need to be changed
|
|
347
375
|
([, d]) => d * 2 < actualValue.length,
|
|
@@ -350,22 +378,43 @@ function fuzzySearch(expectedValues: Array<string>, actualValue: string) {
|
|
|
350
378
|
return result.map(([v]) => v);
|
|
351
379
|
}
|
|
352
380
|
|
|
353
|
-
validateSchema.diagnostic = function(
|
|
381
|
+
validateSchema.diagnostic = function (
|
|
354
382
|
schema: SchemaEntity,
|
|
355
|
-
data:
|
|
356
|
-
|
|
357
|
-
|
|
383
|
+
data: {|
|
|
384
|
+
...
|
|
385
|
+
| {|
|
|
386
|
+
source?: ?string,
|
|
387
|
+
data?: mixed,
|
|
388
|
+
|}
|
|
389
|
+
| {|
|
|
390
|
+
source: string,
|
|
391
|
+
map: {|
|
|
392
|
+
data: mixed,
|
|
393
|
+
pointers: {|[key: string]: Mapping|},
|
|
394
|
+
|},
|
|
395
|
+
|},
|
|
396
|
+
filePath?: ?string,
|
|
397
|
+
prependKey?: ?string,
|
|
398
|
+
|},
|
|
358
399
|
origin: string,
|
|
359
|
-
prependKey: string,
|
|
360
400
|
message: string,
|
|
361
401
|
): void {
|
|
362
|
-
|
|
402
|
+
if (
|
|
403
|
+
'source' in data &&
|
|
404
|
+
'data' in data &&
|
|
405
|
+
typeof data.source !== 'string' &&
|
|
406
|
+
!data
|
|
407
|
+
) {
|
|
408
|
+
throw new Error(
|
|
409
|
+
'At least one of data.source and data.data must be defined!',
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
let object = data.map
|
|
413
|
+
? data.map.data
|
|
414
|
+
: // $FlowFixMe we can assume it's a JSON object
|
|
415
|
+
data.data ?? JSON.parse(data.source);
|
|
416
|
+
let errors = validateSchema(schema, object);
|
|
363
417
|
if (errors.length) {
|
|
364
|
-
let dataContentsString: string =
|
|
365
|
-
typeof dataContents === 'string'
|
|
366
|
-
? dataContents
|
|
367
|
-
: // $FlowFixMe
|
|
368
|
-
JSON.stringify(dataContents, null, '\t');
|
|
369
418
|
let keys = errors.map(e => {
|
|
370
419
|
let message;
|
|
371
420
|
if (e.type === 'enum') {
|
|
@@ -403,10 +452,8 @@ validateSchema.diagnostic = function(
|
|
|
403
452
|
let {prop, actualProps} = e;
|
|
404
453
|
let likely = fuzzySearch(actualProps, prop);
|
|
405
454
|
if (likely.length > 0) {
|
|
406
|
-
message = `Did you mean ${
|
|
407
|
-
|
|
408
|
-
.join(', ')}?`;
|
|
409
|
-
e.dataPath += '/' + prop;
|
|
455
|
+
message = `Did you mean ${JSON.stringify(prop)}?`;
|
|
456
|
+
e.dataPath += '/' + likely[0];
|
|
410
457
|
e.dataType = 'key';
|
|
411
458
|
} else {
|
|
412
459
|
message = `Missing property ${prop}`;
|
|
@@ -422,26 +469,36 @@ validateSchema.diagnostic = function(
|
|
|
422
469
|
}
|
|
423
470
|
return {key: e.dataPath, type: e.dataType, message};
|
|
424
471
|
});
|
|
425
|
-
let
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
472
|
+
let map, code;
|
|
473
|
+
if (data.map) {
|
|
474
|
+
map = data.map;
|
|
475
|
+
code = data.source;
|
|
476
|
+
} else {
|
|
477
|
+
// $FlowFixMe we can assume that data is valid JSON
|
|
478
|
+
map = data.source ?? JSON.stringify(nullthrows(data.data), 0, '\t');
|
|
479
|
+
code = map;
|
|
480
|
+
}
|
|
481
|
+
let codeFrames = [
|
|
482
|
+
{
|
|
483
|
+
filePath: data.filePath ?? undefined,
|
|
484
|
+
language: 'json',
|
|
485
|
+
code,
|
|
486
|
+
codeHighlights: generateJSONCodeHighlights(
|
|
487
|
+
map,
|
|
488
|
+
keys.map(({key, type, message}) => ({
|
|
489
|
+
key: (data.prependKey ?? '') + key,
|
|
490
|
+
type: type,
|
|
491
|
+
message: message != null ? escapeMarkdown(message) : message,
|
|
492
|
+
})),
|
|
493
|
+
),
|
|
494
|
+
},
|
|
495
|
+
];
|
|
436
496
|
|
|
437
497
|
throw new ThrowableDiagnostic({
|
|
438
498
|
diagnostic: {
|
|
439
|
-
message,
|
|
499
|
+
message: message,
|
|
440
500
|
origin,
|
|
441
|
-
|
|
442
|
-
filePath: dataContentsPath || undefined,
|
|
443
|
-
language: 'json',
|
|
444
|
-
codeFrame,
|
|
501
|
+
codeFrames,
|
|
445
502
|
},
|
|
446
503
|
});
|
|
447
504
|
}
|