silvermine / eslint-plugin-silvermine

MIT License
2 stars 7 forks source link

fluent-chaining rule error for chained methods with array access #54

Open yokuze opened 3 years ago

yokuze commented 3 years ago

The fluent-chaining rule emits this error message:

Identifier "undefined" should be on a new line

for code that contains both chained method calls and array access. For example:

url = url
   // Remove any query string:
   .split('?')[0]
   // Remove any hash:
   .split('#')[0];

The above code strips the query string and hash from a URL. Both of the [0] array access statements trigger an error for the fluent-chaining rule with the error message "Identifier "undefined" should be on a new line".

True, we could rewrite this as:

// Remove any query string:
url = url.split('?')[0];
// Remove any hash:
url = url.split('#')[0];

but personally I like the simplicity of the chained method calls, particularly if there were more needed.

Should we support this pattern?

If so, we need to modify the fluent-chaining rule to support this case.

If not, we probably need to fix the error message so that it doesn't print "undefined" where it seems it should print some identifier name. Additionally, when you apply ESLint's suggested auto-fix, you get:

url = url
   // Remove any query string
   .split('?')
   .0]

which removes one of the array access statement's braces and creates invalid JavaScript.

jthomerson commented 3 years ago

@yokuze I agree we should support this usecase in our rule