我正在尝试使用babel模块,babylon
,babel-traverse
。当我尝试替换节点时,程序与Maximum call stack size exceeded
崩溃。这是我的代码
import * as babylon from 'babylon'
import traverse from 'babel-traverse'
import generate from 'babel-generator'
import * as t from 'babel-types'
const code = `
import a from 'b'
n === 3
`
const ast = babylon.parse(code, {
sourceType: 'module'
})
const visitor = {
BinaryExpression(path) {
console.log((path.node))
path.replaceWith(t.binaryExpression('**', t.numericLiteral(3), t.numericLiteral(4)))
}
}
traverse(ast, visitor)
let generated = generate(ast, null, code)
console.log(generated.code)
我正在使用以下babel依赖,任何想法?
"dependencies": {
"babel-generator": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-traverse": "^6.9.0",
"babel-types": "^6.9.1",
"babylon": "^6.8.0"
}
答案 0 :(得分:2)
在BinaryExpression上有访问者时添加新的binaryExpression会导致访问新的binaryExpression,因此这是一个无限循环。您可以在Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
File "<frozen importlib._bootstrap>", line 991, in _find_and_load
File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\progrma\backend\mysite\myapp\models.py", line 12, in <module>
class Myapps(models.Model):
File "D:\progrma\backend\mysite\myapp\models.py", line 16, in Myapps
skill = models.EmbeddedField(
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\models\fields.py", line 225, in __init__
super().__init__(model_container, *args, **kwargs)
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\models\fields.py", line 87, in __init__
self._validate_container()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\djongo\models\fields.py", line 91, in _validate_container
for field in self.model_container._meta.get_fields():
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\options.py", line 707, in _relation_tree
return self._populate_directed_relation_graph()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\options.py", line 678, in _populate_directed_relation_graph
all_models = self.apps.get_models(include_auto_created=True)
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\apps\registry.py", line 178, in get_models
self.check_models_ready()
File "C:\Users\rupek\AppData\Local\Programs\Python\Python38\lib\site-packages\django\apps\registry.py", line 140, in check_models_ready
raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
之后致电path.skip
,以跳过对新表达式的访问。
from djongo import models
class Skill(models.Model):
skill1 = models.CharField(max_length=30)
skill2 = models.CharField(max_length=30)
class Meta:
abstract = True
class Myapps(models.Model):
age = models.IntegerField()
idd = models.IntegerField()
name = models.CharField(max_length=30)
skill = models.EmbeddedField(
model_container=Skill,
)
答案 1 :(得分:1)
您收到堆栈错误的原因是您的二进制表达式替换生成另一个二进制表达式。您的替换二进制表达式一次又一次地被反馈到访问者中。
要打破无限循环,您需要检查您的替换是否已经发生,可能会在您看到运算符为replaceWith
时忽略**
步骤。