Skip to content

Commit

Permalink
[Refactor] use slice instead of substring
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 29, 2023
1 parent 1629d44 commit 26e9578
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ module.exports = {
if (parentType === 'JSXAttribute') {
textToReplace = `"${expressionType === 'TemplateLiteral'
? expression.quasis[0].value.raw
: expression.raw.substring(1, expression.raw.length - 1)
: expression.raw.slice(1, -1)
}"`;
} else if (jsxUtil.isJSX(expression)) {
const sourceCode = context.getSourceCode();
Expand Down Expand Up @@ -222,7 +222,7 @@ module.exports = {

const expression = literalNode.parent.type === 'JSXAttribute'
? `{"${escapeDoubleQuotes(escapeBackslashes(
literalNode.raw.substring(1, literalNode.raw.length - 1)
literalNode.raw.slice(1, -1)
))}"}`
: wrapWithCurlyBraces(literalNode.raw);

Expand Down
6 changes: 3 additions & 3 deletions lib/rules/jsx-sort-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function generateFixerFunction(node, context, reservedList) {
sortableAttributeGroups.forEach((sortableGroup, ii) => {
sortableGroup.forEach((attr, jj) => {
const sortedAttr = sortedAttributeGroups[ii][jj];
const sortedAttrText = source.substring(sortedAttr.range[0], attributeMap.get(sortedAttr).end);
const sortedAttrText = source.slice(sortedAttr.range[0], attributeMap.get(sortedAttr).end);
fixers.push({
range: [attr.range[0], attributeMap.get(attr).end],
text: sortedAttrText,
Expand All @@ -266,10 +266,10 @@ function generateFixerFunction(node, context, reservedList) {
const rangeEnd = firstFixer ? firstFixer.range[1] : -0;

fixers.forEach((fix) => {
source = `${source.substr(0, fix.range[0])}${fix.text}${source.substr(fix.range[1])}`;
source = `${source.slice(0, fix.range[0])}${fix.text}${source.slice(fix.range[1])}`;
});

return fixer.replaceTextRange([rangeStart, rangeEnd], source.substr(rangeStart, rangeEnd - rangeStart));
return fixer.replaceTextRange([rangeStart, rangeEnd], source.slice(rangeStart, rangeEnd));
};
}

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unescaped-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ module.exports = {
if (i === node.loc.end.line) {
end = node.loc.end.column;
}
rawLine = rawLine.substring(start, end);
rawLine = rawLine.slice(start, end);
for (let j = 0; j < entities.length; j++) {
for (let index = 0; index < rawLine.length; index++) {
const c = rawLine[index];
Expand Down
6 changes: 2 additions & 4 deletions lib/util/propTypesSort.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,8 @@ function fixPropTypesSort(
source = nodes.reduceRight((acc, attr, index) => {
const sortedAttr = sortedAttributes[index];
const sourceCodeText = sourceCode.getText();
let sortedAttrText = sourceCodeText.substring(
commentnodeMap.get(sortedAttr).start,
commentnodeMap.get(sortedAttr).end
);
const commentNode = commentnodeMap.get(sortedAttr);
let sortedAttrText = sourceCodeText.slice(commentNode.start, commentNode.end);
if (sortShapeProp && isShapeProp(sortedAttr.value)) {
const shape = getShapeProperties(sortedAttr.value);
if (shape) {
Expand Down

0 comments on commit 26e9578

Please sign in to comment.