eslint-ast-utils
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -6,6 +6,10 @@ 'use strict'; | ||
module.exports = { | ||
computeStaticExpression: require('./lib/compute-static-expression'), | ||
containsIdentifier: containsIdentifier.containsIdentifier, | ||
getPropertyName: require('./lib/get-property-name'), | ||
getRequireSource: require('./lib/get-require-source'), | ||
isFunctionExpression: require('./lib/is-function-expression'), | ||
isPromise: require('./lib/is-promise'), | ||
isStaticRequire: require('./lib/is-static-require'), | ||
someContainIdentifier: containsIdentifier.someContainIdentifier | ||
}; |
@@ -177,3 +177,4 @@ 'use strict'; | ||
return containsIdentifier(name, node.block) || | ||
containsIdentifier(name, node.handler); | ||
containsIdentifier(name, node.handler) || | ||
containsIdentifier(name, node.finalizer); | ||
case 'CatchClause': | ||
@@ -180,0 +181,0 @@ return !introduces(name, node.param) && containsIdentifier(name, node.body); |
{ | ||
"name": "eslint-ast-utils", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Utility library to manipulate ASTs", | ||
@@ -29,3 +29,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"lodash.get": "^4.4.2" | ||
"lodash.get": "^4.4.2", | ||
"lodash.zip": "^4.2.0" | ||
}, | ||
@@ -46,4 +47,9 @@ "devDependencies": { | ||
"text" | ||
] | ||
], | ||
"check-coverage": true, | ||
"lines": 100, | ||
"statements": 100, | ||
"functions": 100, | ||
"branches": 100 | ||
} | ||
} |
188
readme.md
@@ -26,3 +26,3 @@ # eslint-ast-utils [![Build Status](https://travis-ci.org/jfmengels/eslint-ast-utils.svg?branch=master)](https://travis-ci.org/jfmengels/eslint-ast-utils) | ||
Returns `true` if and only if: | ||
- node is a `CallExpression` | ||
- `node` is a `CallExpression` | ||
- `node`'s callee is an `Identifier` named `require` | ||
@@ -41,3 +41,3 @@ - `node` has exactly 1 `Literal` argument whose value is a `string` | ||
Usage example (in the context of an ESLint rule): | ||
Usage example: | ||
```js | ||
@@ -70,3 +70,3 @@ function create(context) { | ||
Usage example (in the context of an ESLint rule): | ||
Usage example: | ||
```js | ||
@@ -110,3 +110,3 @@ function create(context) { | ||
Usage example (in the context of an ESLint rule): | ||
Usage example: | ||
```js | ||
@@ -140,4 +140,184 @@ function create(context) { | ||
### astUtils.getPropertyName(node) | ||
Get the name of a `MemberExpression`'s property. Returns: | ||
- a `string` if the property is accessed through dot notation. | ||
- a `string` if the property is accessed through brackets and is a string. | ||
- a `number` if the property is accessed through brackets and is a number. | ||
- `undefined` if `node` is not a `MemberExpression` | ||
- `undefined` if the property name is a hard to compute expression. | ||
Example: | ||
```js | ||
foo.bar | ||
// => 'bar' | ||
foo['bar'] | ||
// => 'bar' | ||
foo[bar] | ||
// => undefined | ||
foo[0] | ||
// => 0 # Number | ||
foo[null] | ||
// => null | ||
foo[undefined] | ||
// => undefined | ||
``` | ||
Usage example: | ||
```js | ||
function create(context) { | ||
return { | ||
MemberExpression(node) { | ||
if (astUtils.getPropertyName(node).startsWith('_')) { | ||
context.report({ | ||
node: node, | ||
message: 'Don\'t access "private" fields' | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
``` | ||
### astUtils.computeStaticExpression(node) | ||
Get the value of an expression that can be statically computed, i.e. without variables references or expressions too complex. | ||
Returns: | ||
- `undefined` if the value could not be statically computed. | ||
- An object with a `value` property containing the computed value. | ||
Example: | ||
```js | ||
foo | ||
// => undefined | ||
42 | ||
// => {value: 42} | ||
'foo' | ||
// => {value: 'foo'} | ||
undefined | ||
// => {value: undefined} | ||
null | ||
// => {value: null} | ||
1 + 2 - 4 + (-1) | ||
// => {value: -2} | ||
true ? 1 : 2 | ||
// => {value: 1} | ||
`foo ${'bar'}` | ||
// => {value: 'foo bar'} | ||
``` | ||
Usage example: | ||
```js | ||
function create(context) { | ||
return { | ||
TemplateLiteral(node) { | ||
const expression = astUtils.computeStaticExpression(node); | ||
if (expression) { | ||
context.report({ | ||
node: node, | ||
message: `You can replace this template literal by the regular string '${expression.value}'.` | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
``` | ||
### astUtils.isPromise(node) | ||
Checks whether `node` is a Promise. | ||
Returns `true` if and only if `node` is one of the following: | ||
- a call of an expression's `then` or `catch` properties | ||
- a call to a property of `Promise` (except `cancel`, `promisify`, `promisifyAll` and `is`) | ||
- a call to `new Promise` | ||
If `node` uses unknown properties of a value that would be considered a Promise, `node` itself would not be considered as a Promise. | ||
Example: | ||
```js | ||
foo.then(fn); | ||
// => true | ||
foo.catch(fn); | ||
// => true | ||
foo.then(fn).catch(fn); | ||
// => true | ||
foo.then(fn).isFulfilled(fn); // isFulfilled(fn) may not return a Promise | ||
// => false | ||
Promise.resolve(value); | ||
// => true | ||
Promise.reject(value); | ||
// => true | ||
Promise.race(promises); | ||
// => true | ||
Promise.all(promises); | ||
// => true | ||
Promise.map(promises, fn); // Bluebird method | ||
// => true | ||
new Promise(fn); | ||
// => true | ||
new Promise.resolve(value); | ||
// => false | ||
``` | ||
Usage example: | ||
```js | ||
function create(context) { | ||
function reportIfPromise(node) { | ||
if (astUtils.isPromise(node)) { | ||
context.report({ | ||
node: node, | ||
message: 'Prefer using async/await' | ||
}); | ||
} | ||
} | ||
return { | ||
CallExpression: reportIfPromise, | ||
NewExpression: reportIfPromise | ||
}; | ||
} | ||
``` | ||
### astUtils.isFunctionExpression(node) | ||
Checks whether `node` is a function expression or an arrow function expression (not a function declaration). | ||
If `node` uses unknown properties of a value that would be considered a Promise, `node` itself would not be considered as a Promise. | ||
Example: | ||
```js | ||
(function foo() {}) | ||
// => true | ||
() => {} | ||
// => true | ||
function foo() {} // function declaration | ||
// => false | ||
``` | ||
Usage example: | ||
```js | ||
function create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (node.callee.type === 'Identifier' | ||
&& node.callee.name === 'test' | ||
&& !astUtils.isFunctionExpression(node.arguments[0]) | ||
&& !astUtils.isFunctionExpression(node.arguments[1]) | ||
) { | ||
context.report({ | ||
node: node, | ||
message: 'You need to pass a function to test()' | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
``` | ||
## License | ||
MIT © [Jeroen Engels](https://github.com/jfmengels) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24402
11
450
319
2
+ Addedlodash.zip@^4.2.0
+ Addedlodash.zip@4.2.0(transitive)