From b6aa555fc8f49e93fc6f30589ac206a277d592ad Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Mon, 7 Sep 2015 10:31:20 +0300 Subject: [PATCH 1/2] #11 Update dependencies. Add jscs configuration file similar to other enb-packages. Make code changes --- jscs.js | 56 +++++++++++++++++++++++++++++++++++++++++++ lib/error-builder.js | 28 +++++++++++----------- lib/file.js | 32 ++++++++++++------------- lib/source-locator.js | 4 ++-- lib/utils.js | 35 ++++++++++----------------- package.json | 11 ++++----- 6 files changed, 105 insertions(+), 61 deletions(-) create mode 100644 jscs.js diff --git a/jscs.js b/jscs.js new file mode 100644 index 0000000..aa7fbbe --- /dev/null +++ b/jscs.js @@ -0,0 +1,56 @@ +module.exports = { + excludeFiles: [ + 'node_modules', + 'coverage' + ], + requireSpaceAfterKeywords: ['if', 'else', 'for', 'while', 'do', 'switch', 'return', 'try', 'catch'], + requireSpaceBeforeBlockStatements: true, + requireSpacesInConditionalExpression: true, + requireSpacesInFunction: { + beforeOpeningCurlyBrace: true + }, + requireSpacesInAnonymousFunctionExpression: { + beforeOpeningRoundBrace: true + }, + disallowSpacesInNamedFunctionExpression: { + beforeOpeningRoundBrace: true + }, + requireMultipleVarDecl: true, + requireBlocksOnNewline: 1, + disallowPaddingNewlinesInBlocks: true, + disallowSpacesInsideArrayBrackets: 'nested', + disallowSpacesInsideParentheses: true, + requireSpacesInsideObjectBrackets: 'all', + disallowQuotedKeysInObjects: 'allButReserved', + disallowSpaceAfterObjectKeys: true, + requireCommaBeforeLineBreak: true, + requireOperatorBeforeLineBreak: true, + disallowSpaceAfterPrefixUnaryOperators: true, + disallowSpaceBeforePostfixUnaryOperators: true, + requireSpaceBeforeBinaryOperators: true, + requireSpaceAfterBinaryOperators: true, + requireCamelCaseOrUpperCaseIdentifiers: true, + disallowKeywords: ['with'], + disallowMultipleLineStrings: true, + disallowMultipleLineBreaks: true, + validateLineBreaks: 'LF', + validateQuoteMarks: { + mark: '\'', + escape: true + }, + disallowMixedSpacesAndTabs: true, + disallowTrailingWhitespace: true, + disallowKeywordsOnNewLine: ['else', 'catch'], + requireLineFeedAtFileEnd: true, + maximumLineLength: 120, + requireCapitalizedConstructors: true, + safeContextKeyword: ['_this'], + disallowYodaConditions: true, + jsDoc: { + checkParamNames: true, + checkRedundantParams: true, + requireParamTypes: true + }, + requireSpaceAfterLineComment: true, + disallowNewlineBeforeBlockStatements: true +}; diff --git a/lib/error-builder.js b/lib/error-builder.js index 3ef9f5b..e4c697a 100644 --- a/lib/error-builder.js +++ b/lib/error-builder.js @@ -1,19 +1,20 @@ -var fs = require('fs'); -var os = require('os'); -var SourceLocator = require('./source-locator'); +var fs = require('fs'), + os = require('os'), + SourceLocator = require('./source-locator'); module.exports = { generateErrorMessage: function (filename, source, message, line, column) { - var sourceLocator = new SourceLocator(filename, source); - var pos = sourceLocator.locate(line, column); - var lines = fs.readFileSync(pos.source, 'utf8').split('\n'); - var lineNumber = pos.line - 1; - var result = [ - renderLine(lineNumber, lines[lineNumber]), - renderPointer(pos.column) - ]; - var i = lineNumber - 1; - var linesAround = 2; + var sourceLocator = new SourceLocator(filename, source), + pos = sourceLocator.locate(line, column), + lines = fs.readFileSync(pos.source, 'utf8').split('\n'), + lineNumber = pos.line - 1, + result = [ + renderLine(lineNumber, lines[lineNumber]), + renderPointer(pos.column) + ], + i = lineNumber - 1, + linesAround = 2; + while (i >= 0 && i >= (lineNumber - linesAround)) { result.unshift(renderLine(i, lines[i])); i--; @@ -33,7 +34,6 @@ module.exports = { * * @param {String} message * @param {String} filename - * @param {Boolean} colorize * @returns {String} */ function formatErrorMessage(message, filename) { diff --git a/lib/file.js b/lib/file.js index 79ef5cf..0b1de00 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,9 +1,9 @@ -var os = require('os'); -var path = require('path'); -var url = require('url'); -var mozilla = require('source-map'); -var SourceMapGenerator = mozilla.SourceMapGenerator; -var utils = require('./utils'); +var os = require('os'), + path = require('path'), + url = require('url'), + mozilla = require('source-map'), + SourceMapGenerator = mozilla.SourceMapGenerator, + utils = require('./utils'); /** * @param {String} filename @@ -13,14 +13,14 @@ var utils = require('./utils'); */ function File(filename, opts) { // Old syntax support, where second parameter was useSourceMap - opts = (typeof opts === 'boolean') ? {sourceMap: opts} : opts; + opts = (typeof opts === 'boolean') ? { sourceMap: opts } : opts; opts = opts || {}; opts.comment = opts.comment || 'inline'; this._content = ['']; if (opts.sourceMap) { - this._map = new SourceMapGenerator({file: path.basename(filename)}); + this._map = new SourceMapGenerator({ file: path.basename(filename) }); } this._opts = opts; } @@ -55,26 +55,26 @@ File.prototype = { var data = utils.splitContentAndSourceMap(content); if (this._map && data.sourceMap) { - var lineOffset = this._content.length - 1; - var middleDir = path.dirname(relPath) + '/'; + var lineOffset = this._content.length - 1, + middleDir = path.dirname(relPath) + '/'; data.sourceMap.eachMapping(function (mapping) { this._map.addMapping({ source: url.resolve(middleDir, mapping.source), // make source relative to processed file - original: {line: mapping.originalLine, column: mapping.originalColumn}, - generated: {line: lineOffset + mapping.generatedLine, column: mapping.generatedColumn} + original: { line: mapping.originalLine, column: mapping.originalColumn }, + generated: { line: lineOffset + mapping.generatedLine, column: mapping.generatedColumn } }); }.bind(this)); } - var lines = getLines(data.content); - var lastLineNum = lines.length - 1; + var lines = getLines(data.content), + lastLineNum = lines.length - 1; lines.forEach(function (line, i) { if (this._map && !data.sourceMap) { this._map.addMapping({ source: relPath, - original: {line: lineNumber + i, column: i === 0 ? column : 0}, - generated: {line: this._content.length, column: i === 0 ? this.getCursor().column : 0} + original: { line: lineNumber + i, column: i === 0 ? column : 0 }, + generated: { line: this._content.length, column: i === 0 ? this.getCursor().column : 0 } }); } if (i === lastLineNum) { diff --git a/lib/source-locator.js b/lib/source-locator.js index c3b5013..1ff61b1 100644 --- a/lib/source-locator.js +++ b/lib/source-locator.js @@ -1,5 +1,5 @@ -var getSourceMap = require('./utils').getSourceMap; -var path = require('path'); +var getSourceMap = require('./utils').getSourceMap, + path = require('path'); function SourceLocator(filename, source) { this._map = getSourceMap(source); diff --git a/lib/utils.js b/lib/utils.js index 1f83bbc..e678cd8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,10 +1,9 @@ -var os = require('os'); -var SourceMapConsumer = require('source-map').SourceMapConsumer; -var SourceMapGenerator = require('source-map').SourceMapGenerator; -var atob = require('atob'); -var btoa = require('btoa'); - -var SOURCE_MAPPING_URL_COMMENT = 'sourceMappingURL=data:application/json;base64,'; +var os = require('os'), + SourceMapConsumer = require('source-map').SourceMapConsumer, + SourceMapGenerator = require('source-map').SourceMapGenerator, + atob = require('atob'), + btoa = require('btoa'), + SOURCE_MAPPING_URL_COMMENT = 'sourceMappingURL=data:application/json;base64,'; module.exports = { /** @@ -12,16 +11,12 @@ module.exports = { * @return {SourceMapConsumer} */ getSourceMap: function (content) { - return Array.isArray(content) - ? getFromLastLine_(content) - : getFromText_(content); + return Array.isArray(content) ? getFromLastLine_(content) : getFromText_(content); - /// function getFromLastLine_(lines) { return mkSourceMap(split(lines[lines.length - 1]).sourceMap); } - /// function getFromText_(text) { return mkSourceMap(split(text).sourceMap); } @@ -33,11 +28,8 @@ module.exports = { * @return {String|String[]} content without sourcemap string */ removeBuiltInSourceMap: function (content) { - return Array.isArray(content) - ? removeFromLines_(content.concat()) - : removeFromText_(content); + return Array.isArray(content) ? removeFromLines_(content.concat()) : removeFromText_(content); - /// function removeFromLines_(lines) { if (split(lines[lines.length - 1]).sourceMap) { lines.pop(); @@ -45,13 +37,11 @@ module.exports = { return lines; } - /// function removeFromText_(text) { return split(text).content; } }, - /// splitContentAndSourceMap: function (content) { var pieces = split(content); if (pieces.sourceMap) { @@ -75,10 +65,10 @@ module.exports = { opts = opts || {}; opts.comment = opts.comment || 'inline'; - var sourceMapComment = '# ' + SOURCE_MAPPING_URL_COMMENT + btoa(sourceMap.toString()); - var sourceMapLine = opts.comment === 'inline' - ? '//' + sourceMapComment - : '/*' + sourceMapComment + '*/'; + var sourceMapComment = '# ' + SOURCE_MAPPING_URL_COMMENT + btoa(sourceMap.toString()), + sourceMapLine = opts.comment === 'inline' ? + '//' + sourceMapComment : + '/*' + sourceMapComment + '*/'; return [content.trimRight(), sourceMapLine].join(os.EOL); } @@ -109,7 +99,6 @@ function split(text) { }; } -/// function mkSourceMap(base64Str) { return base64Str && new SourceMapConsumer(JSON.parse(atob(base64Str))); } diff --git a/package.json b/package.json index b6762ab..eb9065a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "test": "npm run-script jshint && npm run-script check-style && npm run-script unit", "jshint": "jshint .", "unit": "mocha tests", - "check-style": "jscs -c node_modules/enb-validate-code/jscs.json lib" + "check-style": "jscs -c jscs.js lib" }, "repository": { "type": "git", @@ -18,17 +18,16 @@ }, "homepage": "https://github.com/enb-make/enb-source-map", "dependencies": { - "source-map": "0.1.43", + "source-map": "0.4.4", "btoa": "1.1.2", "atob": "1.1.2" }, "devDependencies": { "chai": "3.2.0", - "enb-validate-code": "0.0.1", - "jscs": "1.4.5", + "jscs": "2.1.1", "jshint": "2.8.0", - "mocha": "2.2.5", - "sinon": "1.15.4", + "mocha": "2.3.1", + "sinon": "1.16.1", "sinon-chai": "2.8.0" } } From 8c849ab5ef422d381fa6095f05ab887f19bc51be Mon Sep 17 00:00:00 2001 From: tormozz48 Date: Tue, 8 Sep 2015 06:51:30 +0300 Subject: [PATCH 2/2] #11 Fix codestyle. Disable requireOperatorBeforeLineBreak jscs option --- jscs.js | 2 +- lib/utils.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/jscs.js b/jscs.js index aa7fbbe..4666840 100644 --- a/jscs.js +++ b/jscs.js @@ -24,7 +24,7 @@ module.exports = { disallowQuotedKeysInObjects: 'allButReserved', disallowSpaceAfterObjectKeys: true, requireCommaBeforeLineBreak: true, - requireOperatorBeforeLineBreak: true, + requireOperatorBeforeLineBreak: false, disallowSpaceAfterPrefixUnaryOperators: true, disallowSpaceBeforePostfixUnaryOperators: true, requireSpaceBeforeBinaryOperators: true, diff --git a/lib/utils.js b/lib/utils.js index e678cd8..0c15a4e 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -11,7 +11,9 @@ module.exports = { * @return {SourceMapConsumer} */ getSourceMap: function (content) { - return Array.isArray(content) ? getFromLastLine_(content) : getFromText_(content); + return Array.isArray(content) + ? getFromLastLine_(content) + : getFromText_(content); function getFromLastLine_(lines) { return mkSourceMap(split(lines[lines.length - 1]).sourceMap); @@ -28,7 +30,9 @@ module.exports = { * @return {String|String[]} content without sourcemap string */ removeBuiltInSourceMap: function (content) { - return Array.isArray(content) ? removeFromLines_(content.concat()) : removeFromText_(content); + return Array.isArray(content) + ? removeFromLines_(content.concat()) + : removeFromText_(content); function removeFromLines_(lines) { if (split(lines[lines.length - 1]).sourceMap) { @@ -66,9 +70,9 @@ module.exports = { opts.comment = opts.comment || 'inline'; var sourceMapComment = '# ' + SOURCE_MAPPING_URL_COMMENT + btoa(sourceMap.toString()), - sourceMapLine = opts.comment === 'inline' ? - '//' + sourceMapComment : - '/*' + sourceMapComment + '*/'; + sourceMapLine = opts.comment === 'inline' + ? '//' + sourceMapComment + : '/*' + sourceMapComment + '*/'; return [content.trimRight(), sourceMapLine].join(os.EOL); }