def improve(self, tree: cst.Module): noqa_detector = NoqaDetectionVisitor() wrapper = MetadataWrapper(tree) with noqa_detector.resolve(wrapper): wrapper.visit(noqa_detector) transformer = self.TRANSFORMER(self.CODE, noqa_detector.get_noqa_lines()) return wrapper.visit(transformer)
def transform(self, filepaths: List[str]): for filepath in filepaths: with open(filepath, "r") as f: orig_contents = MetadataWrapper(cst.parse_module(f.read())) updated_contents = orig_contents.visit(self) with open(filepath, "w") as f: f.write(updated_contents.code)
def _test_simple_class_helper(test: UnitTest, wrapper: MetadataWrapper) -> None: types = wrapper.resolve(TypeInferenceProvider) m = wrapper.module assign = cst.ensure_type( cst.ensure_type( cst.ensure_type( cst.ensure_type(m.body[1].body, cst.IndentedBlock).body[0], cst.FunctionDef, ).body.body[0], cst.SimpleStatementLine, ).body[0], cst.AnnAssign, ) self_number_attr = cst.ensure_type(assign.target, cst.Attribute) test.assertEqual(types[self_number_attr], "int") value = assign.value if value: test.assertEqual(types[value], "int") # self test.assertEqual(types[self_number_attr.value], "simple_class.Item") collector_assign = cst.ensure_type( cst.ensure_type(m.body[3], cst.SimpleStatementLine).body[0], cst.Assign) collector = collector_assign.targets[0].target test.assertEqual(types[collector], "simple_class.ItemCollector") items_assign = cst.ensure_type( cst.ensure_type(m.body[4], cst.SimpleStatementLine).body[0], cst.AnnAssign) items = items_assign.target test.assertEqual(types[items], "typing.Sequence[simple_class.Item]")
def test_simple_class_types(self, source_path: Path, data_path: Path) -> None: data: PyreData = json.loads(data_path.read_text()) wrapper = MetadataWrapper( cst.parse_module(source_path.read_text()), cache={TypeInferenceProvider: data}, ) _test_simple_class_helper(self, wrapper)
def test_simple_class_types(self, source_path: Path, data_path: Path) -> None: data: PyreData = json.loads(data_path.read_text()) wrapper = MetadataWrapper( cst.parse_module(source_path.read_text()), # pyre-fixme[6]: Expected `Mapping[Type[BaseMetadataProvider[object]], # Any]` for 2nd param but got `Dict[Type[TypeInferenceProvider], # Sequence[InferredType]]`. cache={TypeInferenceProvider: data}, ) _test_simple_class_helper(self, wrapper)
def _handle_metadata_reference( self, module: Module) -> Generator[Module, None, None]: oldwrapper = self.context.wrapper wrapper = MetadataWrapper(module) with self.resolve(wrapper): self.context = replace(self.context, wrapper=wrapper) try: yield wrapper.module finally: self.context = replace(self.context, wrapper=oldwrapper)
def _handle_metadata_reference( self, module: Module) -> Generator[Module, None, None]: oldwrapper = self.context.wrapper metadata_manager = self.context.metadata_manager filename = self.context.filename if metadata_manager and filename: # We can look up full-repo metadata for this codemod! cache = metadata_manager.get_cache_for_path(filename) wrapper = MetadataWrapper(module, cache=cache) else: # We are missing either the repo manager or the current path, # which can happen when we are codemodding from stdin or when # an upstream dependency manually instantiates us. wrapper = MetadataWrapper(module) with self.resolve(wrapper): self.context = replace(self.context, wrapper=wrapper) try: yield wrapper.module finally: self.context = replace(self.context, wrapper=oldwrapper)
def gather_imports(self, code: str) -> Set[str]: mod = MetadataWrapper(parse_module( CodemodTest.make_fixture_data(code))) mod.resolve_many(GatherUnusedImportsVisitor.METADATA_DEPENDENCIES) instance = GatherUnusedImportsVisitor(CodemodContext(wrapper=mod)) mod.visit(instance) return set(alias.evaluated_alias or alias.evaluated_name for alias, _ in instance.unused_imports)
def gather_comments(self, code: str) -> GatherCommentsVisitor: mod = MetadataWrapper(parse_module( CodemodTest.make_fixture_data(code))) mod.resolve_many(GatherCommentsVisitor.METADATA_DEPENDENCIES) instance = GatherCommentsVisitor(CodemodContext(wrapper=mod), r".*\Wnoqa(\W.*)?$") mod.visit(instance) return instance
def gather_names(self, code: str) -> GatherNamesFromStringAnnotationsVisitor: mod = MetadataWrapper(parse_module( CodemodTest.make_fixture_data(code))) mod.resolve_many( GatherNamesFromStringAnnotationsVisitor.METADATA_DEPENDENCIES) instance = GatherNamesFromStringAnnotationsVisitor( CodemodContext(wrapper=mod)) mod.visit(instance) return instance
def test_comment_wrap(orig_raw, expected_raw): t = CommentWrap(max_line_length=25) orig = MetadataWrapper(_parse(orig_raw)) updated_cst = orig.visit(t) assert updated_cst.code == dedent(expected_raw)