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-ternary: Refactor to use ESLint recommended way to extend fix range #857

Merged
merged 5 commits into from Dec 12, 2020

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Oct 13, 2020

ESLint recommended a new way to extend fix range.

@fisker
Copy link
Collaborator Author

fisker commented Oct 13, 2020

@mdjermanovic After I tried the way you recommended, I figure I can simplify the logic to this 404860c

function * extendFixRange(range = []) {
	const [start = 0, end = Infinity] = range;
	yield {range: [start, start], text: ''};
	yield {range: [end, end], text: ''};
}

So, I don't need pass fixer to this function, do you think it's good?

@futpib
Copy link
Collaborator

futpib commented Oct 13, 2020

I don't think it's good. { range, text } objects are not public API for ESLint.

@mdjermanovic
Copy link

ESLint recommended a new way to extend fix range.

It isn't official yet. The applying fixes spec was intentionally not too specific in order to allow for future internal optimizations. It's arguable whether eslint/eslint#13748 documents too many internal details and thus adds some implementation constraints that may not reach consensus.

Nevertheless, I'd say it's pretty safe to use this pattern, unlike requiring the FixTracker module which almost certainly won't make it into the public API when we add the exports field in v8.0.0 (eslint/eslint#13654).

@fisker
Copy link
Collaborator Author

fisker commented Oct 13, 2020

@mdjermanovic

Thanks for the quick reply.

I'd say your suggestted implementation in eslint/eslint#13748 is very good, just two lines in fix function.

But I'm going to add this as a utility function, and I think most cases we need in our codebase is extend the range to the whole file, so yield * extendFixRange(fixer, context.getSourceCode().ast.range); still seems too complicated to use.

@futpib Do you insist to use public fixer api?

rules/utils/extend-fix-range.js Outdated Show resolved Hide resolved
rules/utils/extend-fix-range.js Outdated Show resolved Hide resolved
This reverts commit 404860c.
@fisker
Copy link
Collaborator Author

fisker commented Oct 13, 2020

@mdjermanovic Thank you very much, I restored the changes.

@sindresorhus
Copy link
Owner

What's left for this to be mergable?

@fisker
Copy link
Collaborator Author

fisker commented Dec 9, 2020

I don't think there is anything to do, unless you have a better idea to make it easier to use. We may need check other rules. when generating a new variable, should use this function to avoid conflicts with other rules(ours and other plugins).

@sindresorhus sindresorhus merged commit 90fc7b0 into sindresorhus:master Dec 12, 2020
@fisker fisker deleted the extend-fix-range branch December 12, 2020 15:20
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.

None yet

4 participants