-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
transform-error-messages.js
160 lines (142 loc) · 3.84 KB
/
transform-error-messages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable quotes */
'use strict';
let babel = require('@babel/core');
let devExpressionWithCodes = require('../transform-error-messages');
function transform(input, options = {}) {
return babel.transform(input, {
plugins: [[devExpressionWithCodes, options]],
}).code;
}
let oldEnv;
describe('error transform', () => {
beforeEach(() => {
oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = '';
});
afterEach(() => {
process.env.NODE_ENV = oldEnv;
});
it('should replace error constructors', () => {
expect(
transform(`
new Error('Do not override existing functions.');
`)
).toMatchSnapshot();
});
it('should replace error constructors (no new)', () => {
expect(
transform(`
Error('Do not override existing functions.');
`)
).toMatchSnapshot();
});
it("should output FIXME for errors that don't have a matching error code", () => {
expect(
transform(`
Error('This is not a real error message.');
`)
).toMatchSnapshot();
});
it(
"should output FIXME for errors that don't have a matching error " +
'code, unless opted out with a comment',
() => {
// TODO: Since this only detects one of many ways to disable a lint
// rule, we should instead search for a custom directive (like
// no-minify-errors) instead of ESLint. Will need to update our lint
// rule to recognize the same directive.
expect(
transform(`
// eslint-disable-next-line react-internal/prod-error-codes
Error('This is not a real error message.');
`)
).toMatchSnapshot();
}
);
it('should not touch other calls or new expressions', () => {
expect(
transform(`
new NotAnError();
NotAnError();
`)
).toMatchSnapshot();
});
it('should support interpolating arguments with template strings', () => {
expect(
transform(`
new Error(\`Expected \${foo} target to be an array; got \${bar}\`);
`)
).toMatchSnapshot();
});
it('should support interpolating arguments with concatenation', () => {
expect(
transform(`
new Error('Expected ' + foo + ' target to be an array; got ' + bar);
`)
).toMatchSnapshot();
});
it('should support error constructors with concatenated messages', () => {
expect(
transform(`
new Error(\`Expected \${foo} target to \` + \`be an array; got \${bar}\`);
`)
).toMatchSnapshot();
});
it('handles escaped backticks in template string', () => {
expect(
transform(`
new Error(\`Expected \\\`\$\{listener\}\\\` listener to be a function, instead got a value of \\\`\$\{type\}\\\` type.\`);
`)
).toMatchSnapshot();
});
it('handles ignoring errors that are comment-excluded inside ternary expressions', () => {
expect(
transform(`
let val = someBool
? //eslint-disable-next-line react-internal/prod-error-codes
new Error('foo')
: someOtherBool
? new Error('bar')
: //eslint-disable-next-line react-internal/prod-error-codes
new Error('baz');
`)
).toMatchSnapshot();
});
it('handles ignoring errors that are comment-excluded outside ternary expressions', () => {
expect(
transform(`
//eslint-disable-next-line react-internal/prod-error-codes
let val = someBool
? new Error('foo')
: someOtherBool
? new Error('bar')
: new Error('baz');
`)
).toMatchSnapshot();
});
it('handles deeply nested expressions', () => {
expect(
transform(`
let val =
(a,
(b,
// eslint-disable-next-line react-internal/prod-error-codes
new Error('foo')));
`)
).toMatchSnapshot();
expect(
transform(`
let val =
(a,
// eslint-disable-next-line react-internal/prod-error-codes
(b, new Error('foo')));
`)
).toMatchSnapshot();
});
});