-
-
Notifications
You must be signed in to change notification settings - Fork 30
New: rule prefer-replace-text #50
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83b072f
New: rule prefer-replace-text (fixes #47).
aladdin-add f837f63
todo: docs.
aladdin-add b2ce85a
Docs: add prefer-replace-range.md.
aladdin-add dd06a0d
Fix: add type check.
aladdin-add 8854d4d
chore: update comment.
aladdin-add 7248b91
Update prefer-replace-range.md
aladdin-add d54b049
Update prefer-replace-range.md
aladdin-add aa1d01c
Update prefer-replace-range.md
aladdin-add 4bebb6f
Fix: reveiew suggestions.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# prefer using replaceText instead of replaceTextRange. (prefer-replace-text) | ||
|
||
## Rule Details | ||
|
||
The rule reports an error if `replaceTextRange`'s first argument is an array of identical array elements. It can be easily replaced by `replaceText` to improve readability. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/prefer-text-range: error */ | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
// error, can be writen: return fixer.replaceText([node, ''); | ||
return fixer.replaceTextRange([node.range[0], node.range[1]], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
/* eslint eslint-plugin/prefer-text-range: error */ | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceText(node, ''); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
// start = ... | ||
// end = ... | ||
return fixer.replaceTextRange([start, end], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
``` | ||
|
||
## Further Reading | ||
|
||
* [Applying Fixes](https://eslint.org/docs/developer-guide/working-with-rules#applying-fixes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* @fileoverview prefer using replaceText instead of replaceTextRange. | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const utils = require('../utils'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'prefer using replaceText instead of replaceTextRange.', | ||
category: 'Rules', | ||
recommended: false, | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
|
||
create (context) { | ||
const sourceCode = context.getSourceCode(); | ||
const message = 'prefer using replaceText instead of replaceTextRange.'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: I think it would be better to make the message: const message = 'Use replaceText instead of replaceTextRange.'; |
||
let funcInfo = { | ||
upper: null, | ||
codePath: null, | ||
shouldCheck: false, | ||
node: null, | ||
}; | ||
let contextIdentifiers; | ||
|
||
return { | ||
Program (node) { | ||
contextIdentifiers = utils.getContextIdentifiers(context, node); | ||
}, | ||
|
||
// Stacks this function's information. | ||
onCodePathStart (codePath, node) { | ||
const parent = node.parent; | ||
const shouldCheck = (node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression') && | ||
parent.parent.type === 'ObjectExpression' && | ||
parent.parent.parent.type === 'CallExpression' && | ||
contextIdentifiers.has(parent.parent.parent.callee.object) && | ||
parent.parent.parent.callee.property.name === 'report' && | ||
utils.getReportInfo(parent.parent.parent.arguments).fix === node; | ||
|
||
funcInfo = { | ||
upper: funcInfo, | ||
codePath, | ||
shouldCheck, | ||
node, | ||
}; | ||
}, | ||
|
||
// Pops this function's information. | ||
onCodePathEnd () { | ||
funcInfo = funcInfo.upper; | ||
}, | ||
|
||
// Checks the replaceTextRange arguments. | ||
'CallExpression[arguments.length=2]' (node) { | ||
if (funcInfo.shouldCheck && | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.property.name === 'replaceTextRange') { | ||
const arg = node.arguments[0]; | ||
const isIdenticalNodeRange = arg.type === 'ArrayExpression' && | ||
arg.elements[0].type === 'MemberExpression' && arg.elements[1].type === 'MemberExpression' && | ||
sourceCode.getText(arg.elements[0].object) === sourceCode.getText(arg.elements[1].object); | ||
if (isIdenticalNodeRange) { | ||
context.report({ | ||
node, | ||
message, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/** | ||
* @fileoverview prefer using replaceText instead of replaceTextRange | ||
* @author 薛定谔的猫<[email protected]> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/prefer-replace-text'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
const ERROR = { message: 'prefer using replaceText instead of replaceTextRange.' }; | ||
|
||
|
||
ruleTester.run('prefer-placeholders', rule, { | ||
valid: [ | ||
` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceTextRange([start, end], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceTextRange([node1[0], node2[1]], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
` | ||
module.exports = { | ||
create(context) {} | ||
}; | ||
`, | ||
` | ||
module.exports = { | ||
create(context) { | ||
var fixer = function(fixer) { | ||
return fixer.replaceTextRange([node.range[0], node.range[1]], ''); | ||
} | ||
} | ||
}; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceTextRange([node.range[0], node.range[1]], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
{ | ||
code: ` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix: function(fixer) { | ||
return fixer.replaceTextRange([node.range[0], node.range[1]], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
{ | ||
code: ` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix: function(fixer) { | ||
if (foo) {return fixer.replaceTextRange([node.range[0], node.range[1]], '')} | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
{ | ||
code: ` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix: fixer => fixer.replaceTextRange([node.range[0], node.range[1]], '') | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
{ | ||
code: ` | ||
module.exports = { | ||
create(context) { | ||
context.report({ | ||
fix(fixer) { | ||
return fixer.replaceTextRange([node.start, node.end], ''); | ||
} | ||
}); | ||
} | ||
}; | ||
`, | ||
errors: [ERROR], | ||
}, | ||
], | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: the word "written" is spelled incorrectly here. Also, the comment here is missing a
]
.