diff --git a/.pylintrc b/.pylintrc index f19d80ad..b8d1f6b1 100644 --- a/.pylintrc +++ b/.pylintrc @@ -2,11 +2,11 @@ ignore=.cache,.git,.idea,__pycache__,messages,screenshots [MESSAGES CONTROL] -disable=missing-docstring,too-many-branches,too-many-public-methods,unidiomatic-typecheck,len-as-condition,too-many-return-statements,too-many-statements,fixme,too-many-locals,too-many-arguments,invalid-name,inconsistent-return-statements,too-many-lines,relative-beyond-top-level,too-many-nested-blocks,no-self-use,too-few-public-methods,consider-using-in,no-else-continue,consider-using-with +disable=missing-docstring,too-many-branches,too-many-public-methods,unidiomatic-typecheck,len-as-condition,too-many-return-statements,too-many-statements,fixme,too-many-locals,too-many-arguments,too-many-positional-arguments,invalid-name,inconsistent-return-statements,too-many-lines,relative-beyond-top-level,too-many-nested-blocks,too-few-public-methods,consider-using-in,no-else-continue,no-else-return,consider-using-with,consider-using-f-string,useless-object-inheritance,cyclic-import,too-many-boolean-expressions,import-outside-toplevel,redundant-u-string-prefix,use-yield-from [FORMAT] max-line-length=120 expected-line-ending-format=LF [TYPECHECK] -ignored-modules=sublime,sublime_plugin +ignored-modules=sublime,sublime_plugin,jsprettier.const,jsprettier.sthelper,jsprettier.util diff --git a/JsPrettier.py b/JsPrettier.py index 60040c86..54ba9c9f 100644 --- a/JsPrettier.py +++ b/JsPrettier.py @@ -474,29 +474,20 @@ def should_show_plugin(self): return False if self.allow_inline_formatting is True: return True - if self.is_source_js(view) is True: - return True - if self.is_less(view) is True: - return True - if self.is_scss(view) is True: - return True - if self.is_css(view) is True: - return True - if self.is_angular_html(view) is True: - return True - if self.is_mdx(view) is True: - return True - if self.is_markdown(view) is True: - return True - if self.is_yaml(view) is True: - return True - if self.is_php(view) is True: - return True - if self.is_html(view) is True: - return True - if is_file_auto_formattable(view) is True: - return True - return False + is_supported_syntax = ( + self.is_source_js, + self.is_less, + self.is_scss, + self.is_css, + self.is_angular_html, + self.is_mdx, + self.is_markdown, + self.is_yaml, + self.is_php, + self.is_html, + is_file_auto_formattable, + ) + return any(check(view) for check in is_supported_syntax) def is_visible(self): return self.should_show_plugin() diff --git a/jsprettier/sthelper.py b/jsprettier/sthelper.py index 8dea6fc6..7236932d 100644 --- a/jsprettier/sthelper.py +++ b/jsprettier/sthelper.py @@ -109,18 +109,17 @@ def get_st_project_path(): folders = window.folders() if len(folders) == 1: return folders[0] + active_view = window.active_view() + if active_view: + active_file_name = active_view.file_name() else: - active_view = window.active_view() - if active_view: - active_file_name = active_view.file_name() - else: - active_file_name = None - if not active_file_name: - return folders[0] if len(folders) else os.path.expanduser('~') - for folder in folders: - if active_file_name.startswith(folder): - return folder - return os.path.dirname(active_file_name) + active_file_name = None + if not active_file_name: + return folders[0] if len(folders) else os.path.expanduser('~') + for folder in folders: + if active_file_name.startswith(folder): + return folder + return os.path.dirname(active_file_name) def scroll_view_to(view, row_no, col_no): diff --git a/jsprettier/util.py b/jsprettier/util.py index f3de9d73..e1fc1fce 100644 --- a/jsprettier/util.py +++ b/jsprettier/util.py @@ -12,17 +12,12 @@ from re import sub -from .const import IS_PY2 from .const import PLUGIN_NAME from .const import PRETTIER_CONFIG_FILES from .const import PRETTIER_IGNORE_FILE -if IS_PY2: - text_type = unicode - string_types = (str, unicode) -else: - text_type = str - string_types = (str,) +text_type = type(u'') +string_types = (str, text_type) def is_windows(): @@ -139,7 +134,7 @@ def _prettier_opts_in_package_json(package_json_file): try: with open(package_json_file) as package_file: json_data = json.load(package_file) - except Exception: + except (IOError, ValueError): from .sthelper import log_warn log_warn("Could not parse package.json file: '{0}'. Any Prettier options " "defined in this file will be ignored.".format(package_json_file), True) @@ -387,7 +382,7 @@ def format_error_message(error_message, error_code): return '\nPrettier produced the following output:\n\n' \ '{0}\n' \ '\nPrettier process finished with exit code {1}.\n' \ - .format(error_message, '{0}'.format(error_code)) + .format(error_message, error_code) def format_debug_message(label, message, debug_enabled=False): @@ -395,8 +390,7 @@ def format_debug_message(label, message, debug_enabled=False): return header = ' {0} DEBUG - {1} '.format(PLUGIN_NAME, label) horizontal_rule = repeat_str('-', len(header)) - print('\n{0}\n{1}\n{2}\n\n''{3}'.format( - horizontal_rule, header, horizontal_rule, message)) + print('\n{0}\n{1}\n{0}\n\n{2}'.format(horizontal_rule, header, message)) def get_cli_arg_value(additional_cli_args, arg_key, arg_val_can_be_empty=False, default=None): diff --git a/requirements.txt b/requirements.txt index 32451c7c..16fdd54d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ flake8 pep8-naming pytest>=9.0.3 -pylint==2.13.9 +pylint==4.0.9 diff --git a/tests/validate_json_format.py b/tests/validate_json_format.py index 9f3639b0..2a81d4b2 100644 --- a/tests/validate_json_format.py +++ b/tests/validate_json_format.py @@ -1,4 +1,4 @@ -import codecs +import io import json import re @@ -117,8 +117,7 @@ def evaluate(m): txt = g["code"] return txt - content = ''.join( - map(lambda m: evaluate(m), RE_COMMENT.finditer(text))) + content = ''.join(evaluate(m) for m in RE_COMMENT.finditer(text)) return content def check_dangling_commas(self, text): @@ -131,16 +130,14 @@ def check_comma(g, line): self.log_failure(E_COMMA, line) if g["square_comma"] is not None: return g["square_ws"] + g["square_bracket"] - else: - return g["curly_ws"] + g["curly_bracket"] + return g["curly_ws"] + g["curly_bracket"] def evaluate(m): g = m.groupdict() return check_comma(g, self.get_line(m.start(0))) \ if g["code"] is None else g["code"] - return ''.join( - map(lambda m: evaluate(m), RE_TRAILING_COMMA.finditer(text))) + return ''.join(evaluate(m) for m in RE_TRAILING_COMMA.finditer(text)) def log_failure(self, code, line=None): """Log failure. @@ -156,7 +153,7 @@ def log_failure(self, code, line=None): def check_format(self, file_name): """Initiate the check.""" self.fail = False - with codecs.open(file_name, encoding='utf-8') as f: + with io.open(file_name, encoding='utf-8') as f: count = 1 for line in f: if count == 1 and line.strip() == '': @@ -179,7 +176,7 @@ def check_format(self, file_name): text = self.check_dangling_commas(text) try: json.loads(text) - except Exception as e: + except ValueError as e: self.log_failure(E_MALFORMED) print(e) return self.fail