This is an automated email from the ASF dual-hosted git repository. davisp pushed a commit to branch gh-pages in repository https://gitbox.apache.org/repos/asf/couchdb-escodegen.git
commit 9ede3805d6979391dd58e2ad7dde7b3e8d349b40 Author: Constellation <[email protected]> AuthorDate: Sun May 13 12:19:20 2012 +0900 update escodegen.js --- escodegen.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/escodegen.js b/escodegen.js index b4ea9ad..0590190 100644 --- a/escodegen.js +++ b/escodegen.js @@ -133,10 +133,19 @@ '/': Precedence.Multiplicative }; - if (typeof Object.freeze === 'function') { - Object.freeze(Syntax); - Object.freeze(Precedence); - Object.freeze(BinaryPrecedence); + function getDefaultOptions() { + // default options + return { + indent: null, + base: null, + parse: null, + format: { + indent: { + style: ' ', + base: 0 + } + } + }; } function unicodeEscape(ch) { @@ -158,6 +167,42 @@ return result; } + function stringRepeat(str, num) { + var result = ''; + + for (num |= 0; num > 0; num >>>= 1, str += str) { + if (num & 1) { + result += str; + } + } + + return result; + } + + function updateDeeply(target, override) { + var key, val; + + function isHashObject(target) { + return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp); + } + + for (key in override) { + if (override.hasOwnProperty(key)) { + val = override[key]; + if (isHashObject(val)) { + if (isHashObject(target[key])) { + updateDeeply(target[key], val); + } else { + target[key] = updateDeeply({}, val); + } + } else { + target[key] = val; + } + } + } + return target; + } + function escapeString(str) { var result = '', i, len, ch; @@ -723,7 +768,7 @@ case Syntax.Program: result = ''; for (i = 0, len = stmt.body.length; i < len; i += 1) { - result += generateStatement(stmt.body[i]); + result += addIndent(generateStatement(stmt.body[i])); if ((i + 1) < len) { result += '\n'; } @@ -773,14 +818,32 @@ } function generate(node, options) { + var defaultOptions = getDefaultOptions(); + if (typeof options !== 'undefined') { - base = options.base || ''; - indent = options.indent || ' '; + // Obsolete options + // + // `options.indent` + // `options.base` + // + // Instead of them, we can use `option.format.indent`. + if (typeof options.indent === 'string') { + defaultOptions.format.indent.style = options.indent; + } + + options = updateDeeply(defaultOptions, options); + indent = options.format.indent.style; + if (typeof options.base === 'string') { + base = options.base; + } else { + base = stringRepeat(indent, options.format.indent.base); + } parse = options.parse; } else { - base = ''; - indent = ' '; - parse = null; + options = defaultOptions; + indent = options.format.indent.style; + base = stringRepeat(indent, options.format.indent.base); + parse = options.parse; } switch (node.type) { @@ -835,7 +898,7 @@ } // Sync with package.json. - exports.version = '0.0.3-dev'; + exports.version = '0.0.4-dev'; exports.generate = generate;
