Skip to content

Commit

Permalink
no-native rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamund Ferguson committed May 25, 2016
1 parent 7b96139 commit 04c7005
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ new Promise(function (ok, fail) { ... }) // non-standard parameter names

Ensures that `new Promise()` is instantiated with the parameter names `resolve, reject` to avoid confusion with order such as `reject, resolve`. The Promise constructor uses the [RevealingConstructor pattern](https://blog.domenic.me/the-revealing-constructor-pattern/). Using the same parameter names as the language specification makes code more uniform and easier to understand.

### `no-native`

Ensure that `Promise` is included fresh in each file instead of relying
on the existence of a native promise implementation. Helpful if you want
to use `bluebird` or if you don't intend to use an ES6 Promise shim.


#### Valid
```js
var Promise = require("blubird");
var x = Promise.resolve("good");
```

#### Invalid
```js
var x = Promise.resolve("bad");
```

## Installation

Expand Down Expand Up @@ -127,6 +144,7 @@ Then configure the rules you want to use under the rules section.
"promise/always-return": 2,
"promise/always-catch": 2, // deprecated
"promise/catch-or-return": 2,
"promise/no-native": 0,
}
}
```
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
'param-names': 1,
'always-return': 1,
'always-catch': 1,
'no-native': 0,
'catch-or-return': 1
}
}
45 changes: 45 additions & 0 deletions rules/no-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Borrowed from here:
// https://github.com/colonyamerican/eslint-plugin-cah/issues/3

'use strict'

module.exports = function (context) {
var MESSAGE = '"{{name}}" is not defined.'

/**
* Checks for and reports reassigned constants
*
* @param {Scope} scope - an escope Scope object
* @returns {void}
* @private
*/
function isDeclared (scope, ref) {
return scope.variables.some(function (variable) {
if (variable.name !== ref.identifier.name) {
return false
}

if (!variable.defs || !variable.defs.length) {
return false
}

return true
})
}

return {
'Program:exit': function () {
var scope = context.getScope()

scope.through.forEach(function (ref) {
if (ref.identifier.name !== 'Promise') {
return
}

if (!isDeclared(scope, ref)) {
context.report(ref.identifier, MESSAGE, { name: ref.identifier.name })
}
})
}
}
}
23 changes: 23 additions & 0 deletions test/no-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict'

var rule = require('../rules/no-native')
var RuleTester = require('eslint').RuleTester

var ruleTester = new RuleTester()
ruleTester.run('no-native', rule, {
valid: [
'var Promise = null; function x() { return Promise.resolve("hi"); }',
'var Promise = window.Promise || require("bluebird"); var x = Promise.reject();'
],

invalid: [
{
code: 'new Promise(function(reject, resolve) { })',
errors: [ { message: '"Promise" is not defined.' } ]
},
{
code: 'Promise.resolve()',
errors: [ { message: '"Promise" is not defined.' } ]
}
]
})

0 comments on commit 04c7005

Please sign in to comment.