コード例 #1
0
 def test_single_statement(self, before, after):
     searcher = search.PyStmtRewritingSearcher.from_pattern(
         '0xbad',
         {search.ROOT_LABEL: syntactic_template.PythonTemplate('')})
     substitutions = list(search.find_iter(searcher, before, 'a.py'))
     self.assertEqual(after,
                      formatting.apply_substitutions(before, substitutions))
コード例 #2
0
 def write(self, path, content, matches):
     if not self.dry_run:
         try:
             with io.open(path, 'w', encoding='utf-8') as f:
                 f.write(formatting.apply_substitutions(content, matches))
         except IOError as e:
             print('skipped %s: IOError: %s' % (path, e), file=sys.stderr)
コード例 #3
0
 def test_noreplacements(self):
     sub = substitution.Substitution(
         matched_spans={'x': (1, 2)},
         primary_label='x',
     )
     self.assertEqual(
         formatting.apply_substitutions('abc', [sub]),
         'abc',
     )
コード例 #4
0
 def test_2_matches(self):
     sub1 = substitution.Substitution(
         matched_spans={'x': (0, 1)},
         replacements={'x': u'x'},
         primary_label='x',
     )
     sub2 = substitution.Substitution(
         matched_spans={'x': (2, 3)},
         replacements={'x': u'x'},
         primary_label='x',
     )
     self.assertEqual(
         formatting.apply_substitutions('abc', [sub1, sub2]),
         'xbx',
     )
コード例 #5
0
def rewrite_string(
    searcher: AbstractSearcher,
    source: Text,
    path: Text,
    max_iterations=1,
) -> Text:
    """Applies any replacements to the input source, and returns the result."""

    return formatting.apply_substitutions(
        source,
        find_iter(
            searcher,
            source,
            path,
            max_iterations=max_iterations,
        ))
コード例 #6
0
ファイル: test_fixer.py プロジェクト: ssbr/refex
 def test_examples_real(self):
     """Tests that the fixers do actually give the example replacement."""
     for fx in find_fixer.from_pattern('*').fixers:
         example = fx.example_fragment()
         example_replacement = fx.example_replacement()
         with self.subTest(fixer=type(fx).__name__,
                           example=example,
                           example_replacement=example_replacement):
             self.assertIsNotNone(example)
             substitutions = list(
                 search.find_iter(fixer.CombiningPythonFixer([fx]), example,
                                  'a.py'))
             self.assertLen(substitutions, 1)
             replaced = formatting.apply_substitutions(
                 example, substitutions)
             self.assertEqual(replaced, example_replacement)
コード例 #7
0
 def test_0_matches(self):
     self.assertEqual(
         formatting.apply_substitutions('abc', []),
         'abc',
     )
コード例 #8
0
def _fixed_point(
    searcher: 'AbstractSearcher',
    parsed: parsed_file.ParsedFile,
    initial_substitutions: Sequence[substitution.Substitution],
    start: int,
    end: int,
    max_iterations: int,
):
    """Repeatedly apply searcher until there are no more changes."""

    if max_iterations <= 1:
        return initial_substitutions

    # TODO(b/116068515): sort the substitutions here and below.
    new_substitutions = [
        s.relative_to_span(start, end) for s in initial_substitutions
    ]
    if None in new_substitutions:
        logging.error('Out of bounds substitution after filtering: %s',
                      initial_substitutions[new_substitutions.index(None)])
        return initial_substitutions  # give up

    text = parsed.text[start:end]
    logging.debug(
        'Applying _fixed_point with initial subs=%r on on parsed.text[%d:%d]: %r',
        new_substitutions, start, end, text)

    all_substitutions = []

    # max_iterations + 1 to get the extra iteration before the break,
    # and then - 1 to account for the fact that we already did an iteration.
    for i in range(max_iterations):
        rewritten = formatting.apply_substitutions(text, new_substitutions)
        try:
            parsed = _matcher.parse_ast(rewritten, parsed.path)
        except _matcher.ParseError as e:
            logging.error(
                'Could not parse rewritten substitution in %s: %s\n'
                'Tried to rewrite text[%s:%s] == %r\n'
                'Rewrite was: %r\n'
                'Substitutions: %r', parsed.path, e, start, end, text,
                rewritten, new_substitutions)
            break

        # These substitutions parsed and were valid, add them to the list:
        all_substitutions.extend(new_substitutions)

        # Set up the variables for the next rewrite attempt
        logging.debug('_fixed_point Iteration %d: rewrote %r -> %r', i, text,
                      rewritten)
        text = rewritten

        if i == max_iterations:
            # no point bothering to get the next substitution
            break

        new_substitutions = list(searcher.find_iter_parsed(parsed))
        if not new_substitutions:
            break

    if not all_substitutions:
        # even the first rewrite failed to parse
        return []
    elif len(all_substitutions) == len(initial_substitutions):
        # We didn't discover any new substitutions.
        return initial_substitutions
    else:
        return [_compile_substitutions(all_substitutions, start, end, text)]