summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Crosley <timothy.crosley@gmail.com>2019-10-17 09:31:32 -0700
committerTimothy Crosley <timothy.crosley@gmail.com>2019-10-17 09:31:32 -0700
commitf33e60ec7a69709ab81971bfeaf9fa2748cc76da (patch)
tree87182aacb401e42a97bbdd4cfdfbb7b302bc02eb
parent545ec3f539da9ac17b672db6af8802b8e4567d0d (diff)
downloadisort-feature/add-ast-support.tar.gz
Clean up AST handlersfeature/add-ast-support
-rw-r--r--isort/vast/handlers.py383
1 files changed, 5 insertions, 378 deletions
diff --git a/isort/vast/handlers.py b/isort/vast/handlers.py
index fef5e176..b7ff8e55 100644
--- a/isort/vast/handlers.py
+++ b/isort/vast/handlers.py
@@ -164,10 +164,6 @@ class PassThrough(AbstractHandler):
self.code = code
@property
- def javascript(self):
- return self.code
-
- @property
def python(self):
return self.code
@@ -182,59 +178,20 @@ class Escape(Handler):
class MultiLineComment(Handler):
accept_children = Router((Escape, '/'))
- def _javascript(self):
- return '/* {0} */'.format(self.javascript_content)
-
def _python(self):
return '"""{0}"""'.format(self.python_content)
-class JavaDocComment(MultiLineComment):
-
- def _javascript(self):
- return '/**\n{0}\n */'.format(self.javascript_content)
-
- def _python(self):
- return '""""\n{0}\n"""'.format(self.python_content)
-
-
-@routes.add('/**\n')
-class JavaScriptJavaDoc(JavaDocComment):
- end_on = '\n */'
-
-
-@routes.add('""""\n')
-class PythonJavaDoc(JavaDocComment):
- end_on = '\n"""'
-
-
@routes.add('"""')
class PythonComment(MultiLineComment):
end_on = '"""'
-@routes.add('/* ')
-class JavaScriptComment(MultiLineComment):
- end_on = ' */'
-
-
@routes.add("'''")
class String(Handler):
accept_children = Router((Escape, '\\'))
end_on = "'''"
- def _javascript(self):
- content = self.javascript_content.split("\n")
- if len(content) <= 1:
- return Handler._javascript(self)
-
- output = []
- for line in content[:-1]:
- output.append("'{0}\\n' +".format(line))
-
- output.append("'{0}'".format(content[-1]))
- return "\n".join(output)
-
@routes.add("'")
class BasicString(Handler):
@@ -247,11 +204,7 @@ class PythonBasicString(Handler):
accept_children = Router((Escape, '\\'))
end_on = '"'
- def _javascript(self):
- return ("'{0}'".format(self.javascript_content))
-
-
-@routes.add('# ', '// ')
+@routes.add('# ')
class SingleLineComment(Handler):
accept_children = Router()
end_on = "\n"
@@ -268,56 +221,7 @@ class PythonImport(Handler):
return 1
return 0
- def _javascript(self):
- content = self.python_content.split(" ")
- to_import = content[0]
- if "as" in content:
- variable_name = content[content.index('as') + 1]
- else:
- variable_name = to_import.split("/")[-1]
-
- if self.ended_on == " #":
- self.ended_on = " "
-
- return "var {0} = require('{1}');{2}".format(variable_name, to_import, self.ended_on)
-
-
-@routes.add("): pass\n", ") {}\n")
-class Pass(Handler):
- javascript = ') {}\n'
- python = '): pass\n'
-
-
-@routes.add("True", "true")
-class TrueStatement(Handler):
- javascript = 'true'
- python = 'True'
-
-
-@routes.add("False", "false")
-class FalseStatement(Handler):
- javascript = 'false'
- python = 'False'
-
-
-@routes.add('None', 'null')
-class NoneStatement(Handler):
- javascript = 'null'
- python = 'None'
-
-
-@routes.add('print', 'console.log')
-class PrintFunction(Handler):
- javascript = 'console.log'
- python = 'print'
-
-
-@routes.add('var ')
-class JavascriptSetter(Handler):
- accept_children = Router()
-
-
-@routes.add('function ', 'def ')
+@routes.add('def ')
class Function(Handler):
end_on = ("):\n", ") {\n")
@@ -335,42 +239,10 @@ class Function(Handler):
return to_return
- def _javascript(self):
- return "function " + self.javascript_content
-
-@routes.add('^function(', '^def(')
+@routes.add('^def(')
class AnonymousFunction(Function):
-
- def _python(self):
- to_return = self.leading_whitespace + "def(" + self.python_content
- if self.ended_on == self.end_on[0]:
- to_return += ")"
-
- return to_return
-
- def _javascript(self):
- return self.leading_whitespace + "function(" + self.javascript_content
-
-
-@routes.add('=def ')
-class ExportFunction(Handler):
- end_on = "):\n"
- back_track = 2
-
- def _python(self):
- return Handler._python(self)[:-2]
-
- def _javascript(self):
- parts = self.javascript_content.split("(")
- return "module.exports.{0} = function({1}".format(parts[0], "(".join(parts[1:]),
- self.javascript_content.split("("))
-
-
-@routes.add('del ', 'delete ')
-class DeleteStatement(Handler):
- javascript = 'delete '
- python = 'del '
+pace + "function(" + self.javascript_content
@routes.add('if (', 'if ')
@@ -384,24 +256,6 @@ class IfStatement(Handler):
else:
return 4
- def _python(self):
- to_return = "if {0}".format(self.python_content)
- if self.ended_on == "):\n":
- to_return += ")"
-
- return to_return
-
- def _javascript(self):
- if self.ended_on == ":\n" and self.started_on == 'if (':
- to_return = "if (({0}".format(self.javascript_content)
- else:
- to_return = "if ({0}".format(self.javascript_content)
-
- if self.ended_on == "):\n":
- to_return += ")"
-
- return to_return
-
@routes.add('for (', 'for ')
class ForStatement(Handler):
@@ -449,183 +303,14 @@ class ElseStatement(Handler):
return self.leading_whitespace + '} else {\n'
-class Block(Handler):
-
- def handle(self):
- self.indent = ''
- line = self.parser.text_after(self.started_at - 2, '\n')
- for character in line:
- if character in (" ", "\t"):
- self.indent += character
- else:
- break
-
-
-@routes.add('):\n', ':\n')
-class PythonBlock(Block):
- end_on = ('\n\n', ')', ' , ')
- javascript_start_with = ") {\n"
-
- @property
- def back_track(self):
- if self.ended_on == ")" and isinstance(self.parent, Parens):
- return 1
-
- def _javascript(self):
- content = self.javascript_start_with + self.javascript_content.rstrip(" ").rstrip("\t")
-
- extra = ""
- if self.ended_on == self.end_on[1]:
- extra = ")"
- elif self.ended_on == self.end_on[2]:
- extra = ", "
-
- last_child = self.last_child
- if last_child:
- last_child_index = len(self.children) - 1
- while (not self.children[last_child_index].javascript.strip() and not
- isinstance(last_child, PythonNoop) and last_child_index > 0):
- self.children[last_child_index] = PassThrough('', self)
- last_child_index -= 1
- last_child = self.children[last_child_index]
- if isinstance(last_child, PythonBlock) and last_child.javascript.endswith("\n"):
- content += self.indent + "}" + extra
- if(isinstance(self.prev, (ExportFunction, AnonymousFunction)) and not self.ahead() in (".", "(", ";")
- and not extra == ", "):
- content += ";"
- if not extra:
- content += "\n\n"
- return content
- if(not self.javascript_content.replace("\n", "").strip().endswith(";") and not
- isinstance(last_child, (PythonNoop, EndOfLine, SingleLineComment)) and not self.ahead() in
- (".", "(", ";") and not extra == ", "):
- if content[-1] == "\n":
- content = content[:-1] + ";" + "\n"
- else:
- content += ";"
-
- if extra:
- content += self.indent + "}" + extra
- else:
- content += "\n" + self.indent + "}"
- if(isinstance(self.prev, (ExportFunction, AnonymousFunction)) and not self.ahead() in (".", "(", ";")
- and not extra == ", "):
- content += ";"
-
- if not extra:
- content += "\n"
-
- return content
-
-
@routes.add('try:\n')
class PythonTry(PythonBlock):
- javascript_start_with = "try {\n"
-
-
-@routes.add(') {\n', 'try {\n')
-class JavascriptBlock(Block):
- end_on = ("};", "}")
- python_start_with = ":\n"
-
- def _python(self):
- if not self.python_content.strip():
- to_return = "{0}{1}pass\n\n".format(self.python_start_with, self.indent + " ")
- else:
- to_return = "{0}{1}".format(self.python_start_with, self.python_content)
- if isinstance(self.prev, Function):
- to_return = ")" + to_return
-
- return to_return
-
-
-@routes.add('try {\n')
-class JavascriptTry(JavascriptBlock):
- python_start_with = "try:\n"
+ pass
@routes.add('(')
class Parens(Handler):
end_on = ")"
- yield_for = ") {\n"
-
- def _javascript(self):
- if isinstance(self.last_child, PythonBlock) and self.last_child.ended_on == ")":
- return "(" + self.javascript_content
-
- return Handler._javascript(self)
-
- def _python(self):
- if isinstance(self.last_child, PythonBlock) and self.last_child.ended_on == ")":
- return "(" + self.python_content
-
- return Handler._python(self)
-
-
-@routes.add('{')
-class Dictionary(Handler):
- end_on = '}'
-
-
-@routes.add(' is not ', ' !== ')
-class IsNotStatement(Handler):
- javascript = ' !== '
- python = ' is not '
-
-
-@routes.add(' is ', ' === ')
-class IsStatement(Handler):
- javascript = ' === '
- python = ' is '
-
-
-@routes.add(' not ', ' !')
-class NotStatement(Handler):
- javascript = ' !'
- python = ' not '
-
-
-@routes.add('not ', '!')
-class NotStatement(Handler):
- python = 'not '
- javascript = '!'
-
-
-@routes.add('Unset', 'undefined')
-class Unset(Handler):
- javascript = 'undefined'
- python = 'Unset'
-
-
-@routes.add(' and ', ' && ')
-class AndKeyword(Handler):
- javascript = ' && '
- python = ' and '
-
-
-@routes.add(' or ', ' || ')
-class OrKeyword(Handler):
- javascript = ' || '
- python = 'or '
-
-
-@routes.add('pass\n')
-class PythonNoop(Handler):
- javascript = '\n'
-
-
-@routes.add(';\n')
-class EndOfStatement(Handler):
- back_track = 1
-
- def _python(self):
- return ''
-
- def _javascript(self):
- if self.behind() in ('\n', ' ', '\t', '/') or isinstance(self.prev, MultiLineComment):
- return ''
-
- return ';'
@routes.add('\n')
@@ -648,48 +333,6 @@ class EndOfLine(Handler):
return ';\n'
-@routes.add('^String', '^str', '(String', '(str')
-class String(Handler):
-
- def handle(self):
- if self.started_on.startswith("("):
- self.leading_whitespace = "("
-
- def _python(self):
- return self.leading_whitespace + 'str'
-
- def _javascript(self):
- return self.leading_whitespace + 'String'
-
-
-@routes.add('^Boolean', '^bool', '(Boolean', '(bool')
-class Boolean(Handler):
-
- def handle(self):
- if self.started_on.startswith("("):
- self.leading_whitespace = "("
-
- def _python(self):
- return self.leading_whitespace + 'bool'
-
- def _javascript(self):
- return self.leading_whitespace + 'Boolean'
-
-
-@routes.add('^Number', '^int', '(Number', '(int')
-class Number(Handler):
-
- def handle(self):
- if self.started_on.startswith("("):
- self.leading_whitespace = "("
-
- def _python(self):
- return self.leading_whitespace + 'int'
-
- def _javascript(self):
- return self.leading_whitespace + 'Number'
-
-
@routes.add('@')
class Decorator(Handler):
end_on = ('\n', ')\n')
@@ -698,22 +341,6 @@ class Decorator(Handler):
(parts, _) = self.parser.text_till(("("), keep_index=True)
self.function_name = parts.split()[-1]
- def _javascript(self):
- return "{1} = {0}({1});\n".format(Handler._javascript(self)[1:-1], self.function_name)
-
-
-@routes.add('.append(', '.push(')
-class Append(Handler):
- javascript = '.push'
- python = '.append'
- back_track = 1
-
-
-@routes.add('raise ', 'throw ')
-class Append(Handler):
- javascript = 'throw '
- python = 'raise '
-
no_nested_parens = routes.excluding('(')
IfStatement.accept_children = no_nested_parens