]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview Tests for no-undef rule. | |
3 | * @author Mark Macdonald | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
8 | //------------------------------------------------------------------------------ | |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const rule = require("../../../lib/rules/no-undef"), | |
13 | { RuleTester } = require("../../../lib/rule-tester"); | |
14 | ||
15 | //------------------------------------------------------------------------------ | |
16 | // Tests | |
17 | //------------------------------------------------------------------------------ | |
18 | ||
19 | const ruleTester = new RuleTester(); | |
20 | ||
21 | ruleTester.run("no-undef", rule, { | |
22 | valid: [ | |
23 | "var a = 1, b = 2; a;", | |
24 | "/*global b*/ function f() { b; }", | |
25 | { code: "function f() { b; }", globals: { b: false } }, | |
26 | "/*global b a:false*/ a; function f() { b; a; }", | |
27 | "function a(){} a();", | |
28 | "function f(b) { b; }", | |
29 | "var a; a = 1; a++;", | |
30 | "var a; function f() { a = 1; }", | |
31 | "/*global b:true*/ b++;", | |
32 | "/*eslint-env browser*/ window;", | |
33 | "/*eslint-env node*/ require(\"a\");", | |
34 | "Object; isNaN();", | |
35 | "toString()", | |
36 | "hasOwnProperty()", | |
37 | "function evilEval(stuffToEval) { var ultimateAnswer; ultimateAnswer = 42; eval(stuffToEval); }", | |
38 | "typeof a", | |
39 | "typeof (a)", | |
40 | "var b = typeof a", | |
41 | "typeof a === 'undefined'", | |
42 | "if (typeof a === 'undefined') {}", | |
43 | { code: "function foo() { var [a, b=4] = [1, 2]; return {a, b}; }", parserOptions: { ecmaVersion: 6 } }, | |
44 | { code: "var toString = 1;", parserOptions: { ecmaVersion: 6 } }, | |
45 | { code: "function myFunc(...foo) { return foo;}", parserOptions: { ecmaVersion: 6 } }, | |
46 | { code: "var React, App, a=1; React.render(<App attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } }, | |
47 | { code: "var console; [1,2,3].forEach(obj => {\n console.log(obj);\n});", parserOptions: { ecmaVersion: 6 } }, | |
48 | { code: "var Foo; class Bar extends Foo { constructor() { super(); }}", parserOptions: { ecmaVersion: 6 } }, | |
49 | { code: "import Warning from '../lib/warning'; var warn = new Warning('text');", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, | |
50 | { code: "import * as Warning from '../lib/warning'; var warn = new Warning('text');", parserOptions: { ecmaVersion: 6, sourceType: "module" } }, | |
51 | { code: "var a; [a] = [0];", parserOptions: { ecmaVersion: 6 } }, | |
52 | { code: "var a; ({a} = {});", parserOptions: { ecmaVersion: 6 } }, | |
53 | { code: "var a; ({b: a} = {});", parserOptions: { ecmaVersion: 6 } }, | |
54 | { code: "var obj; [obj.a, obj.b] = [0, 1];", parserOptions: { ecmaVersion: 6 } }, | |
55 | { code: "URLSearchParams;", env: { browser: true } }, | |
56 | { code: "Intl;", env: { browser: true } }, | |
57 | { code: "IntersectionObserver;", env: { browser: true } }, | |
58 | { code: "Credential;", env: { browser: true } }, | |
59 | { code: "requestIdleCallback;", env: { browser: true } }, | |
60 | { code: "customElements;", env: { browser: true } }, | |
61 | { code: "PromiseRejectionEvent;", env: { browser: true } }, | |
6f036462 | 62 | { code: "(foo, bar) => { foo ||= WeakRef; bar ??= FinalizationRegistry; }", env: { es2021: true } }, |
eb39fafa DC |
63 | |
64 | // Notifications of readonly are removed: https://github.com/eslint/eslint/issues/4504 | |
65 | "/*global b:false*/ function f() { b = 1; }", | |
66 | { code: "function f() { b = 1; }", globals: { b: false } }, | |
67 | "/*global b:false*/ function f() { b++; }", | |
68 | "/*global b*/ b = 1;", | |
69 | "/*global b:false*/ var b = 1;", | |
70 | "Array = 1;", | |
71 | ||
72 | // new.target: https://github.com/eslint/eslint/issues/5420 | |
73 | { code: "class A { constructor() { new.target; } }", parserOptions: { ecmaVersion: 6 } }, | |
74 | ||
d3726936 | 75 | // Rest property |
eb39fafa DC |
76 | { |
77 | code: "var {bacon, ...others} = stuff; foo(others)", | |
78 | parserOptions: { | |
79 | ecmaVersion: 2018 | |
80 | }, | |
81 | globals: { stuff: false, foo: false } | |
d3726936 TL |
82 | }, |
83 | ||
84 | // export * as ns from "source" | |
85 | { | |
86 | code: 'export * as ns from "source"', | |
87 | parserOptions: { ecmaVersion: 2020, sourceType: "module" } | |
88 | }, | |
89 | ||
90 | // import.meta | |
91 | { | |
92 | code: "import.meta", | |
93 | parserOptions: { ecmaVersion: 2020, sourceType: "module" } | |
eb39fafa DC |
94 | } |
95 | ], | |
96 | invalid: [ | |
97 | { code: "a = 1;", errors: [{ messageId: "undef", data: { name: "a" }, type: "Identifier" }] }, | |
98 | { code: "if (typeof anUndefinedVar === 'string') {}", options: [{ typeof: true }], errors: [{ messageId: "undef", data: { name: "anUndefinedVar" }, type: "Identifier" }] }, | |
99 | { code: "var a = b;", errors: [{ messageId: "undef", data: { name: "b" }, type: "Identifier" }] }, | |
100 | { code: "function f() { b; }", errors: [{ messageId: "undef", data: { name: "b" }, type: "Identifier" }] }, | |
101 | { code: "window;", errors: [{ messageId: "undef", data: { name: "window" }, type: "Identifier" }] }, | |
102 | { code: "require(\"a\");", errors: [{ messageId: "undef", data: { name: "require" }, type: "Identifier" }] }, | |
103 | { code: "var React; React.render(<img attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, errors: [{ messageId: "undef", data: { name: "a" } }] }, | |
104 | { code: "var React, App; React.render(<App attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, errors: [{ messageId: "undef", data: { name: "a" } }] }, | |
105 | { code: "[a] = [0];", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] }, | |
106 | { code: "({a} = {});", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] }, | |
107 | { code: "({b: a} = {});", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] }, | |
108 | { code: "[obj.a, obj.b] = [0, 1];", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "obj" } }, { messageId: "undef", data: { name: "obj" } }] }, | |
109 | ||
110 | // Experimental | |
111 | { | |
112 | code: "const c = 0; const a = {...b, c};", | |
113 | parserOptions: { | |
114 | ecmaVersion: 2018 | |
115 | }, | |
116 | errors: [{ messageId: "undef", data: { name: "b" } }] | |
117 | } | |
118 | ] | |
119 | }); |