diff --git a/.eslintrc b/.eslintrc index ee207616c..6fba462fb 100644 --- a/.eslintrc +++ b/.eslintrc @@ -4,14 +4,54 @@ "node": true }, "rules": { - "no-bitwise": 2, - "camelcase": 2, + // Strict Mode + // @see http://eslint.org/docs/rules/strict + "strict": [ + 2, + "safe" + ], + + // Variables + // @see http://eslint.org/docs/rules/#variables + "no-undef": 2, + "no-unused-vars": 2, + + // Possible Errors + // @see http://eslint.org/docs/rules/#possible-errors + "comma-dangle": [2, "never"], + "no-empty": [2, {"allowEmptyCatch": true}], + + // Best Practices + // @see http://eslint.org/docs/rules/#best-practices "curly": 2, - "no-unused-expressions": 2, + "dot-notation": 2, + "no-caller": 2, + "no-multi-str": 2, + "no-unused-expressions": [2, { + "allowShortCircuit": true, + "allowTernary": true + }], + "no-with": 2, "wrap-iife": [ 2, "any" ], + + // Stylistic Issues + // @see http://eslint.org/docs/rules/#stylistic-issues + "array-bracket-spacing": [2, "never"], + "camelcase": [2, { + "properties": "always" + }], + "comma-style": [2, "last"], + "eol-last": 2, + "keyword-spacing": [ + 2, + { + "before": true, + "after": true, + "overrides": {}} + ], "indent": [ 2, 2, @@ -19,6 +59,7 @@ "SwitchCase": 1 } ], + "linebreak-style": [2, "unix"], "max-len": [ 2, 80, @@ -28,16 +69,31 @@ } ], "new-cap": 2, - "no-caller": 2, + "no-bitwise": 2, + "no-mixed-spaces-and-tabs": 2, + "no-trailing-spaces": [2, {"skipBlankLines": true }], + "one-var": [2, "never"], "quotes": [ 2, - "single" + "single", + {"avoidEscape": true, "allowTemplateLiterals": true} ], - "no-undef": 2, - "no-unused-vars": 2, - "strict": [ - 2, - "safe" - ] + "quote-props": [2, "as-needed"], + "semi": ["error", "always"], + "spaced-comment": ["error", "always"], + "space-before-function-paren": [2, { + "anonymous": "never", + "named": "never" + }], + "space-in-parens": [2, "never"], + "space-infix-ops": [2, {"int32Hint": true}], + "space-unary-ops": [ + 2, { + // `words` applies to unary word operators, + // such as: new, delete, typeof, void, yield + "words": true, + // nonwords - applies to unary operators such as: -, +, --, ++, !, !! + "nonwords": false + }] } } diff --git a/.jscsrc b/.jscsrc deleted file mode 100644 index b1f8d3ef2..000000000 --- a/.jscsrc +++ /dev/null @@ -1,95 +0,0 @@ -{ - "preset": "google", - "disallowEmptyBlocks": true, - "disallowKeywords": ["with"], - "disallowMixedSpacesAndTabs": true, - "disallowMultipleLineStrings": true, - "disallowMultipleVarDecl": true, - "disallowQuotedKeysInObjects": "allButReserved", - "disallowSpaceAfterPrefixUnaryOperators": [ - "++", - "--", - "+", - "-", - "~", - "!" - ], - "disallowSpaceBeforeBinaryOperators": [","], - "disallowSpaceBeforePostfixUnaryOperators": [ - "++", - "--" - ], - "disallowSpacesInNamedFunctionExpression": { - "beforeOpeningRoundBrace": true - }, - "disallowSpacesInsideArrayBrackets": true, - "disallowSpacesInsideParentheses": true, - "disallowTrailingComma": true, - "disallowTrailingWhitespace": true, - "requireCamelCaseOrUpperCaseIdentifiers": true, - "requireCapitalizedConstructors": true, - "requireCommaBeforeLineBreak": true, - "requireDotNotation": true, - "requireLineFeedAtFileEnd": true, - "requireSpaceAfterBinaryOperators": [ - "+", - "-", - "/", - "*", - "=", - "==", - "===", - "!=", - "!==", - ">", - "<", - ">=", - "<=" - ], - "requireSpaceAfterKeywords": [ - "if", - "else", - "for", - "while", - "do", - "switch", - "return", - "try", - "catch", - "case", - "void" - ], - "requireSpaceAfterLineComment": true, - "requireSpaceBeforeBinaryOperators": [ - "+", - "-", - "/", - "*", - "=", - "==", - "===", - "!=", - "!==", - ">", - "<", - ">=", - "<=" - ], - "requireSpacesInAnonymousFunctionExpression": { - "beforeOpeningCurlyBrace": true - }, - "requireSpacesInConditionalExpression": true, - "requireSpacesInFunctionDeclaration": { - "beforeOpeningCurlyBrace": true - }, - "requireSpacesInFunctionExpression": { - "beforeOpeningCurlyBrace": true - }, - "requireSpacesInNamedFunctionExpression": { - "beforeOpeningCurlyBrace": true - }, - "validateIndentation": 2, - "validateLineBreaks": "LF", - "validateQuoteMarks": "'", - "excludeFiles": ["node_modules/**"] -} \ No newline at end of file diff --git a/docs/styleguide/javascript.md b/docs/styleguide/javascript.md index c3eddc58e..5f44d4862 100644 --- a/docs/styleguide/javascript.md +++ b/docs/styleguide/javascript.md @@ -17,9 +17,11 @@ next: widget.html ## 代码质量控制工具 -Amaze UI 使用 [JSHint](http://jshint.com/) 和 [JSCS](https://github.com/jscs-dev/node-jscs) 控制代码质量。 +Amaze UI 使用 ~~[JSHint](http://jshint.com/) 和 [JSCS](https://github.com/jscs-dev/node-jscs)~~[ESLint](http://eslint.org/)控制代码质量。 -详细设置参见 [.jshintrc](https://github.com/allmobilize/amazeui/blob/master/.jshintrc)、[.jscsrc](https://github.com/allmobilize/amazeui/blob/master/.jscsrc)。 +详细设置参见 ~~[.jshintrc](https://github.com/amazeui/amazeui/blob/master/.jshintrc)、[.jscsrc](https://github.com/amazeui/amazeui/blob/master/.jscsrc)~~[.eslintrc](https://github.com/amazeui/amazeui/blob/master/.eslintrc)。 + +> 2016.04.20 替换为 ESLint,参见 [Welcoming JSCS to ESLint](http://eslint.org/blog/2016/04/welcoming-jscs-to-eslint) (部分直接使用第三方库的代码未通过质量控制工具检测。) diff --git a/gulpfile.js b/gulpfile.js index 5be3b930a..5bf19d315 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -63,7 +63,9 @@ var config = { warnings: false }, output: { + /* eslint-disable camelcase */ ascii_only: true + /* eslint-enable camelcase */ } } }; diff --git a/js/core.js b/js/core.js index 2766525a2..ac4cfe637 100644 --- a/js/core.js +++ b/js/core.js @@ -58,15 +58,17 @@ UI.support.animation = (function() { return animationEnd && {end: animationEnd}; })(); +/* eslint-disable dot-notation */ UI.support.touch = ( ('ontouchstart' in window && navigator.userAgent.toLowerCase().match(/mobile|tablet/)) || (window.DocumentTouch && document instanceof window.DocumentTouch) || (window.navigator['msPointerEnabled'] && -window.navigator['msMaxTouchPoints'] > 0) || //IE 10 +window.navigator['msMaxTouchPoints'] > 0) || // IE 10 (window.navigator['pointerEnabled'] && -window.navigator['maxTouchPoints'] > 0) || //IE >=11 +window.navigator['maxTouchPoints'] > 0) || // IE >=11 false); +/* eslint-enable dot-notation */ // https://developer.mozilla.org/zh-CN/docs/DOM/MutationObserver UI.support.mutationobserver = (window.MutationObserver || @@ -428,6 +430,7 @@ UI.template = function(id, data) { me.get(id) : (name = 'template(string)', id); // no warnings var line = 1; + /* eslint-disable max-len, quotes */ var body = ('try { ' + (me.variable ? 'var ' + me.variable + ' = this.stash;' : 'with (this.stash) { ') + "this.ret += '" + @@ -446,6 +449,7 @@ UI.template = function(id, data) { "' + ' line ' + this.line + ')'; } " + "//@ sourceURL=" + name + "\n" // source map ).replace(/this\.ret \+= '';/g, ''); + /* eslint-enable max-len, quotes */ var func = new Function(body); var map = { '&': '&', @@ -517,10 +521,10 @@ UI.DOMObserve = function(elements, options, callback) { try { var observer = new Observer(UI.utils.debounce( function(mutations, instance) { - callback.call(element, mutations, instance); - // trigger this event manually if MutationObserver not supported - $element.trigger('changed.dom.amui'); - }, 50)); + callback.call(element, mutations, instance); + // trigger this event manually if MutationObserver not supported + $element.trigger('changed.dom.amui'); + }, 50)); observer.observe(element, options); @@ -532,7 +536,9 @@ UI.DOMObserve = function(elements, options, callback) { $.fn.DOMObserve = function(options, callback) { return this.each(function() { + /* eslint-disable new-cap */ UI.DOMObserve(this, options, callback); + /* eslint-enable new-cap */ }); }; @@ -564,7 +570,9 @@ $(function() { }); // watches DOM + /* eslint-disable new-cap */ UI.DOMObserve('[data-am-observe]'); + /* eslint-enable */ $html.removeClass('no-js').addClass('js');