strip comments from the JSON config file (#660)

This commit is contained in:
Jeff Williams 2014-06-09 20:36:38 -07:00
parent fd5ca944d2
commit d4219bafad
4 changed files with 128 additions and 1 deletions

3
cli.js
View File

@ -17,6 +17,7 @@ module.exports = (function() {
'use strict';
var logger = require('jsdoc/util/logger');
var stripJsonComments = require('strip-json-comments');
var hasOwnProp = Object.prototype.hasOwnProperty;
@ -86,7 +87,7 @@ cli.loadConfig = function() {
}
try {
env.conf = new Config( fs.readFileSync(confPath, 'utf8') )
env.conf = new Config( stripJsonComments(fs.readFileSync(confPath, 'utf8')) )
.get();
}
catch (e) {

61
node_modules/strip-json-comments/package.json generated vendored Normal file
View File

@ -0,0 +1,61 @@
{
"name": "strip-json-comments",
"version": "0.1.3",
"description": "Strip comments from JSON. Lets you use comments in your JSON files!",
"keywords": [
"json",
"strip",
"remove",
"delete",
"trim",
"comments",
"multiline",
"parse",
"config",
"configuration",
"conf",
"settings",
"util",
"env",
"environment",
"cli",
"bin"
],
"license": "MIT",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"files": [
"cli.js",
"strip-json-comments.js"
],
"main": "strip-json-comments",
"bin": {
"strip-json-comments": "cli.js"
},
"repository": {
"type": "git",
"url": "git://github.com/sindresorhus/strip-json-comments"
},
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "*"
},
"engines": {
"node": ">=0.8.0"
},
"readme": "# strip-json-comments [![Build Status](https://travis-ci.org/sindresorhus/strip-json-comments.svg?branch=master)](https://travis-ci.org/sindresorhus/strip-json-comments)\n\n> Strip comments from JSON. Lets you use comments in your JSON files!\n\nThis is now possible:\n\n```js\n{\n\t// rainbows\n\t\"unicorn\": /* ❤ */ \"cake\"\n}\n```\n\nIt will remove single-line comments `//` and mult-line comments `/**/`.\n\nAlso available as a [gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin and a [require hook](https://github.com/uTest/autostrip-json-comments).\n\n\n*There's already [json-comments](https://npmjs.org/package/json-comments), but it's only for Node.js and uses a naive regex to strip comments which fails on simple cases like `{\"a\":\"//\"}`. This module however parses out the comments.*\n\n\n## Install\n\n```sh\n$ npm install --save strip-json-comments\n```\n\n```sh\n$ bower install --save strip-json-comments\n```\n\n```sh\n$ component install sindresorhus/strip-json-comments\n```\n\n\n## Usage\n\n```js\nvar json = '{/*rainbows*/\"unicorn\":\"cake\"}';\nJSON.parse(stripJsonComments(json));\n//=> {unicorn: 'cake'}\n```\n\n\n## API\n\n### stripJsonComments(input)\n\n#### input\n\nType: `string`\n\nAccepts a string with JSON and returns a string without comments.\n\n\n## CLI\n\n```sh\n$ npm install --global strip-json-comments\n```\n\n```sh\n$ strip-json-comments --help\n\nstrip-json-comments <input-file> > <output-file>\n# or\ncat <input-file> | strip-json-comments > <output-file>\n```\n\n\n## License\n\nMIT © [Sindre Sorhus](http://sindresorhus.com)\n",
"readmeFilename": "readme.md",
"bugs": {
"url": "https://github.com/sindresorhus/strip-json-comments/issues"
},
"homepage": "https://github.com/sindresorhus/strip-json-comments",
"_id": "strip-json-comments@0.1.3",
"_shasum": "164c64e370a8a3cc00c9e01b539e569823f0ee54",
"_from": "strip-json-comments@",
"_resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-0.1.3.tgz"
}

View File

@ -0,0 +1,64 @@
/*!
strip-json-comments
Strip comments from JSON. Lets you use comments in your JSON files!
https://github.com/sindresorhus/strip-json-comments
by Sindre Sorhus
MIT License
*/
(function () {
'use strict';
function stripJsonComments(str) {
var currentChar;
var nextChar;
var insideString = false;
var insideComment = false;
var ret = '';
for (var i = 0; i < str.length; i++) {
currentChar = str[i];
nextChar = str[i + 1];
if (!insideComment && str[i - 1] !== '\\' && currentChar === '"') {
insideString = !insideString;
}
if (insideString) {
ret += currentChar;
continue;
}
if (!insideComment && currentChar + nextChar === '//') {
insideComment = 'single';
i++;
} else if (insideComment === 'single' && currentChar + nextChar === '\r\n') {
insideComment = false;
i++;
} else if (insideComment === 'single' && currentChar === '\n') {
insideComment = false;
} else if (!insideComment && currentChar + nextChar === '/*') {
insideComment = 'multi';
i++;
continue;
} else if (insideComment === 'multi' && currentChar + nextChar === '*/') {
insideComment = false;
i++;
continue;
}
if (insideComment) {
continue;
}
ret += currentChar;
}
return ret;
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = stripJsonComments;
} else {
window.stripJsonComments = stripJsonComments;
}
})();

View File

@ -24,6 +24,7 @@
"js2xmlparser": "~0.1.0",
"marked": "~0.3.1",
"requizzle": "~0.1.0",
"strip-json-comments": "~0.1.3",
"taffydb": "https://github.com/hegemonic/taffydb/tarball/master",
"underscore": "~1.6.0",
"wrench": "~1.3.9"