Is there a way to parse a template to AST without registering all functions et.al? #540
-
Hey! I have a question that you maybe know the answer too already or might be able to direct me towards. I'm building an internal tool (maybe public in the future) where we want to be able to extract translations from twig templates within WordPress projects. This means finding references to the WordPress translation methods ( Previously we've used a regex, but it has proven not to be very safe. So I'm looking into AST parsing and walking the tree to find calls to certain functions. But the thing is, I want to make this tool independent of what each project uses, therefore I have no idea which functions and other extensions the current project has enabled. Then if I try to parse a file errors will be thrown for all unknown functions. Which is not very useful. And it would require too much configuration to repeat all defined functions/extensions etc. per project. This is a sample of the code I have that will throw errors: let loader = new TwingLoaderNull();
let twing = new TwingEnvironment(loader);
let source = new TwingSource('{{ __("Hello", "domain") }} world!', '');
let stream = twing.tokenize(source);
let ast = twing.parse(stream); I also found a reference in an issue to you project Is it possible today to parse source code without throwing errors for unknown functions? Or could you maybe direct me towards something that would lead me towards it? Maybe I need to write my own |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@adambrgmn , I've faced this issue as well and I think it is a bad design decision from the language itself to allow for global functions: they create a confusion between the language semantic and the context passed at runtime. Anyway, I have been wanting for long to add an option to the parser to prevent it from throwing an error when an unknown function or filter is found. I think it is time. |
Beta Was this translation helpful? Give feedback.
@adambrgmn , I've faced this issue as well and I think it is a bad design decision from the language itself to allow for global functions: they create a confusion between the language semantic and the context passed at runtime.
Anyway, I have been wanting for long to add an option to the parser to prevent it from throwing an error when an unknown function or filter is found. I think it is time.