• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4if (!common.hasCrypto)
5  common.skip('missing crypto');
6
7common.skipIfEslintMissing();
8
9const RuleTester = require('../../tools/node_modules/eslint').RuleTester;
10const rule = require('../../tools/eslint-rules/prefer-primordials');
11
12new RuleTester({
13  parserOptions: { ecmaVersion: 6 },
14  env: { es6: true }
15})
16  .run('prefer-primordials', rule, {
17    valid: [
18      'new Array()',
19      'JSON.stringify({})',
20      'class A { *[Symbol.iterator] () { yield "a"; } }',
21      'const a = { *[Symbol.iterator] () { yield "a"; } }',
22      'Object.defineProperty(o, Symbol.toStringTag, { value: "o" })',
23      'parseInt("10")',
24      `
25        const { Reflect } = primordials;
26        module.exports = function() {
27          const { ownKeys } = Reflect;
28        }
29      `,
30      {
31        code: 'const { Array } = primordials; new Array()',
32        options: [{ name: 'Array' }]
33      },
34      {
35        code: 'const { JSONStringify } = primordials; JSONStringify({})',
36        options: [{ name: 'JSON' }]
37      },
38      {
39        code: 'const { SymbolFor } = primordials; SymbolFor("xxx")',
40        options: [{ name: 'Symbol' }]
41      },
42      {
43        code: `
44          const { SymbolIterator } = primordials;
45          class A { *[SymbolIterator] () { yield "a"; } }
46        `,
47        options: [{ name: 'Symbol' }]
48      },
49      {
50        code: `
51          const { Symbol } = primordials;
52          const a = { *[Symbol.iterator] () { yield "a"; } }
53        `,
54        options: [{ name: 'Symbol', ignore: ['iterator'] }]
55      },
56      {
57        code: `
58          const { ObjectDefineProperty, Symbol } = primordials;
59          ObjectDefineProperty(o, Symbol.toStringTag, { value: "o" })
60        `,
61        options: [{ name: 'Symbol', ignore: ['toStringTag'] }]
62      },
63      {
64        code: 'const { Symbol } = primordials; Symbol.for("xxx")',
65        options: [{ name: 'Symbol', ignore: ['for'] }]
66      },
67      {
68        code: 'const { NumberParseInt } = primordials; NumberParseInt("xxx")',
69        options: [{ name: 'parseInt', into: 'Number' }]
70      },
71      {
72        code: `
73          const { ReflectOwnKeys } = primordials;
74          module.exports = function() {
75            ReflectOwnKeys({})
76          }
77        `,
78        options: [{ name: 'Reflect' }],
79      },
80      {
81        code: 'const { Map } = primordials; new Map()',
82        options: [{ name: 'Map', into: 'Safe' }],
83      },
84    ],
85    invalid: [
86      {
87        code: 'new Array()',
88        options: [{ name: 'Array' }],
89        errors: [{ message: /const { Array } = primordials/ }]
90      },
91      {
92        code: 'JSON.parse("{}")',
93        options: [{ name: 'JSON' }],
94        errors: [
95          { message: /const { JSONParse } = primordials/ },
96        ]
97      },
98      {
99        code: 'const { JSON } = primordials; JSON.parse("{}")',
100        options: [{ name: 'JSON' }],
101        errors: [{ message: /const { JSONParse } = primordials/ }]
102      },
103      {
104        code: 'Symbol.for("xxx")',
105        options: [{ name: 'Symbol' }],
106        errors: [
107          { message: /const { SymbolFor } = primordials/ },
108        ]
109      },
110      {
111        code: 'const { Symbol } = primordials; Symbol.for("xxx")',
112        options: [{ name: 'Symbol' }],
113        errors: [{ message: /const { SymbolFor } = primordials/ }]
114      },
115      {
116        code: `
117          const { Symbol } = primordials;
118          class A { *[Symbol.iterator] () { yield "a"; } }
119        `,
120        options: [{ name: 'Symbol' }],
121        errors: [{ message: /const { SymbolIterator } = primordials/ }]
122      },
123      {
124        code: `
125          const { Symbol } = primordials;
126          const a = { *[Symbol.iterator] () { yield "a"; } }
127        `,
128        options: [{ name: 'Symbol' }],
129        errors: [{ message: /const { SymbolIterator } = primordials/ }]
130      },
131      {
132        code: `
133          const { ObjectDefineProperty, Symbol } = primordials;
134          ObjectDefineProperty(o, Symbol.toStringTag, { value: "o" })
135        `,
136        options: [{ name: 'Symbol' }],
137        errors: [{ message: /const { SymbolToStringTag } = primordials/ }]
138      },
139      {
140        code: `
141          const { Number } = primordials;
142          Number.parseInt('10')
143        `,
144        options: [{ name: 'Number' }],
145        errors: [{ message: /const { NumberParseInt } = primordials/ }]
146      },
147      {
148        code: 'parseInt("10")',
149        options: [{ name: 'parseInt', into: 'Number' }],
150        errors: [{ message: /const { NumberParseInt } = primordials/ }]
151      },
152      {
153        code: `
154          module.exports = function() {
155            const { ownKeys } = Reflect;
156          }
157        `,
158        options: [{ name: 'Reflect' }],
159        errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
160      },
161      {
162        code: 'new Map()',
163        options: [{ name: 'Map', into: 'Safe' }],
164        errors: [{ message: /const { SafeMap } = primordials/ }]
165      },
166      {
167        code: `
168          const { Function } = primordials;
169          const noop = Function.prototype;
170        `,
171        options: [{ name: 'Function' }],
172        errors: [{ message: /const { FunctionPrototype } = primordials/ }]
173      },
174    ]
175  });
176