Skip to content

Commit 3f22fa1

Browse files
authoredApr 22, 2022
Merge pull request #2004 from kcamsanc/in-class-method
[ISSUE 1846] Resolve indent issue with class method named in
2 parents 508e803 + 4b6ba1b commit 3f22fa1

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed
 

‎.DS_Store

8 KB
Binary file not shown.

‎js/src/javascript/tokenizer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ Tokenizer.prototype._read_word = function(previous_token) {
186186
if (!(previous_token.type === TOKEN.DOT ||
187187
(previous_token.type === TOKEN.RESERVED && (previous_token.text === 'set' || previous_token.text === 'get'))) &&
188188
reserved_word_pattern.test(resulting_string)) {
189-
if (resulting_string === 'in' || resulting_string === 'of') { // hack for 'in' and 'of' operators
189+
if ((resulting_string === 'in' || resulting_string === 'of') &&
190+
(previous_token.type === TOKEN.WORD || previous_token.type === TOKEN.STRING)) { // hack for 'in' and 'of' operators
190191
return this._create_token(TOKEN.OPERATOR, resulting_string);
191192
}
192193
return this._create_token(TOKEN.RESERVED, resulting_string);

‎python/jsbeautifier/javascript/tokenizer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,10 @@ def _read_word(self, previous_token):
267267
and (previous_token.text == "set" or previous_token.text == "get")
268268
)
269269
) and reserved_word_pattern.match(resulting_string):
270-
if resulting_string == "in" or resulting_string == "of":
270+
if (resulting_string == "in" or resulting_string == "of") and (
271+
previous_token.type == TOKEN.WORD
272+
or previous_token.type == TOKEN.STRING
273+
):
271274
# in and of are operators, need to hack
272275
return self._create_token(TOKEN.OPERATOR, resulting_string)
273276

‎test/data/javascript/tests.js

+83
Original file line numberDiff line numberDiff line change
@@ -4721,6 +4721,89 @@ exports.test_data = {
47214721
')'
47224722
]
47234723
},
4724+
{
4725+
comment: "Issue ##1846 - in keyword in class method causes indentation problem",
4726+
input: [
4727+
'class {',
4728+
' get a() {',
4729+
'\n',
4730+
' }',
4731+
'\n',
4732+
' in() {',
4733+
'\n',
4734+
' }',
4735+
'\n',
4736+
' b() {',
4737+
'\n',
4738+
' }',
4739+
'}'
4740+
],
4741+
output: [
4742+
'class {',
4743+
' get a() {',
4744+
'\n',
4745+
' }',
4746+
'\n',
4747+
' in() {',
4748+
'\n',
4749+
' }',
4750+
'\n',
4751+
' b() {',
4752+
'\n',
4753+
' }',
4754+
'}'
4755+
]
4756+
},
4757+
{
4758+
comment: "Related to Issue ##1846 - Do not indent 'in' keyword if not a class method",
4759+
input: [
4760+
'function test() {',
4761+
'for x in nums {}',
4762+
'"make" in car',
4763+
'3 in number;',
4764+
'}'
4765+
],
4766+
output: [
4767+
'function test() {',
4768+
' for x in nums {}',
4769+
' "make" in car',
4770+
' 3 in number;',
4771+
'}'
4772+
]
4773+
},
4774+
{
4775+
comment: "Related to Issue ##1846 - of keyword in class method causes indentation problem",
4776+
input: [
4777+
'class {',
4778+
' get a() {',
4779+
'\n',
4780+
' }',
4781+
'\n',
4782+
' of() {',
4783+
'\n',
4784+
' }',
4785+
'\n',
4786+
' b() {',
4787+
'\n',
4788+
' }',
4789+
'}'
4790+
],
4791+
output: [
4792+
'class {',
4793+
' get a() {',
4794+
'\n',
4795+
' }',
4796+
'\n',
4797+
' of() {',
4798+
'\n',
4799+
' }',
4800+
'\n',
4801+
' b() {',
4802+
'\n',
4803+
' }',
4804+
'}'
4805+
]
4806+
},
47244807
{
47254808
comment: 'Issue #1950: Do not remove whitespace after number - test scenario: number before a dot',
47264809
input: '1000000000000001000 .toFixed(0)!==1000000000000001024',

0 commit comments

Comments
 (0)
Please sign in to comment.