-
Notifications
You must be signed in to change notification settings - Fork 459
Improved javascript template string expression extracting #792
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Open
gitaarik
wants to merge
1
commit into
python-babel:master
Choose a base branch
from
gitaarik:javascript-extract-improvements
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
:license: BSD, see LICENSE for more details. | ||
""" | ||
|
||
import io | ||
import os | ||
from os.path import relpath | ||
import sys | ||
|
@@ -521,8 +522,10 @@ def extract_javascript(fileobj, keywords, comment_tags, options): | |
:param options: a dictionary of additional options (optional) | ||
Supported options are: | ||
* `jsx` -- set to false to disable JSX/E4X support. | ||
* `template_string` -- set to false to disable ES6 | ||
template string support. | ||
* `template_string` -- if `True`, supports gettext(`key`) | ||
* `parse_template_string` -- if `True` will parse the | ||
contents of javascript | ||
template strings. | ||
""" | ||
from babel.messages.jslexer import Token, tokenize, unquote_string | ||
funcname = message_lineno = None | ||
|
@@ -551,7 +554,11 @@ def extract_javascript(fileobj, keywords, comment_tags, options): | |
call_stack = 0 | ||
token = Token('operator', ')', token.lineno) | ||
|
||
if token.type == 'operator' and token.value == '(': | ||
if options.get('parse_template_string') and not funcname and token.type == 'template_string': | ||
for item in parse_template_string(token.value, fileobj, keywords, comment_tags, options): | ||
yield item | ||
|
||
elif token.type == 'operator' and token.value == '(': | ||
if funcname: | ||
message_lineno = token.lineno | ||
call_stack += 1 | ||
|
@@ -643,3 +650,43 @@ def extract_javascript(fileobj, keywords, comment_tags, options): | |
funcname = token.value | ||
|
||
last_token = token | ||
|
||
|
||
def parse_template_string(template_string, fileobj, keywords, comment_tags, options): | ||
|
||
prev_character = None | ||
level = 0 | ||
inside_str = False | ||
expression_contents = '' | ||
|
||
for character in template_string[1:-1]: | ||
|
||
if not inside_str and character in ('"', "'", '`'): | ||
inside_str = character | ||
elif inside_str == character and prev_character != r'\\': | ||
inside_str = False | ||
|
||
if level: | ||
expression_contents += character | ||
|
||
if not inside_str: | ||
|
||
if character == '{' and prev_character == '$': | ||
level += 1 | ||
|
||
elif level and character == '}': | ||
|
||
level -= 1 | ||
|
||
if level == 0 and expression_contents: | ||
|
||
expression_contents = expression_contents[0:-1] | ||
|
||
fake_file_obj = io.BytesIO(expression_contents.encode()) | ||
|
||
for item in extract_javascript(fake_file_obj, keywords, comment_tags, options): | ||
yield item | ||
|
||
expression_contents = '' | ||
|
||
prev_character = character | ||
Comment on lines
+655
to
+692
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you remove the empty lines within the blocks, so the spacing of this code (which is quite airy 😄 ) matches the rest of the codebase? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in #939. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this interact with e.g. line numbering?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line numbering has been fixed in #939. There is now also a test for that.