From 525b88655b266d1c650341448da5693a4fe5dd28 Mon Sep 17 00:00:00 2001 From: amarcruz Date: Fri, 14 Oct 2016 02:47:23 -0500 Subject: [PATCH] v0.3.1 coverity --- .gitignore | 7 +++---- CHANGELOG.md | 8 +++++++- README.md | 10 +++++++--- lib/jscc.es.js | 28 +++++++++++++++++----------- lib/jscc.js | 28 +++++++++++++++++----------- package.json | 16 +++++++++------- src/evalexpr.js | 13 +++++++------ src/parser.js | 6 +++--- src/preproc.js | 8 +++++++- 9 files changed, 77 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index b42d95b..06061dc 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index ba7d663..25ecbfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index a5033ce..d469a63 100644 --- a/README.md +++ b/README.md @@ -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] @@ -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 @@ -47,6 +47,7 @@ import mylib from 'mylib-debug'; //#else */ import mylib from 'mylib'; //#endif + mylib.log('Starting v$_VERSION...'); ``` @@ -54,6 +55,7 @@ output: ```js import mylib from 'mylib-debug'; + mylib.log('Starting v1.0.0...'); ``` @@ -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... @@ -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 diff --git a/lib/jscc.es.js b/lib/jscc.es.js index 225f106..fdf14a8 100644 --- a/lib/jscc.es.js +++ b/lib/jscc.es.js @@ -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 } @@ -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 @@ -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) { @@ -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 + "\"")) } @@ -303,7 +304,7 @@ Parser.prototype = { }, _error: function _error (s) { - s = evalExpr(s, this.options.values) + s = evalExpr(this, s) throw new Error(s) } } @@ -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 } @@ -514,4 +521,3 @@ function preproc (code, filename, options) { } export default preproc; -//# sourceMappingURL=jscc.es.js.map diff --git a/lib/jscc.js b/lib/jscc.js index a973740..08b932b 100644 --- a/lib/jscc.js +++ b/lib/jscc.js @@ -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 } @@ -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 @@ -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) { @@ -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 + "\"")) } @@ -305,7 +306,7 @@ Parser.prototype = { }, _error: function _error (s) { - s = evalExpr(s, this.options.values) + s = evalExpr(this, s) throw new Error(s) } } @@ -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 } @@ -516,4 +523,3 @@ function preproc (code, filename, options) { } module.exports = preproc; -//# sourceMappingURL=jscc.js.map diff --git a/package.json b/package.json index 74c84e1..d1f4b3b 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -35,9 +34,12 @@ "html5", "parser", "javascript", + "es6", "conditional", "comments", - "preprocessor" + "compilation", + "preprocessor", + "replacement" ], "author": { "name": "aMarCruz", diff --git a/src/evalexpr.js b/src/evalexpr.js index 8745a87..c3fa171 100644 --- a/src/evalexpr.js +++ b/src/evalexpr.js @@ -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 } @@ -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 diff --git a/src/parser.js b/src/parser.js index 5e3bd47..0b43aca 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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) { @@ -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}"`) } @@ -245,7 +245,7 @@ Parser.prototype = { }, _error (s) { - s = evalExpr(s, this.options.values) + s = evalExpr(this, s) throw new Error(s) } } diff --git a/src/preproc.js b/src/preproc.js index 5c2ddc6..3bf2c16 100644 --- a/src/preproc.js +++ b/src/preproc.js @@ -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 }