Skip to content

Releases: isidentical/refactor

refactor v0.6.3

29 Oct 00:14
25b629a
Compare
Choose a tag to compare

Major

Nothing new, compared to 0.6.0.

Other Changes

  • Official Python 3.11 support.

0.6.2

23 Oct 14:36
Compare
Choose a tag to compare

Major

Nothing new, compared to 0.6.0.

Other Changes

  • Augmented and annotated assignments now counted towards definitions when analyzing the scope.
  • refactor.actions.InsertAfter now preserves the final line state (e.g. if the anchor doesn't end with a newline, it also will produce code that won't be ending with a newline).
  • getrefactor.com is now available.

0.6.1

23 Oct 02:10
Compare
Choose a tag to compare

Major

Nothing new, compared to 0.6.0.

Other Changes

  • A bugfix regarding multiline replacements.
  • WASM support/compatibility.

0.6.0

22 Oct 21:59
Compare
Choose a tag to compare

0.6.0

Major

This release adds experimental support for chained actions, a long awaited
feature. This would mean that each match() can now return multiple actions (in
the form of an iterator), and they will be applied gradually.

import ast
from refactor import Rule, actions
from refactor.context import Representative, Scope
from typing import Iterator


class Usages(Representative):
    context_providers = (Scope,)

    def find(self, name: str, needle: ast.AST) -> Iterator[ast.AST]:
        """Iterate all possible usage sites of ``name``."""
        for node in ast.walk(self.context.tree):
            if isinstance(node, ast.Name) and node.id == name:
                scope = self.context.scope.resolve(node)
                if needle in scope.get_definitions(name):
                    yield node


class PropagateAndDelete(Rule):
    context_providers = (Usages,)

    def match(self, node: ast.Import) -> Iterator[actions.BaseAction]:
        # Check if this is a single import with no alias.
        assert isinstance(node, ast.Import)
        assert len(node.names) == 1

        [name] = node.names
        assert name.asname is None

        # Replace each usage of this module with its own __import__() call.
        import_call = ast.Call(
            func=ast.Name("__import__"),
            args=[ast.Constant(name.name)],
            keywords=[],
        )
        for usage in self.context.usages.find(name.name, node):
            yield actions.Replace(usage, import_call)

        # And finally remove the import itself
        yield actions.Erase(node)

Other Changes

  • Encoding is now preserved when using the refactor.Session.run_file API (which means if you use the refactor.Change.apply_diff or using the -a flag in the CLI, the generated source code will be encoded
    with the original encoding before getting written to the file.)
  • Offset tracking has been changed to be at the encoded byte stream level (mirroring CPython behavior here). This fixes some unicode related problems (with actions like refactor.actions.Replace).
  • refactor.context.ScopeInfo.get_definitions now always returns a list, even
    if it can't find any definitions.

0.5.0

07 Aug 12:27
Compare
Choose a tag to compare

0.5.0

Note: With this release, we also started improving our documentation. If you are interested in Refactor, check the new layout and tutorials out and let us know how you feel!

Major

This release includes the overhaul of our action system, and with the next releases we will start removing the old ones. A list
of changes regarding actions can be seen here:

  • refactor.core no longer contains any actions (the deprecated aliases are still imported and exposed but all the new actions go into refactor.actions)
  • Action is now split into two, a refactor.actions.BaseAction which is the base of all actions (useful for type hinting) and a refactor.actions.LazyReplace (a replace action that builds the node lazily in its build()).
  • ReplacementAction is now refactor.actions.Replace
  • NewStatementAction is now refactor.actions.LazyInsertAfter
  • TargetedNewStatementAction is now refactor.actions.InsertAfter

For migrating your code base to the new style actions, we wrote a small tool (that we also used internally), examples/deprecated_aliases.py. Feel free to try it, and let us know if the transition was seamless.

Other Changes

  • Added experimental Windows support, contributed by Hakan Celik
  • common.find_closest now takes end_lineno and end_col_offset into account. It also ensures there is at least one target node.
  • Added debug_mode setting to refactor.context.Configuration.
  • Added a command-line flag (-d/--enable-debug-mode) to the default CLI runner to change session's configuration.
  • When unparsable source code is generated, the contents can be now seen if the debug mode is enabled.
  • [Experimental] Added ability to partially recover floating comments (from preceding or succeeding lines) bound to statements.
  • The context providers now can be accessed with attribute notation, e.g. self.context.scope instead of self.context.metadata["scope].
  • If you access a built-in context provider (scope/ancestry) and it is not already imported, we auto-import it. So most common context providers are now ready to be used.
  • Added common.next_statement_of.

0.5.0b0

06 Aug 21:15
9796869
Compare
Choose a tag to compare
0.5.0b0 Pre-release
Pre-release

What's Changed

Major

This release includes the overhaul of our action system, and with the next release we will be starting deprecating the old ones. A list
of changes:

  • refactor.core no longer contains any actions (the deprecated aliases are still imported and exposed but all the new actions go into refactor.actions)
  • Action is now split into two, a refactor.actions.BaseAction which is the base of all actions (useful for type hinting) and a refactor.actions.LazyReplace (a replace action that builds the node lazily in its build()).
  • ReplacementAction is now refactor.actions.Replace
  • NewStatementAction is now refactor.actions.LazyInsertAfter
  • TargetedNewStatementAction is now refactor.actions.InsertAfter

For migrating your code base to the new style actions, we wrote a small tool (that we also used internally), examples/deprecated_aliases.py. Feel free to try it, and let us know if the transition was seamless.

Other Changes

  • Added experimental Windows support, contributed by Hakan Celik
  • common.find_closest now takes end_lineno and end_col_offset into account. It also ensures there is at least one target node.
  • Added debug_mode setting to refactor.context.Configuration
  • Added a command-line flag (-d/--enable-debug-mode) to the default CLI runner to change session's configuration.
  • When unparsable source code is generated, the contents can be now seen if the debug mode is enabled.
  • [Experimental] Added ability to partially recover floating comments (from preceding or succeeding lines) bound to statements.
  • The context providers now can be accessed with attribute notation, e.g. self.context.scope instead of self.context.metadata["scope].
  • If you access a built-in context provider (scope/ancestry) and it is not already imported, we auto-import it. So most common context providers are now ready to be used.

0.4.4

05 Jul 09:11
Compare
Choose a tag to compare

What's Changed

New Contributors

Full Changelog: 0.4.3...0.4.4

0.4.3

01 Mar 14:22
Compare
Choose a tag to compare

What's Changed

Full Changelog: 0.4.2...0.4.3

0.4.2

23 Jan 00:36
Compare
Choose a tag to compare

What's Changed

  • Fix passing --refactor-file, allow source directories by @gerrymanoim in #22

New Contributors

Full Changelog: 0.4.1...0.4.2

0.4.1

15 Jan 18:08
Compare
Choose a tag to compare

What's Changed

Full Changelog: 0.4.0...0.4.1