Skip to content

Commit

Permalink
[REF] javascript-lint: Use eslint instead of jshint (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
moylop260 committed Jan 26, 2017
1 parent 58dc5ae commit 527c992
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 21 deletions.
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ addons:
apt:
packages:
- python-lxml # because pip installation is slow
- nodejs

virtualenv:
system_site_packages: true
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -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 .
14 changes: 13 additions & 1 deletion pylint_odoo/checkers/modules_odoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -186,6 +190,14 @@ class ModuleChecker(misc.WrapperModuleChecker):
'help': 'List of known import dependencies of odoo,'
' separated by a comma.'
}),
('jslintrc', {
'type': 'string',
'metavar': '<path to file>',
'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 = []
Expand Down Expand Up @@ -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:
Expand Down
247 changes: 247 additions & 0 deletions pylint_odoo/examples/.jslintrc
Original file line number Diff line number Diff line change
@@ -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": {}
}
55 changes: 44 additions & 11 deletions pylint_odoo/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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:
Expand Down
Loading

0 comments on commit 527c992

Please sign in to comment.