constantinople
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -85,2 +85,6 @@ "use strict"; | ||
else { | ||
if (!b.isExpression(expression.callee)) { | ||
constant = false; | ||
return; | ||
} | ||
var callee = toConstant(expression.callee); | ||
@@ -87,0 +91,0 @@ if (!constant) |
{ | ||
"name": "constantinople", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"main": "lib/index.js", | ||
@@ -13,4 +13,4 @@ "types": "lib/index.d.ts", | ||
"dependencies": { | ||
"@babel/types": "^7.6.1", | ||
"@babel/parser": "^7.6.0" | ||
"@babel/parser": "^7.6.0", | ||
"@babel/types": "^7.6.1" | ||
}, | ||
@@ -20,2 +20,3 @@ "devDependencies": { | ||
"mocha": "*", | ||
"prettier": "^1.18.2", | ||
"typescript": "^2.7.1" | ||
@@ -26,4 +27,6 @@ }, | ||
"build": "tsc", | ||
"pretest": "npm run build", | ||
"test": "mocha -R spec" | ||
"pretest": "npm run build && npm run prettier:check", | ||
"test": "mocha -R spec", | ||
"prettier:write": "prettier --ignore-path .gitignore --write './**/*.{md,json,yaml,js,jsx,ts,tsx}'", | ||
"prettier:check": "echo \"If prettier fails you can fix it by running npm run prettier:write\" && prettier --ignore-path .gitignore --list-different './**/*.{md,json,yaml,js,jsx,ts,tsx}'" | ||
}, | ||
@@ -30,0 +33,0 @@ "repository": { |
# constantinople | ||
Determine whether a JavaScript expression evaluates to a constant (using Babylon). Here it is assumed to be safe to underestimate how constant something is. | ||
Determine whether a JavaScript expression evaluates to a constant (using Babylon). Here it is assumed to be safe to underestimate how constant something is. | ||
@@ -16,9 +16,9 @@ [![Build Status](https://img.shields.io/travis/pugjs/constantinople/master.svg)](https://travis-ci.org/pugjs/constantinople) | ||
```js | ||
var isConstant = require('constantinople') | ||
var isConstant = require('constantinople'); | ||
if (isConstant('"foo" + 5')) { | ||
console.dir(isConstant.toConstant('"foo" + 5')) | ||
console.dir(isConstant.toConstant('"foo" + 5')); | ||
} | ||
if (isConstant('Math.floor(10.5)', {Math: Math})) { | ||
console.dir(isConstant.toConstant('Math.floor(10.5)', {Math: Math})) | ||
console.dir(isConstant.toConstant('Math.floor(10.5)', {Math: Math})); | ||
} | ||
@@ -31,5 +31,5 @@ ``` | ||
Returns `true` if `src` evaluates to a constant, `false` otherwise. It will also return `false` if there is a syntax error, which makes it safe to use on potentially ES6 code. | ||
Returns `true` if `src` evaluates to a constant, `false` otherwise. It will also return `false` if there is a syntax error, which makes it safe to use on potentially ES6 code. | ||
Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there. | ||
Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there. | ||
@@ -40,5 +40,5 @@ Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options). | ||
Returns the value resulting from evaluating `src`. This method throws an error if the expression is not constant. e.g. `toConstant("Math.random()")` would throw an error. | ||
Returns the value resulting from evaluating `src`. This method throws an error if the expression is not constant. e.g. `toConstant("Math.random()")` would throw an error. | ||
Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there. | ||
Constants is an object mapping strings to values, where those values should be treated as constants. Note that this makes it a pretty bad idea to have `Math` in there if the user might make use of `Math.random` and a pretty bad idea to have `Date` in there. | ||
@@ -49,2 +49,2 @@ Options are directly passed-through to [Babylon](https://github.com/babel/babylon#options). | ||
MIT | ||
MIT |
@@ -19,3 +19,3 @@ import {parseExpression, ParserOptions} from '@babel/parser'; | ||
let constant = true; | ||
function toConstant(expression: b.CallExpression['callee']): any { | ||
function toConstant(expression: b.Expression): any { | ||
if (!constant) return; | ||
@@ -76,4 +76,4 @@ if (b.isArrayExpression(expression)) { | ||
: b.isIdentifier(expression.callee.property) | ||
? expression.callee.property.name | ||
: undefined; | ||
? expression.callee.property.name | ||
: undefined; | ||
if (member === undefined && !expression.callee.computed) { | ||
@@ -87,2 +87,6 @@ constant = false; | ||
} else { | ||
if (!b.isExpression(expression.callee)) { | ||
constant = false; | ||
return; | ||
} | ||
const callee = toConstant(expression.callee); | ||
@@ -126,4 +130,4 @@ if (!constant) return; | ||
: b.isIdentifier(expression.property) | ||
? expression.property.name | ||
: undefined; | ||
? expression.property.name | ||
: undefined; | ||
if (member === undefined && !expression.computed) { | ||
@@ -155,6 +159,6 @@ constant = false; | ||
: b.isIdentifier(property.key) | ||
? property.key.name | ||
: b.isStringLiteral(property.key) | ||
? property.key.value | ||
: undefined; | ||
? property.key.name | ||
: b.isStringLiteral(property.key) | ||
? property.key.value | ||
: undefined; | ||
if (!key || key[0] === '_') { | ||
@@ -161,0 +165,0 @@ constant = false; |
Sorry, the diff of this file is not supported yet
35585
969
4