Skip to content

Commit

Permalink
v0.3.1 coverity
Browse files Browse the repository at this point in the history
  • Loading branch information
amarcruz committed Oct 14, 2016
1 parent 6fd2637 commit 525b886
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 47 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
# Coverage directories
coverage
cov-int
lib-cov

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes for jscc

### 2016-10-06 v0.3.0
### v0.3.1 @ 2016-10-14

- Source map includes the source filename (needed by jscc-brunch).
- Removed jscc own source maps from the distribution.
- The CommonJS version validated by Coverty Scan.

### v0.3.0 @ 2016-10-06

- Initial Release published as v0.3.0 in npm over an old `jscc` tool from Taketoshi Aono.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Build Status][build-image]][build-url]
[![Issues Count][issues-image]][issues-url]
[![Coverity Scan Build Status][coverity-image]][coverity-url]
[![Coverage][coverage-image]][coverage-url]
[![npm][npm-image]][npm-url]
[![License][license-image]][license-url]
Expand All @@ -19,7 +19,7 @@ With jscc, you have:

jscc is **not** a minifier tool, it only does well that it does...

jscc is derived on [jspreproc](http://amarcruz.github.io/jspreproc), the tiny source file preprocessor in JavaScript, enhanced with Source Map support but without the file importer (rollup and others does this better).
jscc is derived on [jspreproc](http://amarcruz.github.io/jspreproc), the tiny source file preprocessor in JavaScript, enhanced with Source Map support but without the file importer ([rollup](https://github.com/rollup/rollup) and other bundlers does this better).

## Install

Expand Down Expand Up @@ -47,13 +47,15 @@ import mylib from 'mylib-debug';
//#else */
import mylib from 'mylib';
//#endif

mylib.log('Starting v$_VERSION...');
```

output:

```js
import mylib from 'mylib-debug';

mylib.log('Starting v1.0.0...');
```

Expand All @@ -79,7 +81,7 @@ This is work in progress, so please update jscc constantly, I hope the first sta
Expected:

- [ ] Explanatory error messages, with location of the error
- [ ] async mode
- [ ] Async mode
- [ ] Better documentation*
- [ ] Syntax hilighter for some editores
- [ ] You tell me...
Expand All @@ -100,6 +102,8 @@ Don't forget to give me your star!
[climate-url]: https://codeclimate.com/github/aMarCruz/jscc
[issues-image]: https://codeclimate.com/github/aMarCruz/jscc/badges/issue_count.svg
[issues-url]: https://codeclimate.com/github/aMarCruz/jscc
[coverity-image]: https://scan.coverity.com/projects/10389/badge.svg
[coverity-url]: https://scan.coverity.com/projects/amarcruz-jscc
[coverage-image]: https://codeclimate.com/github/aMarCruz/jscc/badges/coverage.svg
[coverage-url]: https://codeclimate.com/github/aMarCruz/jscc/coverage
[npm-image]: https://img.shields.io/npm/v/jscc.svg
Expand Down
28 changes: 17 additions & 11 deletions lib/jscc.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ var _REPVARS = RegExp(((STRINGS.source) + "|" + (EVLVARS.source)), 'g')
* Method to perform the evaluation of the received string using
* a function instantiated dynamically.
*
* @param {object} ctx - Object with the current set of variables
* @param {string} str - String to evaluate, can include other defined vars
* @param {object} ctx - Set of variable definitions
* @returns {any} The result.
*/
function evalExpr (str, ctx) {
function evalExpr (ctx, str) {
var values = ctx.options.values

// var replacement
var _repVars = function (m, p, v) {
return v
? p + (v in ctx ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
? p + (v in values ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
: m
}

Expand All @@ -50,10 +51,10 @@ function evalExpr (str, ctx) {
try {
// eslint-disable-next-line no-new-func
var fn = new Function('', ("return (" + expr + ");"))
result = fn.call(ctx)
result = fn.call(values)
} catch (e) {
e.message += " in expression: " + expr
throw e
result = false
ctx._emitError(((e.message) + " in expression \"" + expr + "\""))
}

return result
Expand Down Expand Up @@ -278,7 +279,7 @@ Parser.prototype = {
return ckey === 'ifnset' ? yes ^ 1 : yes
}
// returns the raw value of the expression
return evalExpr(expr, this.options.values)
return evalExpr(this, expr)
},

_set: function _set (s) {
Expand All @@ -287,7 +288,7 @@ Parser.prototype = {
var k = m[1]
var v = m[2]

this.options.values[k] = v ? evalExpr(v.trim(), this.options.values) : undefined
this.options.values[k] = v ? evalExpr(this, v.trim()) : undefined
} else {
this._emitError(("Invalid memvar assignment \"" + s + "\""))
}
Expand All @@ -303,7 +304,7 @@ Parser.prototype = {
},

_error: function _error (s) {
s = evalExpr(s, this.options.values)
s = evalExpr(this, s)
throw new Error(s)
}
}
Expand Down Expand Up @@ -472,7 +473,13 @@ function preproc (code, filename, options) {
code: magicStr.toString()
}
if (changes && options.sourceMap) {
result.map = magicStr.generateMap({ hires: true })
var name = options.values._FILE

result.map = magicStr.generateMap({
source: name,
file: name.split(/[\\/]/).pop(),
hires: true
})
}
return result
}
Expand Down Expand Up @@ -514,4 +521,3 @@ function preproc (code, filename, options) {
}

export default preproc;
//# sourceMappingURL=jscc.es.js.map
28 changes: 17 additions & 11 deletions lib/jscc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ var _REPVARS = RegExp(((STRINGS.source) + "|" + (EVLVARS.source)), 'g')
* Method to perform the evaluation of the received string using
* a function instantiated dynamically.
*
* @param {object} ctx - Object with the current set of variables
* @param {string} str - String to evaluate, can include other defined vars
* @param {object} ctx - Set of variable definitions
* @returns {any} The result.
*/
function evalExpr (str, ctx) {
function evalExpr (ctx, str) {
var values = ctx.options.values

// var replacement
var _repVars = function (m, p, v) {
return v
? p + (v in ctx ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
? p + (v in values ? ("this." + v) : v in global ? ("global." + v) : 'undefined')
: m
}

Expand All @@ -52,10 +53,10 @@ function evalExpr (str, ctx) {
try {
// eslint-disable-next-line no-new-func
var fn = new Function('', ("return (" + expr + ");"))
result = fn.call(ctx)
result = fn.call(values)
} catch (e) {
e.message += " in expression: " + expr
throw e
result = false
ctx._emitError(((e.message) + " in expression \"" + expr + "\""))
}

return result
Expand Down Expand Up @@ -280,7 +281,7 @@ Parser.prototype = {
return ckey === 'ifnset' ? yes ^ 1 : yes
}
// returns the raw value of the expression
return evalExpr(expr, this.options.values)
return evalExpr(this, expr)
},

_set: function _set (s) {
Expand All @@ -289,7 +290,7 @@ Parser.prototype = {
var k = m[1]
var v = m[2]

this.options.values[k] = v ? evalExpr(v.trim(), this.options.values) : undefined
this.options.values[k] = v ? evalExpr(this, v.trim()) : undefined
} else {
this._emitError(("Invalid memvar assignment \"" + s + "\""))
}
Expand All @@ -305,7 +306,7 @@ Parser.prototype = {
},

_error: function _error (s) {
s = evalExpr(s, this.options.values)
s = evalExpr(this, s)
throw new Error(s)
}
}
Expand Down Expand Up @@ -474,7 +475,13 @@ function preproc (code, filename, options) {
code: magicStr.toString()
}
if (changes && options.sourceMap) {
result.map = magicStr.generateMap({ hires: true })
var name = options.values._FILE

result.map = magicStr.generateMap({
source: name,
file: name.split(/[\\/]/).pop(),
hires: true
})
}
return result
}
Expand Down Expand Up @@ -516,4 +523,3 @@ function preproc (code, filename, options) {
}

module.exports = preproc;
//# sourceMappingURL=jscc.js.map
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jscc",
"version": "0.3.0",
"version": "0.3.1",
"description": "The JavaScript preprocessor for conditional comments and replacements in text files",
"main": "./lib/jscc.js",
"module": "./lib/jscc.es.js",
Expand All @@ -12,12 +12,11 @@
"scripts": {
"test": "istanbul cover ./node_modules/mocha/bin/_mocha -- test/index.js",
"lint": "node-version-gte-4 && eslint src test/index.js || node-version-lt-4",
"pretest": "npm run build:cjs",
"pretest": "rollup -c -m -f cjs -o lib/jscc.js",
"build": "rm -rf lib/* && npm run build:cjs && npm run build:es6",
"build:cjs": "rollup -c -m -f cjs -o lib/jscc.js",
"build:es6": "rollup -c -m -f es -o lib/jscc.es.js",
"prepublish": "npm run lint && npm test && npm run build:es6",
"test:es6": "rollup -i test6.js -o stest.js"
"build:cjs": "rollup -c -f cjs -o lib/jscc.js",
"build:es6": "rollup -c -f es -o lib/jscc.es.js",
"prepublish": "npm run lint && npm test && npm run build"
},
"files": [
"lib/*.js",
Expand All @@ -35,9 +34,12 @@
"html5",
"parser",
"javascript",
"es6",
"conditional",
"comments",
"preprocessor"
"compilation",
"preprocessor",
"replacement"
],
"author": {
"name": "aMarCruz",
Expand Down
13 changes: 7 additions & 6 deletions src/evalexpr.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const _REPVARS = RegExp(`${STRINGS.source}|${EVLVARS.source}`, 'g')
* Method to perform the evaluation of the received string using
* a function instantiated dynamically.
*
* @param {object} ctx - Object with the current set of variables
* @param {string} str - String to evaluate, can include other defined vars
* @param {object} ctx - Set of variable definitions
* @returns {any} The result.
*/
export default function evalExpr (str, ctx) {
export default function evalExpr (ctx, str) {
const values = ctx.options.values

// var replacement
const _repVars = function (m, p, v) {
return v
? p + (v in ctx ? `this.${v}` : v in global ? `global.${v}` : 'undefined')
? p + (v in values ? `this.${v}` : v in global ? `global.${v}` : 'undefined')
: m
}

Expand All @@ -31,10 +32,10 @@ export default function evalExpr (str, ctx) {
try {
// eslint-disable-next-line no-new-func
const fn = new Function('', `return (${expr});`)
result = fn.call(ctx)
result = fn.call(values)
} catch (e) {
e.message += ` in expression: ${expr}`
throw e
result = false
ctx._emitError(`${e.message} in expression "${expr}"`)
}

return result
Expand Down
6 changes: 3 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Parser.prototype = {
return ckey === 'ifnset' ? yes ^ 1 : yes
}
// returns the raw value of the expression
return evalExpr(expr, this.options.values)
return evalExpr(this, expr)
},

_set (s) {
Expand All @@ -229,7 +229,7 @@ Parser.prototype = {
const k = m[1]
const v = m[2]

this.options.values[k] = v ? evalExpr(v.trim(), this.options.values) : undefined
this.options.values[k] = v ? evalExpr(this, v.trim()) : undefined
} else {
this._emitError(`Invalid memvar assignment "${s}"`)
}
Expand All @@ -245,7 +245,7 @@ Parser.prototype = {
},

_error (s) {
s = evalExpr(s, this.options.values)
s = evalExpr(this, s)
throw new Error(s)
}
}
8 changes: 7 additions & 1 deletion src/preproc.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ export default function preproc (code, filename, options) {
code: magicStr.toString()
}
if (changes && options.sourceMap) {
result.map = magicStr.generateMap({ hires: true })
const name = options.values._FILE

result.map = magicStr.generateMap({
source: name,
file: name.split(/[\\/]/).pop(),
hires: true
})
}
return result
}
Expand Down

0 comments on commit 525b886

Please sign in to comment.