diff --git a/.travis.yml b/.travis.yml index 1501bf7e..01abbcaf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,6 @@ addons: apt: packages: - python-lxml # because pip installation is slow - - nodejs virtualenv: system_site_packages: true @@ -25,9 +24,11 @@ matrix: TOXENV=py27-pylint20 install: + - nvm install 6 # Update nodejs version to 6.latest, required by eslint # Remove packages installed from addons-apt-packages of travis file - - find -L ${TRAVIS_BUILD_DIR} -name requirements.txt -exec sed -i '/lxml/d' {} \; - - find -L ${TRAVIS_BUILD_DIR} -name install.sh -exec sed -i '/node/d' {} \; + - sed -i '/lxml/d' ${TRAVIS_BUILD_DIR}/requirements.txt + - sed -i '/node/d' ${TRAVIS_BUILD_DIR}/install.sh + - sed -i '/pip install ./d' ${TRAVIS_BUILD_DIR}/install.sh # Install dependencies - ${TRAVIS_BUILD_DIR}/install.sh diff --git a/install.sh b/install.sh index dac9e26a..e8df05a6 100755 --- a/install.sh +++ b/install.sh @@ -1,4 +1,4 @@ wget -qO- https://deb.nodesource.com/setup | bash - \ && apt-get install nodejs -npm install -g jshint +npm install -g eslint pip install . diff --git a/pylint_odoo/checkers/modules_odoo.py b/pylint_odoo/checkers/modules_odoo.py index 10825235..6166ee2e 100644 --- a/pylint_odoo/checkers/modules_odoo.py +++ b/pylint_odoo/checkers/modules_odoo.py @@ -148,6 +148,10 @@ 'unittest2', 'usb', 'vatnumber', 'vobject', 'werkzeug', 'wsgiref', 'xlsxwriter', 'xlwt', 'yaml', ] +DFTL_JSLINTRC = os.path.join( + os.path.dirname(os.path.dirname(os.path.realpath(__file__))), + 'examples', '.jslintrc' +) class ModuleChecker(misc.WrapperModuleChecker): @@ -186,6 +190,14 @@ class ModuleChecker(misc.WrapperModuleChecker): 'help': 'List of known import dependencies of odoo,' ' separated by a comma.' }), + ('jslintrc', { + 'type': 'string', + 'metavar': '', + 'default': os.environ.get('PYLINT_ODOO_JSLINTRC') or DFTL_JSLINTRC, + 'help': ('A path to a file that contains a configuration file of ' + 'javascript lint. You can use the environment variable ' + '"PYLINT_ODOO_JSLINTRC" too. Default: %s' % DFTL_JSLINTRC) + }), ) class_inherit_names = [] @@ -617,7 +629,7 @@ def _check_javascript_lint(self): self.msg_args = [] for js_file_rel in self.filter_files_ext('js', relpath=True): js_file = os.path.join(self.module_path, js_file_rel) - errors = self.check_js_lint(js_file) + errors = self.check_js_lint(js_file, self.config.jslintrc) for error in errors: self.msg_args.append((js_file_rel + error,)) if self.msg_args: diff --git a/pylint_odoo/examples/.jslintrc b/pylint_odoo/examples/.jslintrc new file mode 100644 index 00000000..b58dbfb3 --- /dev/null +++ b/pylint_odoo/examples/.jslintrc @@ -0,0 +1,247 @@ +{ + "globals": { + "$": false, + "_": false, + "jQuery": false, + "odoo": false, + "openerp": false, + "self": false + }, + "env": { + "browser": true + }, + "rules": { + "no-alert": "error", + "no-array-constructor": "error", + "no-bitwise": "error", + "no-caller": "error", + "no-case-declarations": "error", + "no-catch-shadow": "error", + "no-class-assign": "error", + "no-cond-assign": "error", + "no-confusing-arrow": "error", + "no-console": "error", + "no-const-assign": "error", + "no-constant-condition": "error", + "no-continue": "error", + "no-control-regex": "error", + "no-debugger": "error", + "no-delete-var": "error", + "no-div-regex": "error", + "no-dupe-args": "error", + "no-dupe-class-members": "error", + "no-dupe-keys": "error", + "no-duplicate-case": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-empty": "error", + "no-empty-character-class": "error", + "no-empty-function": "error", + "no-empty-pattern": "error", + "no-eq-null": "error", + "no-eval": "error", + "no-ex-assign": "error", + "no-extend-native": "error", + "no-extra-bind": "error", + "no-extra-boolean-cast": "error", + "no-extra-label": "error", + "no-extra-parens": "error", + "no-extra-semi": "error", + "no-fallthrough": "error", + "no-floating-decimal": "error", + "no-func-assign": "error", + "no-implicit-coercion": "error", + "no-implicit-globals": "error", + "no-implied-eval": "error", + "no-inline-comments": "error", + "no-inner-declarations": "error", + "no-invalid-regexp": "error", + "no-invalid-this": "off", + "no-irregular-whitespace": "error", + "no-iterator": "error", + "no-label-var": "error", + "no-labels": "error", + "no-lone-blocks": "error", + "no-lonely-if": "error", + "no-loop-func": "off", + "no-magic-numbers": "off", + "no-mixed-operators": "error", + "no-mixed-requires": "error", + "no-mixed-spaces-and-tabs": "error", + "no-multi-spaces": "error", + "no-multi-str": "error", + "no-multiple-empty-lines": "error", + "no-native-reassign": "error", + "no-negated-condition": "error", + "no-negated-in-lhs": "error", + "no-nested-ternary": "off", + "no-new": "error", + "no-new-func": "error", + "no-new-object": "error", + "no-new-require": "error", + "no-new-symbol": "error", + "no-new-wrappers": "error", + "no-obj-calls": "error", + "no-octal": "error", + "no-octal-escape": "error", + "no-param-reassign": "error", + "no-path-concat": "error", + "no-plusplus": "off", + "no-process-env": "error", + "no-process-exit": "error", + "no-proto": "error", + "no-prototype-builtins": "error", + "no-redeclare": "error", + "no-regex-spaces": "error", + "no-restricted-globals": "error", + "no-restricted-imports": "error", + "no-restricted-modules": "error", + "no-restricted-syntax": "error", + "no-return-assign": "error", + "no-script-url": "error", + "no-self-assign": "error", + "no-self-compare": "error", + "no-sequences": "error", + "no-shadow": "error", + "no-shadow-restricted-names": "error", + "no-whitespace-before-property": "error", + "no-spaced-func": "error", + "no-sparse-arrays": "error", + "no-sync": "error", + "no-tabs": "error", + "no-ternary": "off", + "no-trailing-spaces": "error", + "no-this-before-super": "error", + "no-throw-literal": "error", + "no-undef": "error", + "no-undef-init": "error", + "no-undefined": "error", + "no-unexpected-multiline": "error", + "no-underscore-dangle": "off", + "no-unmodified-loop-condition": "error", + "no-unneeded-ternary": "error", + "no-unreachable": "error", + "no-unsafe-finally": "error", + "no-unused-expressions": "error", + "no-unused-labels": "error", + "no-unused-vars": "off", + "no-use-before-define": "error", + "no-useless-call": "error", + "no-useless-computed-key": "error", + "no-useless-concat": "error", + "no-useless-constructor": "error", + "no-useless-escape": "error", + "no-useless-rename": "error", + "no-void": "error", + "no-var": "off", + "no-warning-comments": "off", + "no-with": "error", + "array-bracket-spacing": "off", + "array-callback-return": "error", + "arrow-body-style": "error", + "arrow-parens": "error", + "arrow-spacing": "off", + "accessor-pairs": "error", + "block-scoped-var": "off", + "block-spacing": "off", + "brace-style": "error", + "callback-return": "error", + "camelcase": "off", + "comma-dangle": "off", + "comma-spacing": "off", + "comma-style": "error", + "complexity": [ + "error", + 15 + ], + "computed-property-spacing": "off", + "consistent-return": "off", + "consistent-this": "off", + "constructor-super": "error", + "curly": "error", + "default-case": "off", + "dot-location": "error", + "dot-notation": "error", + "eol-last": "error", + "eqeqeq": "error", + "func-names": "off", + "func-style": "off", + "generator-star-spacing": "off", + "global-require": "error", + "guard-for-in": "error", + "handle-callback-err": "error", + "id-blacklist": "error", + "id-length": "off", + "id-match": "error", + "indent": "off", + "init-declarations": "error", + "jsx-quotes": "error", + "key-spacing": "off", + "keyword-spacing": "off", + "linebreak-style": [ + "error", + "unix" + ], + "lines-around-comment": "off", + "max-depth": "error", + "max-len": "off", + "max-lines": "off", + "max-nested-callbacks": "error", + "max-params": "off", + "max-statements": "off", + "max-statements-per-line": "error", + "multiline-ternary": "error", + "new-cap": "off", + "new-parens": "error", + "newline-after-var": "off", + "newline-before-return": "off", + "newline-per-chained-call": "off", + "object-curly-newline": "off", + "object-curly-spacing": "off", + "object-property-newline": "off", + "object-shorthand": "off", + "one-var": "off", + "one-var-declaration-per-line": "off", + "operator-assignment": "error", + "operator-linebreak": "error", + "padded-blocks": "off", + "prefer-arrow-callback": "off", + "prefer-const": "error", + "prefer-reflect": "error", + "prefer-rest-params": "error", + "prefer-spread": "error", + "prefer-template": "off", + "quote-props": "off", + "quotes": "off", + "radix": "error", + "require-jsdoc": "off", + "require-yield": "error", + "rest-spread-spacing": "off", + "semi": [ + "error", + "always" + ], + "semi-spacing": "off", + "sort-imports": "off", + "sort-vars": "off", + "space-before-blocks": "off", + "space-before-function-paren": "off", + "space-in-parens": "off", + "space-infix-ops": "off", + "space-unary-ops": "off", + "spaced-comment": "off", + "strict": "off", + "template-curly-spacing": "off", + "unicode-bom": "error", + "use-isnan": "error", + "valid-jsdoc": "error", + "valid-typeof": "error", + "vars-on-top": "off", + "wrap-iife": "error", + "wrap-regex": "error", + "yield-star-spacing": "off", + "yoda": "error" + }, + "parserOptions": {}, + "ecmaFeatures": {} +} diff --git a/pylint_odoo/misc.py b/pylint_odoo/misc.py index 76d113ff..9ec2ad91 100644 --- a/pylint_odoo/misc.py +++ b/pylint_odoo/misc.py @@ -12,6 +12,11 @@ from . import settings +try: + from shutil import which # python3.x +except ImportError: + from whichcraft import which + def get_plugin_msgs(pylint_run_res): """Get all message of this pylint plugin. @@ -197,8 +202,10 @@ def filter_files_ext(self, fext, relpath=True, skip_examples=True): """ dirnames_to_skip = [] if skip_examples: - dirnames_to_skip.extend(['example', 'examples', 'sample', - 'samples', 'lib']) + dirnames_to_skip.extend([ + 'example', 'examples', 'sample', 'samples', 'lib', 'libs', + 'doc', 'docs', 'template', 'templates', + ]) if not fext.startswith('.'): fext = '.' + fext fext = fext.lower() @@ -223,19 +230,45 @@ def check_rst_syntax(self, fname): """ return rst_lint(fname) - def check_js_lint(self, fname): + def npm_which_module(self, module): + module_bin = which(module) + npm_bin = which('npm') + if not module_bin and npm_bin: + npm_bin_paths = [] + for cmd in ([npm_bin, 'bin'], [npm_bin, 'bin', '-g']): + process = subprocess.Popen(cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + output, err = process.communicate() + npm_bin_path = output.strip('\n ') + if os.path.isdir(npm_bin_path) and not err: + npm_bin_paths.append(npm_bin_path) + if npm_bin_paths: + module_bin = which(module, path=os.pathsep.join(npm_bin_paths)) + return module_bin + + def check_js_lint(self, fname, frc=None): """Check javascript lint in fname. :param fname: String with full path of file to check + :param frc: String with full path of configuration file for + the javascript-lint tool :return: Return list of errors. """ - cmd = ['jshint', '--reporter=unix', fname] - try: - output = subprocess.Popen( - cmd, stderr=subprocess.STDOUT, - stdout=subprocess.PIPE).stdout.read() - except OSError as oserr: - output_err = ' - ' + cmd[0] + ': ' + oserr.strerror - return [output_err] + lint_bin = self.npm_which_module('eslint') + if not lint_bin: + return [] + cmd = [lint_bin, '--format=unix', fname] + if frc: + cmd.append('--config=' + frc) + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + output, err = process.communicate() + if process.returncode != 0 and err: + return [] + # Strip multi-line output https://github.com/eslint/eslint/issues/6810 + for old in re.findall(r"`(.*)` instead.", output, re.DOTALL): + new = old.split('\n')[0][:20] + '...' + output = output.replace(old, new) output = output.replace(fname, '') output_spplited = [] if output: diff --git a/pylint_odoo/test/main.py b/pylint_odoo/test/main.py index c346c003..bd957fcd 100644 --- a/pylint_odoo/test/main.py +++ b/pylint_odoo/test/main.py @@ -1,6 +1,9 @@ import os +import stat import sys +from tempfile import gettempdir + import unittest from contextlib import contextmanager from cProfile import Profile @@ -26,7 +29,7 @@ 'file-not-used': 6, 'incoherent-interpreter-exec-perm': 3, 'invalid-commit': 4, - 'javascript-lint': 2, + 'javascript-lint': 8, 'license-allowed': 1, 'manifest-author-string': 1, 'manifest-deprecated-key': 1, @@ -39,7 +42,7 @@ 'method-search': 1, 'missing-import-error': 3, 'missing-manifest-dependency': 2, - 'missing-newline-extrafiles': 3, + 'missing-newline-extrafiles': 4, 'missing-readme': 1, 'missing-return': 1, 'no-utf8-coding-comment': 3, @@ -87,7 +90,7 @@ def setUp(self): self.profile = Profile() self.sys_path_origin = list(sys.path) self.maxDiff = None - self.expected_errors = EXPECTED_ERRORS + self.expected_errors = EXPECTED_ERRORS.copy() def tearDown(self): sys.path = list(self.sys_path_origin) @@ -162,7 +165,7 @@ def test_50_ignore(self): self.assertEqual(real_errors.items(), [('deprecated-openerp-xml-node', 4)]) - def test_60_ignore_patternls(self): + def test_60_ignore_patterns(self): """Test --ignore-patterns parameter """ extra_params = ['--ignore-patterns=' '.*\/test_module\/*\/.*xml$', @@ -173,6 +176,44 @@ def test_60_ignore_patternls(self): self.assertEqual(real_errors.items(), [('deprecated-openerp-xml-node', 3)]) + def test_70_without_jslint_installed(self): + """Test without jslint installed""" + # if not self.jslint_bin_content: + # return + # TODO: Use mock to create a monkey patch + which_original = misc.which + + def my_which(bin_name, *args, **kwargs): + if bin_name == 'eslint': + return None + return which_original(bin_name) + misc.which = my_which + my_which("noeslint") + pylint_res = self.run_pylint(self.paths_modules) + misc.which = which_original + real_errors = pylint_res.linter.stats['by_msg'] + self.expected_errors.pop('javascript-lint') + self.assertEqual(self.expected_errors, real_errors) + + def test_80_with_jslint_error(self): + """Test with jslint error""" + # TODO: Use mock to create a monkey patch + which_original = misc.which + + def my_which(bin_name, *args, **kwargs): + fname = os.path.join(gettempdir(), 'jslint.bad') + with open(fname, "w") as f_jslint: + f_jslint.write("#!/usr/bin/env node\n{}}") + os.chmod(fname, os.stat(fname).st_mode | stat.S_IEXEC) + return fname + + misc.which = my_which + pylint_res = self.run_pylint(self.paths_modules) + misc.which = which_original + real_errors = pylint_res.linter.stats['by_msg'] + self.expected_errors.pop('javascript-lint') + self.assertEqual(self.expected_errors, real_errors) + if __name__ == '__main__': unittest.main() diff --git a/pylint_odoo/test_repo/broken_module/broken_example.js b/pylint_odoo/test_repo/broken_module/broken_example.js index a09b3c93..98e119b1 100644 --- a/pylint_odoo/test_repo/broken_module/broken_example.js +++ b/pylint_odoo/test_repo/broken_module/broken_example.js @@ -2,4 +2,6 @@ $(document).ready(function () { $('.example').each(function () { var oe_website_sale = this; }) /*missing semicolon*/ + /*Use of console log*/ + console.log("This is similar to a print"); }) /*missing semicolon*/ diff --git a/pylint_odoo/test_repo/broken_module/broken_example2.js b/pylint_odoo/test_repo/broken_module/broken_example2.js new file mode 100644 index 00000000..eb2c3286 --- /dev/null +++ b/pylint_odoo/test_repo/broken_module/broken_example2.js @@ -0,0 +1,7 @@ +/*Use of "+function" instead of "Number(function" */ ++function ($) { + 'use strict'; + var var_1 = "value1"; + var var_2 = "value2"; +}; +/* Newline required at end of file but not found */ \ No newline at end of file diff --git a/pylint_odoo/test_repo/broken_module/rst_syntax.rst b/pylint_odoo/test_repo/broken_module/rst_syntax.rst new file mode 100644 index 00000000..aceca33f --- /dev/null +++ b/pylint_odoo/test_repo/broken_module/rst_syntax.rst @@ -0,0 +1,6 @@ + Module broken +====================== + + +`````````` +syntax error diff --git a/requirements.txt b/requirements.txt index ee61925c..449b4ca0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,4 @@ lxml>=2.3.2 Pygments==2.0.2 restructuredtext_lint==0.12.2 isort==4.2.5 - +whichcraft diff --git a/tox.ini b/tox.ini index 69815c4a..da31f7e1 100644 --- a/tox.ini +++ b/tox.ini @@ -12,6 +12,7 @@ deps = isort==4.2.5 # Test deps coveralls + whichcraft [testenv] deps = @@ -22,8 +23,13 @@ deps = {[base]deps} setenv = PYLINT_ODOO_STATS = {toxinidir}/.cprofile_{envname} +passenv = + * +whitelist_externals = + npm commands = + npm install eslint coverage run setup.py test coverage report -m