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
|
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------
var rule = require("../lib/rules/no-cu-reportError");
var RuleTester = require("eslint").RuleTester;
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 9 } });
// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------
function callError() {
return [{ messageId: "useConsoleError", type: "CallExpression" }];
}
ruleTester.run("no-cu-reportError", rule, {
valid: [
"console.error('foo')",
"Cu.cloneInto({}, {})",
"foo().catch(console.error)",
// TODO: Bug 1802347 - this should be treated as an error as well.
"Cu.reportError('foo', stack)",
],
invalid: [
{
code: "Cu.reportError('foo')",
output: "console.error('foo')",
errors: callError(),
},
{
code: "Cu.reportError(bar)",
output: "console.error(bar)",
errors: callError(),
},
{
code: "foo().catch(Cu.reportError)",
output: "foo().catch(console.error)",
errors: callError(),
},
// When referencing identifiers/members, try to reference them rather
// than stringifying:
{
code: "Cu.reportError('foo' + e)",
output: "console.error('foo', e)",
errors: callError(),
},
{
code: "Cu.reportError('foo' + msg.data)",
output: "console.error('foo', msg.data)",
errors: callError(),
},
// Don't touch existing concatenation of literals (usually done for
// wrapping reasons)
{
code: "Cu.reportError('foo' + 'bar' + 'baz')",
output: "console.error('foo' + 'bar' + 'baz')",
errors: callError(),
},
// Ensure things work when nested:
{
code: "Cu.reportError('foo' + e + 'baz')",
output: "console.error('foo', e, 'baz')",
errors: callError(),
},
// Ensure things work when nested in different places:
{
code: "Cu.reportError('foo' + e + 'quux' + 'baz')",
output: "console.error('foo', e, 'quux' + 'baz')",
errors: callError(),
},
{
code: "Cu.reportError('foo' + 'quux' + e + 'baz')",
output: "console.error('foo' + 'quux', e, 'baz')",
errors: callError(),
},
// Ensure things work with parens changing order of operations:
{
code: "Cu.reportError('foo' + 'quux' + (e + 'baz'))",
output: "console.error('foo' + 'quux' + (e + 'baz'))",
errors: callError(),
},
],
});
|