3.0.1 β€’ Published 6 months ago

@putout/plugin-new v3.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@putout/plugin-new NPM version

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

(c) MDN

🐊Putout plugin adds ability to add missing and remove useless operator new.

Install

npm i @putout/plugin-new

Rule

{
    "rules": {
        "new/remove-useless": "on",
        "new/add-missing": "on"
    }
}

remove-useless

Operator new has no sense for Boolean, String, Number, Object, RegExp, Math, Reflect, Error, TypeError:

Thus the function call Error(…) is equivalent to the object creation expression new Error(…) with the same arguments.

(c) https://262.ecma-international.org/12.0/#sec-error-constructor

And Symbol, BigInt cannot be used with new, as it is primitive.

❌ Example of incorrect code

new Error('Something whent wrong');
new new Boolean()();

βœ… Example of correct code

Error('Something whent wrong');
Boolean();

add-missing

The Set constructor lets you create Set objects that store unique values of any type, whether primitive values or object references.

(c) MDN

Missing operator new should be added since built-in objects:

  • Set;
  • WeakSet;
  • Map;
  • WeakMap;
  • Int8Array;
  • Uint8Array;
  • Uint8ClampedArray;
  • Int16Array;
  • Uint16Array;
  • Int32Array;
  • Uint32Array;
  • Float32Array;
  • Float64Array;
  • BigInt64Array;
  • BigUint64Array;

Produces TypeError when called without new:

Uncaught TypeError: Constructor Set requires 'new'

❌ Example of incorrect code

const map = Map();

βœ… Example of correct code

const map = new Map();

Comparison

LinterRuleFix
🐊 Putoutremove-useless-newβœ…
⏣ ESLintno-new-wrappers❌
β €no-new-object❌
β €no-array-constructor❌
β €no-new-symbol❌
β €no-new-native-constructor❌

License

MIT

3.0.1

6 months ago

3.0.0

6 months ago

2.1.0

11 months ago

2.0.0

12 months ago

1.1.0

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago