• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if ((!common.hasCrypto) || (!common.hasIntl)) {
5  common.skip('ESLint tests require crypto and Intl');
6}
7
8common.skipIfEslintMissing();
9
10const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
11const rule = require('../../tools/eslint-rules/prefer-primordials');
12
13new RuleTester({
14  parserOptions: { ecmaVersion: 6 },
15  env: { es6: true }
16})
17  .run('prefer-primordials', rule, {
18    valid: [
19      'new Array()',
20      'JSON.stringify({})',
21      'class A { *[Symbol.iterator] () { yield "a"; } }',
22      'const a = { *[Symbol.iterator] () { yield "a"; } }',
23      'Object.defineProperty(o, Symbol.toStringTag, { value: "o" })',
24      'parseInt("10")',
25      `
26        const { Reflect } = primordials;
27        module.exports = function() {
28          const { ownKeys } = Reflect;
29        }
30      `,
31      {
32        code: 'const { Array } = primordials; new Array()',
33        options: [{ name: 'Array' }]
34      },
35      {
36        code: 'const { JSONStringify } = primordials; JSONStringify({})',
37        options: [{ name: 'JSON' }]
38      },
39      {
40        code: 'const { SymbolFor } = primordials; SymbolFor("xxx")',
41        options: [{ name: 'Symbol' }]
42      },
43      {
44        code: `
45          const { SymbolIterator } = primordials;
46          class A { *[SymbolIterator] () { yield "a"; } }
47        `,
48        options: [{ name: 'Symbol' }]
49      },
50      {
51        code: `
52          const { Symbol } = primordials;
53          const a = { *[Symbol.iterator] () { yield "a"; } }
54        `,
55        options: [{ name: 'Symbol', ignore: ['iterator'] }]
56      },
57      {
58        code: `
59          const { ObjectDefineProperty, Symbol } = primordials;
60          ObjectDefineProperty(o, Symbol.toStringTag, { value: "o" });
61          const val = Symbol.toStringTag;
62          const { toStringTag } = Symbol;
63        `,
64        options: [{ name: 'Symbol', ignore: ['toStringTag'] }]
65      },
66      {
67        code: 'const { Symbol } = primordials; Symbol.for("xxx")',
68        options: [{ name: 'Symbol', ignore: ['for'] }]
69      },
70      {
71        code: 'const { NumberParseInt } = primordials; NumberParseInt("xxx")',
72        options: [{ name: 'parseInt', into: 'Number' }]
73      },
74      {
75        code: `
76          const { ReflectOwnKeys } = primordials;
77          module.exports = function() {
78            ReflectOwnKeys({})
79          }
80        `,
81        options: [{ name: 'Reflect' }],
82      },
83      {
84        code: 'const { Map } = primordials; new Map()',
85        options: [{ name: 'Map', into: 'Safe' }],
86      },
87      {
88        code: `
89          const { Function } = primordials;
90          const rename = Function;
91          const obj = { rename };
92        `,
93        options: [{ name: 'Function' }],
94      },
95      {
96        code: `
97          const { Function } = primordials;
98          let rename;
99          rename = Function;
100          const obj = { rename };
101        `,
102        options: [{ name: 'Function' }],
103      },
104      {
105        code: 'function identifier() {}',
106        options: [{ name: 'identifier' }]
107      },
108      {
109        code: 'function* identifier() {}',
110        options: [{ name: 'identifier' }]
111      },
112      {
113        code: 'class identifier {}',
114        options: [{ name: 'identifier' }]
115      },
116      {
117        code: 'new class { identifier(){} }',
118        options: [{ name: 'identifier' }]
119      },
120      {
121        code: 'const a = { identifier: \'4\' }',
122        options: [{ name: 'identifier' }]
123      },
124      {
125        code: 'identifier:{const a = 4}',
126        options: [{ name: 'identifier' }]
127      },
128      {
129        code: 'switch(0){case identifier:}',
130        options: [{ name: 'identifier' }]
131      },
132    ],
133    invalid: [
134      {
135        code: 'new Array()',
136        options: [{ name: 'Array' }],
137        errors: [{ message: /const { Array } = primordials/ }]
138      },
139      {
140        code: 'JSON.parse("{}")',
141        options: [{ name: 'JSON' }],
142        errors: [
143          { message: /const { JSONParse } = primordials/ },
144        ]
145      },
146      {
147        code: 'const { JSON } = primordials; JSON.parse("{}")',
148        options: [{ name: 'JSON' }],
149        errors: [{ message: /const { JSONParse } = primordials/ }]
150      },
151      {
152        code: 'Symbol.for("xxx")',
153        options: [{ name: 'Symbol' }],
154        errors: [
155          { message: /const { SymbolFor } = primordials/ },
156        ]
157      },
158      {
159        code: 'const { Symbol } = primordials; Symbol.for("xxx")',
160        options: [{ name: 'Symbol' }],
161        errors: [{ message: /const { SymbolFor } = primordials/ }]
162      },
163      {
164        code: `
165          const { Symbol } = primordials;
166          class A { *[Symbol.iterator] () { yield "a"; } }
167        `,
168        options: [{ name: 'Symbol' }],
169        errors: [{ message: /const { SymbolIterator } = primordials/ }]
170      },
171      {
172        code: `
173          const { Symbol } = primordials;
174          const a = { *[Symbol.iterator] () { yield "a"; } }
175        `,
176        options: [{ name: 'Symbol' }],
177        errors: [{ message: /const { SymbolIterator } = primordials/ }]
178      },
179      {
180        code: `
181          const { ObjectDefineProperty, Symbol } = primordials;
182          ObjectDefineProperty(o, Symbol.toStringTag, { value: "o" })
183        `,
184        options: [{ name: 'Symbol' }],
185        errors: [{ message: /const { SymbolToStringTag } = primordials/ }]
186      },
187      {
188        code: `
189          const { Number } = primordials;
190          Number.parseInt('10')
191        `,
192        options: [{ name: 'Number' }],
193        errors: [{ message: /const { NumberParseInt } = primordials/ }]
194      },
195      {
196        code: 'parseInt("10")',
197        options: [{ name: 'parseInt', into: 'Number' }],
198        errors: [{ message: /const { NumberParseInt } = primordials/ }]
199      },
200      {
201        code: `
202          module.exports = function() {
203            const { ownKeys } = Reflect;
204          }
205        `,
206        options: [{ name: 'Reflect' }],
207        errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
208      },
209      {
210        code: `
211          const { Reflect } = primordials;
212          module.exports = function() {
213            const { ownKeys } = Reflect;
214          }
215        `,
216        options: [{ name: 'Reflect' }],
217        errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
218      },
219      {
220        code: 'new Map()',
221        options: [{ name: 'Map', into: 'Safe' }],
222        errors: [{ message: /const { SafeMap } = primordials/ }]
223      },
224      {
225        code: `
226          const { Function } = primordials;
227          const noop = Function.prototype;
228        `,
229        options: [{ name: 'Function' }],
230        errors: [{ message: /const { FunctionPrototype } = primordials/ }]
231      },
232      {
233        code: `
234          const obj = { Function };
235        `,
236        options: [{ name: 'Function' }],
237        errors: [{ message: /const { Function } = primordials/ }]
238      },
239      {
240        code: `
241          const rename = Function;
242        `,
243        options: [{ name: 'Function' }],
244        errors: [{ message: /const { Function } = primordials/ }]
245      },
246      {
247        code: `
248          const rename = Function;
249          const obj = { rename };
250        `,
251        options: [{ name: 'Function' }],
252        errors: [{ message: /const { Function } = primordials/ }]
253      },
254      {
255        code: `
256          let rename;
257          rename = Function;
258          const obj = { rename };
259        `,
260        options: [{ name: 'Function' }],
261        errors: [{ message: /const { Function } = primordials/ }]
262      },
263    ]
264  });
265