]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-global-assign.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / no-global-assign.js
1 /**
2 * @fileoverview Tests for no-global-assign rule.
3 * @author Ilya Volodin
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-global-assign"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-global-assign", rule, {
22 valid: [
23 "string = 'hello world';",
24 "var string;",
25 { code: "Object = 0;", options: [{ exceptions: ["Object"] }] },
26 "top = 0;",
27 { code: "onload = 0;", env: { browser: true } },
28 "require = 0;",
29 { code: "a = 1", globals: { a: true } },
30 "/*global a:true*/ a = 1"
31 ],
32 invalid: [
33 {
34 code: "String = 'hello world';",
35 errors: [{
36 messageId: "globalShouldNotBeModified",
37 data: { name: "String" },
38 type: "Identifier"
39 }]
40 },
41 {
42 code: "String++;",
43 errors: [{
44 messageId: "globalShouldNotBeModified",
45 data: { name: "String" },
46 type: "Identifier"
47 }]
48 },
49 {
50 code: "({Object = 0, String = 0} = {});",
51 parserOptions: { ecmaVersion: 6 },
52 errors: [
53 {
54 messageId: "globalShouldNotBeModified",
55 data: { name: "Object" },
56 type: "Identifier"
57 },
58 {
59 messageId: "globalShouldNotBeModified",
60 data: { name: "String" },
61 type: "Identifier"
62 }
63 ]
64 },
65 {
66 code: "top = 0;",
67 env: { browser: true },
68 errors: [{
69 messageId: "globalShouldNotBeModified",
70 data: { name: "top" },
71 type: "Identifier"
72 }]
73 },
74 {
75 code: "require = 0;",
76 env: { node: true },
77 errors: [{
78 messageId: "globalShouldNotBeModified",
79 data: { name: "require" },
80 type: "Identifier"
81 }]
82 },
83
84 // Notifications of readonly are moved from no-undef: https://github.com/eslint/eslint/issues/4504
85 {
86 code: "/*global b:false*/ function f() { b = 1; }",
87 errors: [{
88 messageId: "globalShouldNotBeModified",
89 data: { name: "b" },
90 type: "Identifier"
91 }]
92 },
93 {
94 code: "function f() { b = 1; }",
95 globals: { b: false },
96 errors: [{
97 messageId: "globalShouldNotBeModified",
98 data: { name: "b" },
99 type: "Identifier"
100 }]
101 },
102 {
103 code: "/*global b:false*/ function f() { b++; }",
104 errors: [{
105 messageId: "globalShouldNotBeModified",
106 data: { name: "b" },
107 type: "Identifier"
108 }]
109 },
110 {
111 code: "/*global b*/ b = 1;",
112 errors: [{
113 messageId: "globalShouldNotBeModified",
114 data: { name: "b" },
115 type: "Identifier"
116 }]
117 },
118 {
119 code: "Array = 1;",
120 errors: [{
121 messageId: "globalShouldNotBeModified",
122 data: { name: "Array" },
123 type: "Identifier"
124 }]
125 }
126 ]
127 });