From 8f68f62f8ad42a7662566043cbdf1c2019275a33 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Thu, 22 Jan 2015 19:23:56 +1100 Subject: [PATCH] add jscs --- .jscsrc | 60 +++++++++++++++++++ .jshintignore | 2 - Makefile | 2 + bin/6to5/file.js | 4 +- bin/6to5/index.js | 2 +- lib/6to5/generation/buffer.js | 4 +- lib/6to5/generation/generator.js | 2 +- lib/6to5/generation/whitespace.js | 2 +- .../transformation/helpers/replace-supers.js | 21 +++---- .../templates/async-to-generator.js | 2 +- .../templates/tagged-template-literal.js | 1 - .../transformers/es6/block-scoping.js | 8 +-- .../transformers/es6/classes.js | 9 ++- .../transformers/es6/constants.js | 1 - .../transformers/es6/destructuring.js | 28 ++++----- .../transformation/transformers/es6/for-of.js | 2 +- .../transformation/transformers/es6/spread.js | 2 +- .../transformers/other/react.js | 2 +- lib/6to5/types/index.js | 8 ++- package.json | 1 + 20 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 .jscsrc diff --git a/.jscsrc b/.jscsrc new file mode 100644 index 0000000000..f0d8206e82 --- /dev/null +++ b/.jscsrc @@ -0,0 +1,60 @@ +{ + "excludeFiles": ["lib/6to5/transformation/templates"], + "disallowSpacesInNamedFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + "requireSpacesInAnonymousFunctionExpression": { + "beforeOpeningRoundBrace": true + }, + "disallowSpacesInFunctionDeclaration": { + "beforeOpeningRoundBrace": true + }, + "disallowSpacesInsideArrayBrackets": true, + "disallowSpacesInsideParentheses": true, + "disallowQuotedKeysInObjects": true, + "disallowSpaceAfterObjectKeys": true, + "disallowSpaceAfterPrefixUnaryOperators": true, + "disallowSpaceBeforePostfixUnaryOperators": true, + "disallowSpaceBeforeBinaryOperators": [ + "," + ], + "disallowMixedSpacesAndTabs": true, + "disallowTrailingWhitespace": true, + "disallowYodaConditions": true, + "disallowKeywords": [ "with" ], + "disallowMultipleLineBreaks": true, + "requireSpaceBeforeBlockStatements": true, + "requireParenthesesAroundIIFE": true, + "requireSpacesInConditionalExpression": true, + "requireBlocksOnNewline": 1, + "requireCommaBeforeLineBreak": true, + "requireSpaceBeforeBinaryOperators": true, + "requireSpaceAfterBinaryOperators": true, + "requireCamelCaseOrUpperCaseIdentifiers": true, + "requireLineFeedAtFileEnd": true, + "requireCapitalizedConstructors": true, + "requireDotNotation": true, + "requireSpacesInForStatement": true, + "requireCurlyBraces": [ + "do" + ], + "requireSpaceAfterKeywords": [ + "if", + "else", + "for", + "while", + "do", + "switch", + "case", + "return", + "try", + "catch", + "typeof" + ], + "validateLineBreaks": "LF", + "validateQuoteMarks": "\"", + "validateIndentation": 2 +} diff --git a/.jshintignore b/.jshintignore index f19c227747..7b9eaccb48 100644 --- a/.jshintignore +++ b/.jshintignore @@ -1,3 +1 @@ -node_modules -test lib/6to5/transformation/templates diff --git a/Makefile b/Makefile index b382a43200..3e9b9610a5 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs #UGLIFY_CMD = node_modules/uglify-js/bin/uglifyjs --mangle sort JSHINT_CMD = node_modules/jshint/bin/jshint MOCHA_CMD = node_modules/mocha/bin/_mocha +JSCS_CMD = node_modules/jscs/bin/jscs export NODE_ENV = test @@ -27,6 +28,7 @@ clean: lint: $(JSHINT_CMD) --reporter node_modules/jshint-stylish/stylish.js lib bin + $(JSCS_CMD) lib bin test-clean: rm -rf test/tmp diff --git a/bin/6to5/file.js b/bin/6to5/file.js index 6a1dd9b7fc..a994cba336 100644 --- a/bin/6to5/file.js +++ b/bin/6to5/file.js @@ -77,7 +77,7 @@ module.exports = function (commander, filenames) { if (chunk !== null) code += chunk; }); - process.stdin.on("end", function() { + process.stdin.on("end", function () { results.push(util.transform(commander.filename, code)); output(); }); @@ -117,7 +117,7 @@ module.exports = function (commander, filenames) { persistent: true, ignoreInitial: true }).on("all", function (type, filename) { - if (type === "add" || type === "change" || type === "unlink" ) { + if (type === "add" || type === "change" || type === "unlink") { console.log(type, filename); walk(); } diff --git a/bin/6to5/index.js b/bin/6to5/index.js index 414102fec9..da2137456a 100755 --- a/bin/6to5/index.js +++ b/bin/6to5/index.js @@ -28,7 +28,7 @@ commander.option("-R, --react-compat", "Makes the react transformer produce pre- commander.option("-E, --include-regenerator", "Include the regenerator runtime if necessary", false); commander.option("--keep-module-id-extensions", "Keep extensions when generating module ids", false); -commander.on("--help", function(){ +commander.on("--help", function () { var outKeys = function (title, obj) { console.log(" " + title + ":"); console.log(); diff --git a/lib/6to5/generation/buffer.js b/lib/6to5/generation/buffer.js index 26e6bcc9aa..3a1bb303bf 100644 --- a/lib/6to5/generation/buffer.js +++ b/lib/6to5/generation/buffer.js @@ -102,13 +102,13 @@ Buffer.prototype._newline = function (removeLast) { * If buffer ends with a newline and some spaces after it, trim those spaces. */ Buffer.prototype._removeSpacesAfterLastNewline = function () { - var lastNewlineIndex = this.buf.lastIndexOf('\n'); + var lastNewlineIndex = this.buf.lastIndexOf("\n"); if (lastNewlineIndex === -1) return; var index = this.buf.length - 1; while (index > lastNewlineIndex) { - if (this.buf[index] !== ' ') { + if (this.buf[index] !== " ") { break; } diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 47e5a998e0..9761b53442 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -40,7 +40,7 @@ _.each(Buffer.prototype, function (fn, key) { CodeGenerator.normaliseOptions = function (code, opts) { var indent = detectIndent(code); var style = indent.indent; - if (!style || style === " ") style = " "; + if (!style || style === " ") style = " "; return _.merge({ parentheses: true, diff --git a/lib/6to5/generation/whitespace.js b/lib/6to5/generation/whitespace.js index 30855472d4..04089795e0 100644 --- a/lib/6to5/generation/whitespace.js +++ b/lib/6to5/generation/whitespace.js @@ -101,7 +101,7 @@ Whitespace.prototype.getNewlinesBetween = function (startToken, endToken) { var lines = 0; for (var line = start; line < end; line++) { - if (typeof this.used[line] === 'undefined') { + if (typeof this.used[line] === "undefined") { this.used[line] = true; lines++; } diff --git a/lib/6to5/transformation/helpers/replace-supers.js b/lib/6to5/transformation/helpers/replace-supers.js index 6ac70166d5..5c70d64c57 100644 --- a/lib/6to5/transformation/helpers/replace-supers.js +++ b/lib/6to5/transformation/helpers/replace-supers.js @@ -8,22 +8,17 @@ var t = require("../../types"); /** * Description * - * @param {Object} methodNode - * @param {Object} className - * @param {Object} superName - * @param {Boolean} isLoose - * @param {Scope} scope - * @param {File} file + * @param {Object} opts */ -function ReplaceSupers(methodNode, className, superName, isLoose, scope, file) { +function ReplaceSupers(opts) { this.topLevelThisReference = null; - this.methodNode = methodNode; - this.className = className; - this.superName = superName; - this.isLoose = isLoose; - this.scope = scope; - this.file = file; + this.methodNode = opts.methodNode; + this.className = opts.className; + this.superName = opts.superName; + this.isLoose = opts.isLoose; + this.scope = opts.scope; + this.file = opts.file; } /** diff --git a/lib/6to5/transformation/templates/async-to-generator.js b/lib/6to5/transformation/templates/async-to-generator.js index f03547d2fe..cb14a12231 100644 --- a/lib/6to5/transformation/templates/async-to-generator.js +++ b/lib/6to5/transformation/templates/async-to-generator.js @@ -2,7 +2,7 @@ return function () { var gen = fn.apply(this, arguments); - return new Promise(function(resolve, reject) { + return new Promise(function (resolve, reject) { var callNext = step.bind(gen.next); var callThrow = step.bind(gen.throw); diff --git a/lib/6to5/transformation/templates/tagged-template-literal.js b/lib/6to5/transformation/templates/tagged-template-literal.js index 5a3a997307..b8c6ac8fe8 100644 --- a/lib/6to5/transformation/templates/tagged-template-literal.js +++ b/lib/6to5/transformation/templates/tagged-template-literal.js @@ -3,4 +3,3 @@ raw: { value: Object.freeze(raw) } })); }); - diff --git a/lib/6to5/transformation/transformers/es6/block-scoping.js b/lib/6to5/transformation/transformers/es6/block-scoping.js index 097d821324..ebcd994a99 100644 --- a/lib/6to5/transformation/transformers/es6/block-scoping.js +++ b/lib/6to5/transformation/transformers/es6/block-scoping.js @@ -246,10 +246,10 @@ var letReferenceFunctionVisitor = { var letReferenceBlockVisitor = { enter: function (node, parent, scope, context, state) { - if (t.isFunction(node)) { - traverse(node, letReferenceFunctionVisitor, scope, state); - return context.skip(); - } + if (t.isFunction(node)) { + traverse(node, letReferenceFunctionVisitor, scope, state); + return context.skip(); + } } }; diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index 8a9c5adfcf..db4d8a34b7 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -137,7 +137,14 @@ Class.prototype.buildBody = function () { for (var i = 0; i < classBody.length; i++) { var node = classBody[i]; if (t.isMethodDefinition(node)) { - var replaceSupers = new ReplaceSupers(node, this.className, this.superName, this.isLoose, this.scope, this.file); + var replaceSupers = new ReplaceSupers({ + methodNode: node, + className: this.className, + superName: this.superName, + isLoose: this.isLoose, + scope: this.scope, + file: this.file + }); replaceSupers.replace(); if (node.key.name === "constructor") { diff --git a/lib/6to5/transformation/transformers/es6/constants.js b/lib/6to5/transformation/transformers/es6/constants.js index a92dfce39a..86aad9ebc5 100644 --- a/lib/6to5/transformation/transformers/es6/constants.js +++ b/lib/6to5/transformation/transformers/es6/constants.js @@ -2,7 +2,6 @@ var traverse = require("../../../traverse"); var t = require("../../../types"); -var _ = require("lodash"); var visitor = { enter: function (node, parent, scope, context, state) { diff --git a/lib/6to5/transformation/transformers/es6/destructuring.js b/lib/6to5/transformation/transformers/es6/destructuring.js index b13f188e44..b4148aabf2 100644 --- a/lib/6to5/transformation/transformers/es6/destructuring.js +++ b/lib/6to5/transformation/transformers/es6/destructuring.js @@ -65,24 +65,24 @@ var pushObjectPattern = function (opts, nodes, pattern, parentId) { for (var i = 0; i < pattern.properties.length; i++) { var prop = pattern.properties[i]; if (t.isSpreadProperty(prop)) { - // get all the keys that appear in this object before the current spread - var keys = []; - for (var i2 = 0; i2 < pattern.properties.length; i2++) { - var prop2 = pattern.properties[i2]; + // get all the keys that appear in this object before the current spread + var keys = []; + for (var i2 = 0; i2 < pattern.properties.length; i2++) { + var prop2 = pattern.properties[i2]; - if (i2 >= i) break; - if (t.isSpreadProperty(prop2)) continue; + if (i2 >= i) break; + if (t.isSpreadProperty(prop2)) continue; - var key = prop2.key; - if (t.isIdentifier(key)) { - key = t.literal(prop2.key.name); - } - keys.push(key); + var key = prop2.key; + if (t.isIdentifier(key)) { + key = t.literal(prop2.key.name); } - keys = t.arrayExpression(keys); + keys.push(key); + } + keys = t.arrayExpression(keys); - var value = t.callExpression(opts.file.addHelper("object-without-properties"), [parentId, keys]); - nodes.push(buildVariableAssign(opts, prop.argument, value)); + var value = t.callExpression(opts.file.addHelper("object-without-properties"), [parentId, keys]); + nodes.push(buildVariableAssign(opts, prop.argument, value)); } else { if (t.isLiteral(prop.key)) prop.computed = true; diff --git a/lib/6to5/transformation/transformers/es6/for-of.js b/lib/6to5/transformation/transformers/es6/for-of.js index 858434d256..f8a3779d5a 100644 --- a/lib/6to5/transformation/transformers/es6/for-of.js +++ b/lib/6to5/transformation/transformers/es6/for-of.js @@ -19,7 +19,7 @@ exports.ForOfStatement = function (node, parent, scope, context, file) { t.ensureBlock(node); // add the value declaration to the new loop body - if (declar){ + if (declar) { if (build.shouldUnshift) { block.body.unshift(declar); } else { diff --git a/lib/6to5/transformation/transformers/es6/spread.js b/lib/6to5/transformation/transformers/es6/spread.js index 1c503cc2b5..f0389e8d8f 100644 --- a/lib/6to5/transformation/transformers/es6/spread.js +++ b/lib/6to5/transformation/transformers/es6/spread.js @@ -66,7 +66,7 @@ exports.CallExpression = function (node, parent, scope, context, file) { node.arguments = []; var nodes; - if (args.length === 1 && args[0].argument.name === 'arguments') { + if (args.length === 1 && args[0].argument.name === "arguments") { nodes = [args[0].argument]; } else { nodes = build(args, file); diff --git a/lib/6to5/transformation/transformers/other/react.js b/lib/6to5/transformation/transformers/other/react.js index 2ecaa07523..5d1e8fec6e 100644 --- a/lib/6to5/transformation/transformers/other/react.js +++ b/lib/6to5/transformation/transformers/other/react.js @@ -39,7 +39,7 @@ exports.XJSAttribute = { } }; -var isCompatTag = function(tagName) { +var isCompatTag = function (tagName) { return /^[a-z]|\-/.test(tagName); }; diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 8e719cf369..a485af8248 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -77,16 +77,18 @@ t.is = function (type, node, opts, skipAliasCheck) { if (!typeMatches && !skipAliasCheck) { var aliases = t.FLIPPED_ALIAS_KEYS[type]; - if (typeof aliases !== 'undefined') + if (typeof aliases !== "undefined") { typeMatches = aliases.indexOf(node.type) > -1; + } } if (!typeMatches) { return false; } - if (typeof opts !== 'undefined') + if (typeof opts !== "undefined") { return t.shallowEqual(node, opts); + } return true; }; @@ -316,7 +318,7 @@ t.toIdentifier = function (name) { name = "_" + name; } - return name || '_'; + return name || "_"; }; /** diff --git a/package.json b/package.json index 1b0e4b7e72..1f04f67ed3 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,7 @@ "browserify": "8.1.1", "chai": "1.10.0", "istanbul": "0.3.5", + "jscs": "^1.10.0", "jshint-stylish": "1.0.0", "matcha": "0.6.0", "mocha": "2.1.0",