Skip to content

Commit

Permalink
Merge pull request #2004 from kcamsanc/in-class-method
Browse files Browse the repository at this point in the history
[ISSUE 1846] Resolve indent issue with class method named in
  • Loading branch information
bitwiseman committed Apr 22, 2022
2 parents 508e803 + 4b6ba1b commit 3f22fa1
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
3 changes: 2 additions & 1 deletion js/src/javascript/tokenizer.js
Expand Up @@ -186,7 +186,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
if (!(previous_token.type === TOKEN.DOT ||
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
reserved_word_pattern.test(resulting_string)) {
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
if ((resulting_string === 'in' || resulting_string === 'of') &&
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
return this._create_token(TOKEN.OPERATOR, resulting_string);
}
return this._create_token(TOKEN.RESERVED, resulting_string);
Expand Down
5 changes: 4 additions & 1 deletion python/jsbeautifier/javascript/tokenizer.py
Expand Up @@ -267,7 +267,10 @@ def _read_word(self, previous_token):
and (previous_token.text == "set" or previous_token.text == "get")
)
) and reserved_word_pattern.match(resulting_string):
if resulting_string == "in" or resulting_string == "of":
if (resulting_string == "in" or resulting_string == "of") and (
previous_token.type == TOKEN.WORD
or previous_token.type == TOKEN.STRING
):
# in and of are operators, need to hack
return self._create_token(TOKEN.OPERATOR, resulting_string)

Expand Down
83 changes: 83 additions & 0 deletions test/data/javascript/tests.js
Expand Up @@ -4721,6 +4721,89 @@ exports.test_data = {
')'
]
},
{
comment: "Issue ##1846 - in keyword in class method causes indentation problem",
input: [
'class {',
' get a() {',
'\n',
' }',
'\n',
' in() {',
'\n',
' }',
'\n',
' b() {',
'\n',
' }',
'}'
],
output: [
'class {',
' get a() {',
'\n',
' }',
'\n',
' in() {',
'\n',
' }',
'\n',
' b() {',
'\n',
' }',
'}'
]
},
{
comment: "Related to Issue ##1846 - Do not indent 'in' keyword if not a class method",
input: [
'function test() {',
'for x in nums {}',
'"make" in car',
'3 in number;',
'}'
],
output: [
'function test() {',
' for x in nums {}',
' "make" in car',
' 3 in number;',
'}'
]
},
{
comment: "Related to Issue ##1846 - of keyword in class method causes indentation problem",
input: [
'class {',
' get a() {',
'\n',
' }',
'\n',
' of() {',
'\n',
' }',
'\n',
' b() {',
'\n',
' }',
'}'
],
output: [
'class {',
' get a() {',
'\n',
' }',
'\n',
' of() {',
'\n',
' }',
'\n',
' b() {',
'\n',
' }',
'}'
]
},
{
comment: 'Issue #1950: Do not remove whitespace after number - test scenario: number before a dot',
input: '1000000000000001000 .toFixed(0)!==1000000000000001024',
Expand Down

0 comments on commit 3f22fa1

Please sign in to comment.