From 1574a9df55a6e6c07c09c7cdb3a2c45be10dc7bb Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:02:49 -0700 Subject: [PATCH 01/23] Added a script to create rule --- .idea/.gitignore | 8 ++ .idea/Fixit.iml | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 17 ++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ fixit/cli/add_new_rule.py | 84 +++++++++++++++++++ 8 files changed, 144 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Fixit.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 fixit/cli/add_new_rule.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/Fixit.iml b/.idea/Fixit.iml new file mode 100644 index 00000000..c444878a --- /dev/null +++ b/.idea/Fixit.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..d2e4bbdd --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,17 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..86561143 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..1a140847 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py new file mode 100644 index 00000000..9f159102 --- /dev/null +++ b/fixit/cli/add_new_rule.py @@ -0,0 +1,84 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +import argparse +from pathlib import Path +from typing import Union + + +""" +Usage: + $ python -m fixit.cli.add_new_rule --help + $ python -m fixit.cli.add_new_rule + $ python -m fixit.cli.add_new_rule --path fixit/rules/new_rule.py + +""" + +_LICENCE = """\ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +""" + +_IMPORTS = """\ +import libcst as cst +from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid + +""" + +_RULE_CLASS = """\ +class Rule(CstLintRule): + \""" + docstring or new_rule description + \""" + MESSAGE = 'Enter rule description message' + + VALID = [Valid()] + + INVALID = [Invalid()] + +""" + + +def is_path_exists(path: str) -> Union[Path, TypeError, FileExistsError]: + """Check for valid path, if yes, return `Path` else raise `Error` """ + path = Path(path) + if path.exists(): + raise FileNotFoundError(f"{path} already exists") + elif not path.parent.exists(): + raise TypeError(f"{path} is not a valid path, provide path with file name") + else: + return path + + +def create_rule_file(file_path: Path) -> None: + """Create a new rule file.""" + context = _LICENCE + _IMPORTS + _RULE_CLASS + with open(file_path, "w") as f: + f.write(context) + + print(f"Successfully created {file_path.name} rule file at {file_path.parent}") + + +def main(): + parser = argparse.ArgumentParser( + description="Creates a skeleton of adding new rule file" + ) + parser.add_argument( + "-p", + "--path", + type=is_path_exists, + default=Path("fixit/rules/new_rule.py"), + help="Path to add rule file, Default is fixit/rules/new_rule.py", + ) + + args = parser.parse_args() + create_rule_file(args.path) + + +if __name__ == "__main__": + main() From 85f841cace2a4bda41b91daeffb220a5c5094f6c Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:07:31 -0700 Subject: [PATCH 02/23] Update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 04548163..440dfaef 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ build/ docs/source/rules/ coverage.xml coverage_report.html +.idea From 5b26a8e2d0bc20614ec541b20cd683f6e2549ac8 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:09:14 -0700 Subject: [PATCH 03/23] Updated gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 440dfaef..9261cd0d 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,4 @@ build/ docs/source/rules/ coverage.xml coverage_report.html -.idea +.idea/ From ba24249066c23d6bf5e59492d6696bd2787b8f63 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:15:47 -0700 Subject: [PATCH 04/23] Removed untracked files --- .idea/.gitignore | 8 -------- .idea/Fixit.iml | 8 -------- .idea/inspectionProfiles/Project_Default.xml | 17 ----------------- .idea/inspectionProfiles/profiles_settings.xml | 6 ------ .idea/misc.xml | 7 ------- .idea/modules.xml | 8 -------- .idea/vcs.xml | 6 ------ 7 files changed, 60 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/Fixit.iml delete mode 100644 .idea/inspectionProfiles/Project_Default.xml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 73f69e09..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/.idea/Fixit.iml b/.idea/Fixit.iml deleted file mode 100644 index c444878a..00000000 --- a/.idea/Fixit.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index d2e4bbdd..00000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2da..00000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 86561143..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 1a140847..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From 8f2dcd1bebcfa8978b2605d75113bd761889d927 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:29:12 -0700 Subject: [PATCH 05/23] Lint --- fixit/cli/add_new_rule.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 9f159102..1b417a02 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -16,7 +16,7 @@ """ -_LICENCE = """\ +_LICENCE = """ # Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the @@ -24,13 +24,13 @@ """ -_IMPORTS = """\ +_IMPORTS = """ import libcst as cst from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid """ -_RULE_CLASS = """\ +_RULE_CLASS = """ class Rule(CstLintRule): \""" docstring or new_rule description From a5fb62f0ec641a65f6bc42750dc979bffb8f7186 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 22:43:03 -0700 Subject: [PATCH 06/23] Minor fixes --- fixit/cli/add_new_rule.py | 19 +++++++++---------- fixit/rules/new_rule.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 fixit/rules/new_rule.py diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 1b417a02..149e1ec9 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -16,8 +16,7 @@ """ -_LICENCE = """ -# Copyright (c) Facebook, Inc. and its affiliates. +_LICENCE = """# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. @@ -44,15 +43,15 @@ class Rule(CstLintRule): """ -def is_path_exists(path: str) -> Union[Path, TypeError, FileExistsError]: +def is_path_exists(path: str) -> Path: """Check for valid path, if yes, return `Path` else raise `Error` """ - path = Path(path) - if path.exists(): - raise FileNotFoundError(f"{path} already exists") - elif not path.parent.exists(): - raise TypeError(f"{path} is not a valid path, provide path with file name") + filepath = Path(path) + if filepath.exists(): + raise FileNotFoundError(f"{filepath} already exists") + elif not filepath.parent.exists(): + raise TypeError(f"{filepath} is not a valid path, provide path with file name") else: - return path + return filepath def create_rule_file(file_path: Path) -> None: @@ -64,7 +63,7 @@ def create_rule_file(file_path: Path) -> None: print(f"Successfully created {file_path.name} rule file at {file_path.parent}") -def main(): +def main() -> None: parser = argparse.ArgumentParser( description="Creates a skeleton of adding new rule file" ) diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py new file mode 100644 index 00000000..de35223d --- /dev/null +++ b/fixit/rules/new_rule.py @@ -0,0 +1,18 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + +import libcst as cst +from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid + +class Rule(CstLintRule): + """ + docstring or new_rule description + """ + MESSAGE = 'Enter rule description message' + + VALID = [Valid()] + + INVALID = [Invalid()] + From 4ccb1057b115d95984f4ddab7aff24f3624ba10e Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 23:03:13 -0700 Subject: [PATCH 07/23] Minor fixes to new file --- fixit/cli/add_new_rule.py | 13 +++++++++---- fixit/rules/new_rule.py | 12 +++++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 149e1ec9..587fb203 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -5,7 +5,6 @@ import argparse from pathlib import Path -from typing import Union """ @@ -24,22 +23,28 @@ """ _IMPORTS = """ -import libcst as cst from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid """ +_TO_DOS = """ +\""" +This is a model rule file for adding a new rule to fixit module +\""" + +""" + _RULE_CLASS = """ class Rule(CstLintRule): \""" docstring or new_rule description \""" + MESSAGE = 'Enter rule description message' VALID = [Valid()] INVALID = [Invalid()] - """ @@ -56,7 +61,7 @@ def is_path_exists(path: str) -> Path: def create_rule_file(file_path: Path) -> None: """Create a new rule file.""" - context = _LICENCE + _IMPORTS + _RULE_CLASS + context = _LICENCE + _IMPORTS + _TO_DOS + _RULE_CLASS with open(file_path, "w") as f: f.write(context) diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py index de35223d..52fd6c5e 100644 --- a/fixit/rules/new_rule.py +++ b/fixit/rules/new_rule.py @@ -3,16 +3,22 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. -import libcst as cst + from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid + +""" +This is a model rule file for adding a new rule to fixit module +""" + + class Rule(CstLintRule): """ docstring or new_rule description """ - MESSAGE = 'Enter rule description message' + + MESSAGE = "Enter rule description message" VALID = [Valid()] INVALID = [Invalid()] - From 61ca70708f64c5c1a1895dc67d7bd7492a006704 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 23:26:22 -0700 Subject: [PATCH 08/23] Lint fixes to new file --- fixit/cli/add_new_rule.py | 4 ++-- fixit/rules/new_rule.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 587fb203..44dab642 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -42,9 +42,9 @@ class Rule(CstLintRule): MESSAGE = 'Enter rule description message' - VALID = [Valid()] + VALID = [Valid("'example'")] - INVALID = [Invalid()] + INVALID = [Invalid("'example'")] """ diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py index 52fd6c5e..6a666110 100644 --- a/fixit/rules/new_rule.py +++ b/fixit/rules/new_rule.py @@ -17,8 +17,8 @@ class Rule(CstLintRule): docstring or new_rule description """ - MESSAGE = "Enter rule description message" + MESSAGE = 'Enter rule description message' - VALID = [Valid()] + VALID = [Valid("'example'")] - INVALID = [Invalid()] + INVALID = [Invalid("'example'")] From e121ab3b6705c2eed3c908786f09ae53ddc6e860 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 7 Sep 2020 23:52:45 -0700 Subject: [PATCH 09/23] Lint fixes --- fixit/cli/add_new_rule.py | 2 +- fixit/rules/new_rule.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 44dab642..85346702 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -40,7 +40,7 @@ class Rule(CstLintRule): docstring or new_rule description \""" - MESSAGE = 'Enter rule description message' + MESSAGE = "Enter rule description message" VALID = [Valid("'example'")] diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py index 6a666110..645357c0 100644 --- a/fixit/rules/new_rule.py +++ b/fixit/rules/new_rule.py @@ -17,7 +17,7 @@ class Rule(CstLintRule): docstring or new_rule description """ - MESSAGE = 'Enter rule description message' + MESSAGE = "Enter rule description message" VALID = [Valid("'example'")] From 94bc7313f0b9b71f68f54349c0e0f54083dfc6e7 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 00:14:19 -0700 Subject: [PATCH 10/23] Removed new rule file --- fixit/rules/new_rule.py | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 fixit/rules/new_rule.py diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py deleted file mode 100644 index 645357c0..00000000 --- a/fixit/rules/new_rule.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright (c) Facebook, Inc. and its affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - - -from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid - - -""" -This is a model rule file for adding a new rule to fixit module -""" - - -class Rule(CstLintRule): - """ - docstring or new_rule description - """ - - MESSAGE = "Enter rule description message" - - VALID = [Valid("'example'")] - - INVALID = [Invalid("'example'")] From 9de1f125cfc814abe24ca61f656f9222c96fd1dc Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 12:09:02 -0700 Subject: [PATCH 11/23] Comments formatting --- fixit/cli/add_new_rule.py | 23 +++++++++++++---------- fixit/rules/new_rule.py | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 fixit/rules/new_rule.py diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 85346702..dc611a64 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -3,19 +3,20 @@ # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. +# Usage: +# +# $ python -m fixit.cli.add_new_rule --help +# $ python -m fixit.cli.add_new_rule +# $ python -m fixit.cli.add_new_rule --path fixit/rules/new_rule.py + import argparse +from fixit.common.config import get_lint_config +from libcst.codemod._cli import invoke_formatter from pathlib import Path -""" -Usage: - $ python -m fixit.cli.add_new_rule --help - $ python -m fixit.cli.add_new_rule - $ python -m fixit.cli.add_new_rule --path fixit/rules/new_rule.py - -""" - -_LICENCE = """# Copyright (c) Facebook, Inc. and its affiliates. +_LICENCE = """\ +# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. @@ -62,8 +63,10 @@ def is_path_exists(path: str) -> Path: def create_rule_file(file_path: Path) -> None: """Create a new rule file.""" context = _LICENCE + _IMPORTS + _TO_DOS + _RULE_CLASS + updated_context = invoke_formatter(get_lint_config().formatter, context) + with open(file_path, "w") as f: - f.write(context) + f.write(updated_context) print(f"Successfully created {file_path.name} rule file at {file_path.parent}") diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py new file mode 100644 index 00000000..645357c0 --- /dev/null +++ b/fixit/rules/new_rule.py @@ -0,0 +1,24 @@ +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + + +from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid + + +""" +This is a model rule file for adding a new rule to fixit module +""" + + +class Rule(CstLintRule): + """ + docstring or new_rule description + """ + + MESSAGE = "Enter rule description message" + + VALID = [Valid("'example'")] + + INVALID = [Invalid("'example'")] From 9705f7aa4ec8808f9c3162af3d7b92ed5a91335d Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 12:12:10 -0700 Subject: [PATCH 12/23] Sorted imports --- fixit/cli/add_new_rule.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index dc611a64..bfcea8c4 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -1,6 +1,6 @@ # Copyright (c) Facebook, Inc. and its affiliates. # -# This source code is licensed under the MIT license found in the +# This source code is licensed under the MIT license found in thefla # LICENSE file in the root directory of this source tree. # Usage: @@ -10,10 +10,12 @@ # $ python -m fixit.cli.add_new_rule --path fixit/rules/new_rule.py import argparse -from fixit.common.config import get_lint_config -from libcst.codemod._cli import invoke_formatter from pathlib import Path +from libcst.codemod._cli import invoke_formatter + +from fixit.common.config import get_lint_config + _LICENCE = """\ # Copyright (c) Facebook, Inc. and its affiliates. From f9ccfb83c7c4487180fe85d9f02a4391cff48451 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 12:15:54 -0700 Subject: [PATCH 13/23] Removed new rule file --- fixit/rules/new_rule.py | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 fixit/rules/new_rule.py diff --git a/fixit/rules/new_rule.py b/fixit/rules/new_rule.py deleted file mode 100644 index 645357c0..00000000 --- a/fixit/rules/new_rule.py +++ /dev/null @@ -1,24 +0,0 @@ -# Copyright (c) Facebook, Inc. and its affiliates. -# -# This source code is licensed under the MIT license found in the -# LICENSE file in the root directory of this source tree. - - -from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid - - -""" -This is a model rule file for adding a new rule to fixit module -""" - - -class Rule(CstLintRule): - """ - docstring or new_rule description - """ - - MESSAGE = "Enter rule description message" - - VALID = [Valid("'example'")] - - INVALID = [Invalid("'example'")] From 1ac58b5f24810fadb0e9d6c71267df1e1a8692fd Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 13:22:48 -0700 Subject: [PATCH 14/23] Updated ipynb documentation --- docs/source/build_a_lint_rule.ipynb | 80 +++++++++++++++++++++++++++-- fixit/cli/add_new_rule.py | 8 +-- 2 files changed, 81 insertions(+), 7 deletions(-) diff --git a/docs/source/build_a_lint_rule.ipynb b/docs/source/build_a_lint_rule.ipynb index 694ef7df..b788d4f3 100644 --- a/docs/source/build_a_lint_rule.ipynb +++ b/docs/source/build_a_lint_rule.ipynb @@ -121,14 +121,86 @@ " ..." ] }, + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "Iteration order of attributes is the same as the order they appear in the source code.\n", + "In this case, that means visit_If_test is called before visit_If_body and visit_If_orelse.\n", + "\n", + "Use fixit's cli to generate a skeleton of new rule file::\n", + "\n", + " $ python -m fixit.cli.add_new_rule # Creates new_rule.py at fixit/rules/new_rule.py\n", + " $ python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py # Creates rule file at path specified\n", + "\n", + "This will generate a model rule file used to create new rule." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "import libcst as cst\n", + "import libcst.matchers as m\n", + "\n", + "from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid\n", + "\n", + "class Rule(CstLintRule):\n", + " \"\"\"\n", + " docstring or new_rule description\n", + " \"\"\"\n", + "\n", + " MESSAGE = \"Enter rule description message\"\n", + "\n", + " VALID = [Valid(\"'example'\")] # Valid examples\n", + "\n", + " INVALID = [Invalid(\"'example'\")] # Invalid examples\n", + " ..." + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, + { + "cell_type": "raw", + "source": [ + "Use above structure to create a new rule. \n", + "\n", + "The Declarative Matcher API\n", + "===========================\n", + "\n", + "Once we have a ``ClassDef`` node, we need to see if it contains a base class named ``object``.\n", + "We could implement by inspecting attributes of the node using equality and isinstance." + ], + "metadata": { + "collapsed": false + } + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "# check if any of the base classes of this class def is \"object\"\n", + "def visit_ClassDef(self, node: cst.ClassDef):\n", + " has_object_base = any(\n", + " isinstance(arg.value, cst.Name) and arg.value.value == \"object\"\n", + " for arg in node.bases\n", + " )" + ] + }, { "cell_type": "raw", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ - "Iteration order of attributes is the same as the order they appear in the source code.\n", - "In this case, that means visit_If_test is called before visit_If_body and visit_If_orelse.\n", + "Use above structure to create a new rule. \n", "\n", "The Declarative Matcher API\n", "===========================\n", @@ -323,9 +395,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.7.0b1" } }, "nbformat": 4, "nbformat_minor": 4 -} +} \ No newline at end of file diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index bfcea8c4..071d6335 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -1,6 +1,6 @@ # Copyright (c) Facebook, Inc. and its affiliates. # -# This source code is licensed under the MIT license found in thefla +# This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # Usage: @@ -17,8 +17,7 @@ from fixit.common.config import get_lint_config -_LICENCE = """\ -# Copyright (c) Facebook, Inc. and its affiliates. +_LICENCE = """# Copyright (c) Facebook, Inc. and its affiliates. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. @@ -26,6 +25,9 @@ """ _IMPORTS = """ +import libcst as cst +import libcst.matchers as m + from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid """ From e6e1bb88b0ec4a702e078960a42aef0896c85520 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 16:00:05 -0700 Subject: [PATCH 15/23] Removed redundant cells --- docs/source/build_a_lint_rule.ipynb | 50 +++++++---------------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/docs/source/build_a_lint_rule.ipynb b/docs/source/build_a_lint_rule.ipynb index b788d4f3..64c05568 100644 --- a/docs/source/build_a_lint_rule.ipynb +++ b/docs/source/build_a_lint_rule.ipynb @@ -123,12 +123,14 @@ }, { "cell_type": "raw", - "metadata": {}, + "metadata": { + "raw_mimetype": "text/restructuredtext" + }, "source": [ "Iteration order of attributes is the same as the order they appear in the source code.\n", "In this case, that means visit_If_test is called before visit_If_body and visit_If_orelse.\n", "\n", - "Use fixit's cli to generate a skeleton of new rule file::\n", + "Use fixit's cli to generate a skeleton of adding a new rule file::\n", "\n", " $ python -m fixit.cli.add_new_rule # Creates new_rule.py at fixit/rules/new_rule.py\n", " $ python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py # Creates rule file at path specified\n", @@ -139,6 +141,11 @@ { "cell_type": "code", "execution_count": null, + "metadata": { + "pycharm": { + "name": "#%%\n" + } + }, "outputs": [], "source": [ "import libcst as cst\n", @@ -157,41 +164,6 @@ "\n", " INVALID = [Invalid(\"'example'\")] # Invalid examples\n", " ..." - ], - "metadata": { - "collapsed": false, - "pycharm": { - "name": "#%%\n" - } - } - }, - { - "cell_type": "raw", - "source": [ - "Use above structure to create a new rule. \n", - "\n", - "The Declarative Matcher API\n", - "===========================\n", - "\n", - "Once we have a ``ClassDef`` node, we need to see if it contains a base class named ``object``.\n", - "We could implement by inspecting attributes of the node using equality and isinstance." - ], - "metadata": { - "collapsed": false - } - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [], - "source": [ - "# check if any of the base classes of this class def is \"object\"\n", - "def visit_ClassDef(self, node: cst.ClassDef):\n", - " has_object_base = any(\n", - " isinstance(arg.value, cst.Name) and arg.value.value == \"object\"\n", - " for arg in node.bases\n", - " )" ] }, { @@ -211,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -379,7 +351,7 @@ } ], "metadata": { - "celltoolbar": "Edit Metadata", + "celltoolbar": "Raw Cell Format", "kernelspec": { "display_name": "Python 3", "language": "python", From 0ed634c820b50c8e314053c7dab48adf91e6b578 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 8 Sep 2020 16:16:02 -0700 Subject: [PATCH 16/23] Execution count to null --- docs/source/build_a_lint_rule.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/source/build_a_lint_rule.ipynb b/docs/source/build_a_lint_rule.ipynb index 64c05568..7bf92214 100644 --- a/docs/source/build_a_lint_rule.ipynb +++ b/docs/source/build_a_lint_rule.ipynb @@ -183,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ From 6308923678786cca0485369e572b7bc943766453 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 9 Sep 2020 10:29:37 -0700 Subject: [PATCH 17/23] Updated getting started doc --- docs/source/getting_started.ipynb | 241 ++++++++++++++++++++++++++++-- 1 file changed, 226 insertions(+), 15 deletions(-) diff --git a/docs/source/getting_started.ipynb b/docs/source/getting_started.ipynb index 781afd7f..e0a1b3d3 100644 --- a/docs/source/getting_started.ipynb +++ b/docs/source/getting_started.ipynb @@ -19,11 +19,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "nbsphinx": "hidden" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "env: PYTHONPATH=/Users/lomesh/open-source/Fixit\n", + "/var/folders/dk/x6hpyx3d3555d6d0b6nhszjw0000gn/T/tmp4x7t6a68\n" + ] + } + ], "source": [ "import tempfile\n", "temp_dir = tempfile.TemporaryDirectory()\n", @@ -44,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -66,11 +75,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "nbsphinx": "hidden" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/bin/sh: pyre: command not found\n", + "Initialized empty Git repository in /private/var/folders/dk/x6hpyx3d3555d6d0b6nhszjw0000gn/T/tmp4x7t6a68/.git/\n", + "/bin/sh: pyre: command not found\n" + ] + } + ], "source": [ "FILE_NAME = \"example.py\"\n", "from pathlib import Path\n", @@ -94,11 +113,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Scanning 1 files\n", + "Testing 23 rules\n", + "\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "example.py:4:1\n", + " NoInheritFromObjectRule: Inheriting from object is a no-op. 'class Foo:' is\n", + " just fine =)\n", + "example.py:5:12\n", + " UsePlusForStringConcatRule: Implicit string concatenation detected, please\n", + " add '+' to be explicit. E.g. a tuple or a call (\"a\" \"b\") with a missing\n", + " comma results in multiple strings being concatenated as one string and\n", + " causes unexpected behaviour.\n", + "example.py:10:16\n", + " ComparePrimitivesByEqualRule: Don't use `is` or `is not` to compare\n", + " primitives, as they compare references. Use == or != instead.\n", + "example.py:13:25\n", + " RewriteToComprehensionRule: It's unnecessary to use a list comprehension\n", + " inside a call to dict since there are equivalent comprehensions for this\n", + " type\n", + "example.py:10:16\n", + " F632: use ==/!= to compare constant literals (str, bytes, int, float, tuple)\n", + "example.py:13:80\n", + " E501: line too long (85 > 79 characters)\n", + "\n", + "Found 6 reports in 1 files in 1.37 seconds.\n" + ] + } + ], "source": [ - "! python -m fixit.cli.run_rules" + "! python3.8 -m fixit.cli.run_rules" ] }, { @@ -114,11 +167,86 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Scanning 1 files\n", + "./example.py\n", + "\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "example.py:4:1 [applied fix]\n", + " NoInheritFromObjectRule: Inheriting from object is a no-op. 'class Foo:' is\n", + " just fine =)\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "example.py\n", + "example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "example.py:5:12 [applied fix]\n", + " UsePlusForStringConcatRule: Implicit string concatenation detected, please\n", + " add '+' to be explicit. E.g. a tuple or a call (\"a\" \"b\") with a missing\n", + " comma results in multiple strings being concatenated as one string and\n", + " causes unexpected behaviour.\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "example.py:10:16 [applied fix]\n", + " ComparePrimitivesByEqualRule: Don't use `is` or `is not` to compare\n", + " primitives, as they compare references. Use == or != instead.\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "example.py:13:25 [applied fix]\n", + " RewriteToComprehensionRule: It's unnecessary to use a list comprehension\n", + " inside a call to dict since there are equivalent comprehensions for this\n", + " type\n", + "Encountered exception for the following paths:\n", + "./example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "example.py\n", + "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", + "\u001b[1mreformatted -\u001b[0m\n", + "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", + "\u001b[1m1 file reformatted\u001b[0m.\u001b[0m\n", + "\n", + "Found 4 reports in 1 files in 6.57 seconds.\n" + ] + } + ], "source": [ - "! python -m fixit.cli.apply_fix" + "! python3.8 -m fixit.cli.apply_fix" ] }, { @@ -132,9 +260,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1mdiff --git a/example.py b/example.py\u001b[m\r\n", + "\u001b[1mindex aed4bb5..3f667f8 100644\u001b[m\r\n", + "\u001b[1m--- a/example.py\u001b[m\r\n", + "\u001b[1m+++ b/example.py\u001b[m\r\n", + "\u001b[36m@@ -1,14 +1,14 @@\u001b[m\r\n", + " from typing import Dict\u001b[m\r\n", + " \u001b[m\r\n", + " \u001b[m\r\n", + "\u001b[31m-class C(object):\u001b[m\r\n", + "\u001b[31m- attr = \"ab\" \"cd\" \"ef\" \"gh\"\u001b[m\r\n", + "\u001b[32m+\u001b[m\u001b[32mclass C:\u001b[m\r\n", + "\u001b[32m+\u001b[m\u001b[32m attr = \"ab\" + \"cd\" + \"ef\" + \"gh\"\u001b[m\r\n", + " \u001b[m\r\n", + " def method(self) -> Dict[int, str]:\u001b[m\r\n", + " filtered_char = []\u001b[m\r\n", + " for char in self.attr:\u001b[m\r\n", + "\u001b[31m- if char is not \"a\":\u001b[m\r\n", + "\u001b[32m+\u001b[m\u001b[32m if char != \"a\":\u001b[m\r\n", + " filtered_char.append(char)\u001b[m\r\n", + " \u001b[m\r\n", + "\u001b[31m- index_to_char = dict([(idx, char) for idx, char in enumerate(filtered_char)])\u001b[m\r\n", + "\u001b[32m+\u001b[m\u001b[32m index_to_char = {idx: char for idx, char in enumerate(filtered_char)}\u001b[m\r\n", + " return index_to_char\u001b[m\r\n" + ] + } + ], "source": [ "! git diff" ] @@ -287,7 +445,60 @@ "\n", "- For more detailes on this script's usage, run::\n", "\n", - " python -m fixit.cli.insert_suppressions --help" + " python -m fixit.cli.insert_suppressions --help\n", + " \n", + " \n", + "Creating New Rules\n", + "================== \n", + "\n", + "If you wish to create a new rule then, we have provided a script to make a general model for adding new rule in fixit module. To do this, run" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", + "1 file left unchanged.\u001b[0m\n", + "Successfully created my_rule.py rule file at fixit/rules\n", + "# Copyright (c) Facebook, Inc. and its affiliates.\n", + "#\n", + "# This source code is licensed under the MIT license found in the\n", + "# LICENSE file in the root directory of this source tree.\n", + "\n", + "\n", + "import libcst as cst\n", + "import libcst.matchers as m\n", + "\n", + "from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid\n", + "\n", + "\n", + "\"\"\"\n", + "This is a model rule file for adding a new rule to fixit module\n", + "\"\"\"\n", + "\n", + "\n", + "class Rule(CstLintRule):\n", + " \"\"\"\n", + " docstring or new_rule description\n", + " \"\"\"\n", + "\n", + " MESSAGE = \"Enter rule description message\"\n", + "\n", + " VALID = [Valid(\"'example'\")]\n", + "\n", + " INVALID = [Invalid(\"'example'\")]\n" + ] + } + ], + "source": [ + "! python3.8 -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", + "! cat fixit/rules/my_rule.py" ] }, { @@ -319,7 +530,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.5" + "version": "3.7.0b1" } }, "nbformat": 4, From 051b60f7475791fdd9911c743d8ca8f8223642ef Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 9 Sep 2020 10:32:22 -0700 Subject: [PATCH 18/23] Minor fixes --- docs/source/getting_started.ipynb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/source/getting_started.ipynb b/docs/source/getting_started.ipynb index e0a1b3d3..bf99fbe0 100644 --- a/docs/source/getting_started.ipynb +++ b/docs/source/getting_started.ipynb @@ -151,7 +151,7 @@ } ], "source": [ - "! python3.8 -m fixit.cli.run_rules" + "! python -m fixit.cli.run_rules" ] }, { @@ -246,7 +246,7 @@ } ], "source": [ - "! python3.8 -m fixit.cli.apply_fix" + "! python -m fixit.cli.apply_fix" ] }, { @@ -497,7 +497,7 @@ } ], "source": [ - "! python3.8 -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", + "! python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", "! cat fixit/rules/my_rule.py" ] }, From 4fe948cd001ebc0333b81d1a360c837a75b306ea Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 9 Sep 2020 10:40:16 -0700 Subject: [PATCH 19/23] Removed execution count --- docs/source/getting_started.ipynb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/source/getting_started.ipynb b/docs/source/getting_started.ipynb index bf99fbe0..cc2e412c 100644 --- a/docs/source/getting_started.ipynb +++ b/docs/source/getting_started.ipynb @@ -19,7 +19,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "nbsphinx": "hidden" }, @@ -53,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": { "nbsphinx": "hidden" }, @@ -113,7 +113,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -167,7 +167,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -260,7 +260,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -456,7 +456,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [ { From a81c9ca8fb739a66a7914f7315f83c9041a66933 Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 10 Sep 2020 11:22:37 -0700 Subject: [PATCH 20/23] Clear outputs from ipynb --- docs/source/getting_started.ipynb | 220 +++--------------------------- 1 file changed, 17 insertions(+), 203 deletions(-) diff --git a/docs/source/getting_started.ipynb b/docs/source/getting_started.ipynb index cc2e412c..81cfc9d2 100644 --- a/docs/source/getting_started.ipynb +++ b/docs/source/getting_started.ipynb @@ -23,16 +23,7 @@ "metadata": { "nbsphinx": "hidden" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "env: PYTHONPATH=/Users/lomesh/open-source/Fixit\n", - "/var/folders/dk/x6hpyx3d3555d6d0b6nhszjw0000gn/T/tmp4x7t6a68\n" - ] - } - ], + "outputs": [], "source": [ "import tempfile\n", "temp_dir = tempfile.TemporaryDirectory()\n", @@ -79,17 +70,7 @@ "metadata": { "nbsphinx": "hidden" }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "/bin/sh: pyre: command not found\n", - "Initialized empty Git repository in /private/var/folders/dk/x6hpyx3d3555d6d0b6nhszjw0000gn/T/tmp4x7t6a68/.git/\n", - "/bin/sh: pyre: command not found\n" - ] - } - ], + "outputs": [], "source": [ "FILE_NAME = \"example.py\"\n", "from pathlib import Path\n", @@ -115,41 +96,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Scanning 1 files\n", - "Testing 23 rules\n", - "\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "example.py:4:1\n", - " NoInheritFromObjectRule: Inheriting from object is a no-op. 'class Foo:' is\n", - " just fine =)\n", - "example.py:5:12\n", - " UsePlusForStringConcatRule: Implicit string concatenation detected, please\n", - " add '+' to be explicit. E.g. a tuple or a call (\"a\" \"b\") with a missing\n", - " comma results in multiple strings being concatenated as one string and\n", - " causes unexpected behaviour.\n", - "example.py:10:16\n", - " ComparePrimitivesByEqualRule: Don't use `is` or `is not` to compare\n", - " primitives, as they compare references. Use == or != instead.\n", - "example.py:13:25\n", - " RewriteToComprehensionRule: It's unnecessary to use a list comprehension\n", - " inside a call to dict since there are equivalent comprehensions for this\n", - " type\n", - "example.py:10:16\n", - " F632: use ==/!= to compare constant literals (str, bytes, int, float, tuple)\n", - "example.py:13:80\n", - " E501: line too long (85 > 79 characters)\n", - "\n", - "Found 6 reports in 1 files in 1.37 seconds.\n" - ] - } - ], + "outputs": [], "source": [ "! python -m fixit.cli.run_rules" ] @@ -169,82 +116,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Scanning 1 files\n", - "./example.py\n", - "\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "example.py:4:1 [applied fix]\n", - " NoInheritFromObjectRule: Inheriting from object is a no-op. 'class Foo:' is\n", - " just fine =)\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "example.py\n", - "example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "example.py:5:12 [applied fix]\n", - " UsePlusForStringConcatRule: Implicit string concatenation detected, please\n", - " add '+' to be explicit. E.g. a tuple or a call (\"a\" \"b\") with a missing\n", - " comma results in multiple strings being concatenated as one string and\n", - " causes unexpected behaviour.\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "example.py:10:16 [applied fix]\n", - " ComparePrimitivesByEqualRule: Don't use `is` or `is not` to compare\n", - " primitives, as they compare references. Use == or != instead.\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "example.py:13:25 [applied fix]\n", - " RewriteToComprehensionRule: It's unnecessary to use a list comprehension\n", - " inside a call to dict since there are equivalent comprehensions for this\n", - " type\n", - "Encountered exception for the following paths:\n", - "./example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "example.py\n", - "\u001b[93mRunning `pyre start` may solve the issue.\u001b[00m\n", - "\u001b[1mreformatted -\u001b[0m\n", - "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", - "\u001b[1m1 file reformatted\u001b[0m.\u001b[0m\n", - "\n", - "Found 4 reports in 1 files in 6.57 seconds.\n" - ] - } - ], + "outputs": [], "source": [ "! python -m fixit.cli.apply_fix" ] @@ -262,37 +134,7 @@ "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[1mdiff --git a/example.py b/example.py\u001b[m\r\n", - "\u001b[1mindex aed4bb5..3f667f8 100644\u001b[m\r\n", - "\u001b[1m--- a/example.py\u001b[m\r\n", - "\u001b[1m+++ b/example.py\u001b[m\r\n", - "\u001b[36m@@ -1,14 +1,14 @@\u001b[m\r\n", - " from typing import Dict\u001b[m\r\n", - " \u001b[m\r\n", - " \u001b[m\r\n", - "\u001b[31m-class C(object):\u001b[m\r\n", - "\u001b[31m- attr = \"ab\" \"cd\" \"ef\" \"gh\"\u001b[m\r\n", - "\u001b[32m+\u001b[m\u001b[32mclass C:\u001b[m\r\n", - "\u001b[32m+\u001b[m\u001b[32m attr = \"ab\" + \"cd\" + \"ef\" + \"gh\"\u001b[m\r\n", - " \u001b[m\r\n", - " def method(self) -> Dict[int, str]:\u001b[m\r\n", - " filtered_char = []\u001b[m\r\n", - " for char in self.attr:\u001b[m\r\n", - "\u001b[31m- if char is not \"a\":\u001b[m\r\n", - "\u001b[32m+\u001b[m\u001b[32m if char != \"a\":\u001b[m\r\n", - " filtered_char.append(char)\u001b[m\r\n", - " \u001b[m\r\n", - "\u001b[31m- index_to_char = dict([(idx, char) for idx, char in enumerate(filtered_char)])\u001b[m\r\n", - "\u001b[32m+\u001b[m\u001b[32m index_to_char = {idx: char for idx, char in enumerate(filtered_char)}\u001b[m\r\n", - " return index_to_char\u001b[m\r\n" - ] - } - ], + "outputs": [], "source": [ "! git diff" ] @@ -451,56 +293,28 @@ "Creating New Rules\n", "================== \n", "\n", - "If you wish to create a new rule then, we have provided a script to make a general model for adding new rule in fixit module. To do this, run" + "If you wish to create and add a new rule to fixit module then, we have provided a script to generate a basic class structure for adding new rule. To do this, run below commands from fixit's root directory" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n", - "1 file left unchanged.\u001b[0m\n", - "Successfully created my_rule.py rule file at fixit/rules\n", - "# Copyright (c) Facebook, Inc. and its affiliates.\n", - "#\n", - "# This source code is licensed under the MIT license found in the\n", - "# LICENSE file in the root directory of this source tree.\n", - "\n", - "\n", - "import libcst as cst\n", - "import libcst.matchers as m\n", - "\n", - "from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid\n", - "\n", - "\n", - "\"\"\"\n", - "This is a model rule file for adding a new rule to fixit module\n", - "\"\"\"\n", - "\n", - "\n", - "class Rule(CstLintRule):\n", - " \"\"\"\n", - " docstring or new_rule description\n", - " \"\"\"\n", - "\n", - " MESSAGE = \"Enter rule description message\"\n", - "\n", - " VALID = [Valid(\"'example'\")]\n", - "\n", - " INVALID = [Invalid(\"'example'\")]\n" - ] - } - ], + "outputs": [], "source": [ "! python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", "! cat fixit/rules/my_rule.py" ] }, + { + "cell_type": "raw", + "metadata": { + "raw_mimetype": "text/restructuredtext" + }, + "source": [ + "Now, you can add rule's functionality on top of above generated file." + ] + }, { "cell_type": "code", "execution_count": null, @@ -530,7 +344,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0b1" + "version": "3.7.3" } }, "nbformat": 4, From 05f4e78917fc0b2d5a73d5b118713e0ee1f3f40c Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 10 Sep 2020 11:50:57 -0700 Subject: [PATCH 21/23] Replaced class from build file --- docs/source/build_a_lint_rule.ipynb | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/docs/source/build_a_lint_rule.ipynb b/docs/source/build_a_lint_rule.ipynb index 7bf92214..39f9dae5 100644 --- a/docs/source/build_a_lint_rule.ipynb +++ b/docs/source/build_a_lint_rule.ipynb @@ -135,7 +135,7 @@ " $ python -m fixit.cli.add_new_rule # Creates new_rule.py at fixit/rules/new_rule.py\n", " $ python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py # Creates rule file at path specified\n", "\n", - "This will generate a model rule file used to create new rule." + "This will generate a rule file used to create and add new rule to fixit module." ] }, { @@ -148,22 +148,8 @@ }, "outputs": [], "source": [ - "import libcst as cst\n", - "import libcst.matchers as m\n", - "\n", - "from fixit import CstLintRule, InvalidTestCase as Invalid, ValidTestCase as Valid\n", - "\n", - "class Rule(CstLintRule):\n", - " \"\"\"\n", - " docstring or new_rule description\n", - " \"\"\"\n", - "\n", - " MESSAGE = \"Enter rule description message\"\n", - "\n", - " VALID = [Valid(\"'example'\")] # Valid examples\n", - "\n", - " INVALID = [Invalid(\"'example'\")] # Invalid examples\n", - " ..." + "! python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", + "! cat fixit/rules/my_rule.py" ] }, { @@ -172,7 +158,7 @@ "raw_mimetype": "text/restructuredtext" }, "source": [ - "Use above structure to create a new rule. \n", + "Now, you can add rule's functionality on top of above generated file. \n", "\n", "The Declarative Matcher API\n", "===========================\n", @@ -367,9 +353,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.0b1" + "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +} From cc13df72104044470aac46743a37f05544c1d3cf Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 10 Sep 2020 12:49:58 -0700 Subject: [PATCH 22/23] Exception correction --- fixit/cli/add_new_rule.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fixit/cli/add_new_rule.py b/fixit/cli/add_new_rule.py index 071d6335..e2eeee24 100644 --- a/fixit/cli/add_new_rule.py +++ b/fixit/cli/add_new_rule.py @@ -57,7 +57,7 @@ def is_path_exists(path: str) -> Path: """Check for valid path, if yes, return `Path` else raise `Error` """ filepath = Path(path) if filepath.exists(): - raise FileNotFoundError(f"{filepath} already exists") + raise FileExistsError(f"{filepath} already exists") elif not filepath.parent.exists(): raise TypeError(f"{filepath} is not a valid path, provide path with file name") else: From 8e39e810848594ff1a3f0ec058797eaad3d8a77c Mon Sep 17 00:00:00 2001 From: Charles Date: Thu, 10 Sep 2020 14:08:45 -0700 Subject: [PATCH 23/23] Minor Fixes --- docs/source/build_a_lint_rule.ipynb | 24 ++++++++++++++++++++++-- docs/source/getting_started.ipynb | 27 +-------------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/docs/source/build_a_lint_rule.ipynb b/docs/source/build_a_lint_rule.ipynb index 39f9dae5..74c69299 100644 --- a/docs/source/build_a_lint_rule.ipynb +++ b/docs/source/build_a_lint_rule.ipynb @@ -138,6 +138,18 @@ "This will generate a rule file used to create and add new rule to fixit module." ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "nbsphinx": "hidden" + }, + "outputs": [], + "source": [ + "import os\n", + "os.chdir(\"../../\")" + ] + }, { "cell_type": "code", "execution_count": null, @@ -148,7 +160,15 @@ }, "outputs": [], "source": [ - "! python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", + "! python3 -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "! cat fixit/rules/my_rule.py" ] }, @@ -337,7 +357,7 @@ } ], "metadata": { - "celltoolbar": "Raw Cell Format", + "celltoolbar": "Edit Metadata", "kernelspec": { "display_name": "Python 3", "language": "python", diff --git a/docs/source/getting_started.ipynb b/docs/source/getting_started.ipynb index 81cfc9d2..3e31cdf9 100644 --- a/docs/source/getting_started.ipynb +++ b/docs/source/getting_started.ipynb @@ -287,32 +287,7 @@ "\n", "- For more detailes on this script's usage, run::\n", "\n", - " python -m fixit.cli.insert_suppressions --help\n", - " \n", - " \n", - "Creating New Rules\n", - "================== \n", - "\n", - "If you wish to create and add a new rule to fixit module then, we have provided a script to generate a basic class structure for adding new rule. To do this, run below commands from fixit's root directory" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "! python -m fixit.cli.add_new_rule --path fixit/rules/my_rule.py\n", - "! cat fixit/rules/my_rule.py" - ] - }, - { - "cell_type": "raw", - "metadata": { - "raw_mimetype": "text/restructuredtext" - }, - "source": [ - "Now, you can add rule's functionality on top of above generated file." + " python -m fixit.cli.insert_suppressions --help" ] }, {