@putout/plugin-new
🐊Putout plugin adds ability to remove useless and add missing operator 'new'
Last updated 3 months ago by coderaiser .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @putout/plugin-new 
SYNC missed versions from official npm registry.

@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 went wrong');
new new Boolean()();

βœ… Example of correct code

Error('Something went 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

Linter Rule Fix
???? Putout remove-useless-new βœ…
⏣ ESLint no-new-wrappers ❌
β € no-new-object ❌
β € no-array-constructor ❌
β € no-new-symbol ❌
β € no-new-native-constructor ❌

License

MIT

Current Tags

  • 5.1.0                                ...           latest (3 months ago)

13 Versions

  • 5.1.0                                ...           3 months ago
  • 5.0.0                                ...           3 months ago
  • 4.0.1                                ...           3 months ago
  • 4.0.0                                ...           a year ago
  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.1.0                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (6)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |