Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer-string-starts-ends-with: add suggestions for safely handling non-strings #1277

Merged

Conversation

bmish
Copy link
Contributor

@bmish bmish commented May 14, 2021

Fixes #1267.

@bmish bmish force-pushed the prefer-string-starts-ends-with-suggestions branch from 991a7c3 to 458dfdb Compare May 14, 2021 06:45
@fisker
Copy link
Collaborator

fisker commented May 14, 2021

Use generator function instead of return array, should make function more clear

@bmish bmish changed the title prefer-string-starts-ends-with: add suggestions for handling non-existent strings prefer-string-starts-ends-with: add suggestions for safely handling non-strings May 14, 2021
bmish and others added 5 commits May 14, 2021 12:04
* main:
  Temporarily disable `import/extensions` rule
  Update `xo` to v0.40 (sindresorhus#1257)
  Test: Better `output` assertion (sindresorhus#1259)
  `prefer-negative-index`: Refactor (sindresorhus#1255)
  `prefer-dom-node-remove`: Improve parentheses handling (sindresorhus#1254)
  Update `eslint-plugin-eslint-plugin` to v3.0.3 (sindresorhus#1256)
  32.0.1
  `prefer-set-has`: Remove outdated AST types (sindresorhus#1253)
  `prevent-abbreviations`: Fix shorthand import/export detection (sindresorhus#1252)
  32.0.0
  `no-for-loop`: Ignore known non-array loop variables (sindresorhus#1242)
  Add `no-document-cookie` rule (sindresorhus#1244)
  `prefer-array-find`: Singularize variable name in autofix (sindresorhus#1243)
  Update `@babel/core` (sindresorhus#1241)
  Mention libraries used under the hood in `better-regexp` rule doc (sindresorhus#1240)
  `prefer-switch`: Do not add braces to the default case (sindresorhus#1235)
@fisker
Copy link
Collaborator

fisker commented May 15, 2021

The code fix part still need more work , if you don't mind, I can fix it later, unless you prefer do it yourself, I can guide.

Another thing, I think we should remove auto fix for known non-strings, yes auto-fix will throw on runtime, but not every line is test covered, or even not reachable. You need fix this.

@bmish
Copy link
Contributor Author

bmish commented May 15, 2021

@fisker I opened a separate PR for ignore known non-strings: #1281 I think we should merge that first since that can be done independently of this suggestion PR.

@bmish
Copy link
Contributor Author

bmish commented May 15, 2021

@fisker feel free to push tweaks if you want. I will keep working on it too, appreciate your guidance!

@fisker
Copy link
Collaborator

fisker commented May 17, 2021

I pushed some changes, this corrects the fix logic, this add missing parens when the target is ternary (a ? b : c ?? '').startsWith -> ((a ? b : c) ?? '').startsWith, see #1277 (comment).

@fisker
Copy link
Collaborator

fisker commented May 18, 2021

@bmish Can you fix conflicts?

@fisker fisker marked this pull request as ready for review May 18, 2021 07:16
@fisker
Copy link
Collaborator

fisker commented May 18, 2021

Removed suggestions for known string.

Updated message, I'm not good at English, but I think it's more useful.

Copy link
Collaborator

@fisker fisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good, few other comments.

Thanks @bmish

Co-authored-by: fisker Cheung <lionkay@gmail.com>
// Goal: `String(target).startsWith(pattern)`
case FIX_TYPE_STRING_CASTING:
// `target` was a call argument, don't need check parentheses
targetText = `String(${targetText})`;
Copy link
Contributor Author

@bmish bmish May 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds extra parenthesis that aren't needed sometimes. I think I had code to handle this before.

in: (/^b/).test((a))
out: ((a)).startsWith('b')

Not a big deal though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that, but better "add missing parens" instead of "remove extra parens".

@bmish
Copy link
Contributor Author

bmish commented May 18, 2021

@fisker looks good, thanks for helping to finish it up.

@sindresorhus sindresorhus merged commit f14a9d1 into sindresorhus:main May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prefer-string-starts-ends-with: incorrect autofix when string does not exist
3 participants