-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 109dd3e
Showing
8 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
# Custom for Visual Studio | ||
*.cs diff=csharp | ||
*.sln merge=union | ||
*.csproj merge=union | ||
*.vbproj merge=union | ||
*.fsproj merge=union | ||
*.dbproj merge=union | ||
|
||
# Standard to msysgit | ||
*.doc diff=astextplain | ||
*.DOC diff=astextplain | ||
*.docx diff=astextplain | ||
*.DOCX diff=astextplain | ||
*.dot diff=astextplain | ||
*.DOT diff=astextplain | ||
*.pdf diff=astextplain | ||
*.PDF diff=astextplain | ||
*.rtf diff=astextplain | ||
*.RTF diff=astextplain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
pids | ||
logs | ||
results | ||
npm-debug.log | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- "0.8" | ||
- "0.10" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2013 Forbes Lindesay | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# constantinople | ||
|
||
Determine whether a JavaScript expression evaluates to a constant (using UglifyJS). Here it is assumed to be safe to underestimate how constant something is. | ||
|
||
[![Build Status](https://travis-ci.org/ForbesLindesay/constantinople.png?branch=master)](https://travis-ci.org/ForbesLindesay/constantinople) | ||
[![Dependency Status](https://gemnasium.com/ForbesLindesay/constantinople.png)](https://gemnasium.com/ForbesLindesay/constantinople) | ||
[![NPM version](https://badge.fury.io/js/constantinople.png)](http://badge.fury.io/js/constantinople) | ||
|
||
## Installation | ||
|
||
npm install constantinople | ||
|
||
## Usage | ||
|
||
```js | ||
var isConstant = require('constantinople') | ||
|
||
if (isConstant('"foo" + 5')) { | ||
console.dir(isConstant.toConstant('"foo" + 5')) | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### isConstant(src) | ||
|
||
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. | ||
|
||
### toConstant(src) | ||
|
||
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. | ||
|
||
## License | ||
|
||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict' | ||
|
||
var uglify = require('uglify-js') | ||
|
||
var lastSRC = 'null' | ||
var lastRes = true | ||
|
||
module.exports = isConstant | ||
function isConstant(src) { | ||
if (lastSRC === src) return lastRes | ||
lastSRC = src | ||
try { | ||
return lastRes = (detect(src).length === 0) | ||
} catch (ex) { | ||
return lastRes = false | ||
} | ||
} | ||
isConstant.isConstant = isConstant | ||
|
||
isConstant.toConstant = toConstant | ||
function toConstant(src) { | ||
if (!isConstant(src)) throw new Error(JSON.stringify(src) + ' is not constant.') | ||
return Function('return (' + src + ')')() | ||
} | ||
|
||
function detect(src) { | ||
var ast = uglify.parse(src.toString()) | ||
ast.figure_out_scope() | ||
var globals = ast.globals | ||
.map(function (node, name) { | ||
return name | ||
}) | ||
return globals | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "constantinople", | ||
"version": "0.0.0", | ||
"description": "Determine whether a JavaScript expression evaluates to a constant (using UglifyJS)", | ||
"keywords": [], | ||
"dependencies": { | ||
"uglify-js": "~2.3.6" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*" | ||
}, | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ForbesLindesay/constantinople.git" | ||
}, | ||
"author": "ForbesLindesay", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
var assert = require('assert') | ||
var constaninople = require('../') | ||
|
||
describe('isConstant(src)', function () { | ||
it('handles "[5 + 3 + 10]"', function () { | ||
assert(constaninople.isConstant('[5 + 3 + 10]') === true) | ||
}) | ||
it('handles "/[a-z]/.test(\'a\')"', function () { | ||
assert(constaninople.isConstant('/[a-z]/.test(\'a\')') === true) | ||
}) | ||
it('handles "Math.random()"', function () { | ||
assert(constaninople.isConstant('Math.random()') === false) | ||
}) | ||
it('handles "Math.random("', function () { | ||
assert(constaninople.isConstant('Math.random(') === false) | ||
}) | ||
}) | ||
|
||
|
||
describe('toConstant(src)', function () { | ||
it('handles "[5 + 3 + 10]"', function () { | ||
assert.deepEqual(constaninople.toConstant('[5 + 3 + 10]'), [5 + 3 + 10]) | ||
}) | ||
it('handles "/[a-z]/.test(\'a\')"', function () { | ||
assert(constaninople.toConstant('/[a-z]/.test(\'a\')') === true) | ||
}) | ||
it('handles "Math.random()"', function () { | ||
try { | ||
constaninople.toConstant('Math.random()') | ||
} catch (ex) { | ||
return | ||
} | ||
assert(false, 'Math.random() should result in an error') | ||
}) | ||
it('handles "Math.random("', function () { | ||
try { | ||
constaninople.toConstant('Math.random(') | ||
} catch (ex) { | ||
return | ||
} | ||
assert(false, 'Math.random( should result in an error') | ||
}) | ||
}) |