]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | /** |
2 | * @fileoverview A rule to disallow calls to the Object constructor | |
3 | * @author Matt DuVall <http://www.mattduvall.com/> | |
4 | */ | |
5 | ||
6 | "use strict"; | |
7 | ||
56c4a2cb DC |
8 | //------------------------------------------------------------------------------ |
9 | // Requirements | |
10 | //------------------------------------------------------------------------------ | |
11 | ||
12 | const astUtils = require("./utils/ast-utils"); | |
13 | ||
eb39fafa DC |
14 | //------------------------------------------------------------------------------ |
15 | // Rule Definition | |
16 | //------------------------------------------------------------------------------ | |
17 | ||
34eeec05 | 18 | /** @type {import('../shared/types').Rule} */ |
eb39fafa DC |
19 | module.exports = { |
20 | meta: { | |
21 | type: "suggestion", | |
22 | ||
23 | docs: { | |
8f9d1d4d | 24 | description: "Disallow `Object` constructors", |
eb39fafa | 25 | recommended: false, |
f2a92ac6 | 26 | url: "https://eslint.org/docs/latest/rules/no-new-object" |
eb39fafa DC |
27 | }, |
28 | ||
29 | schema: [], | |
30 | ||
31 | messages: { | |
8f9d1d4d | 32 | preferLiteral: "The object literal notation {} is preferable." |
eb39fafa DC |
33 | } |
34 | }, | |
35 | ||
36 | create(context) { | |
f2a92ac6 DC |
37 | |
38 | const sourceCode = context.sourceCode; | |
39 | ||
eb39fafa | 40 | return { |
eb39fafa | 41 | NewExpression(node) { |
56c4a2cb | 42 | const variable = astUtils.getVariableByName( |
f2a92ac6 | 43 | sourceCode.getScope(node), |
56c4a2cb DC |
44 | node.callee.name |
45 | ); | |
46 | ||
47 | if (variable && variable.identifiers.length > 0) { | |
48 | return; | |
49 | } | |
50 | ||
eb39fafa DC |
51 | if (node.callee.name === "Object") { |
52 | context.report({ | |
53 | node, | |
54 | messageId: "preferLiteral" | |
55 | }); | |
56 | } | |
57 | } | |
58 | }; | |
eb39fafa DC |
59 | } |
60 | }; |