Skip to content

Commit

Permalink
[Refactor] avoid making a holey array
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jul 4, 2024
1 parent 3c1d520 commit eda77f3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
8 changes: 5 additions & 3 deletions lib/rules/jsx-closing-bracket-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
'use strict';

const has = require('object.hasown/polyfill')();
const repeat = require('string.prototype.repeat');

const docsUrl = require('../util/docsUrl');
const getSourceCode = require('../util/eslint').getSourceCode;
const report = require('../util/report');
Expand Down Expand Up @@ -168,7 +170,7 @@ module.exports = {
function getIndentation(tokens, expectedLocation, correctColumn) {
const newColumn = correctColumn || 0;
let indentation;
let spaces = [];
let spaces = '';
switch (expectedLocation) {
case 'props-aligned':
indentation = /^\s*/.exec(getSourceCode(context).lines[tokens.lastProp.firstLine - 1])[0];
Expand All @@ -182,9 +184,9 @@ module.exports = {
}
if (indentation.length + 1 < newColumn) {
// Non-whitespace characters were included in the column offset
spaces = new Array(+correctColumn + 1 - indentation.length);
spaces = repeat(' ', +correctColumn - indentation.length);
}
return indentation + spaces.join(' ');
return indentation + spaces;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/jsx-closing-tag-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

'use strict';

const repeat = require('string.prototype.repeat');

const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const report = require('../util/report');
Expand Down Expand Up @@ -53,7 +55,7 @@ module.exports = {
node,
loc: node.loc,
fix(fixer) {
const indent = Array(opening.loc.start.column + 1).join(' ');
const indent = repeat(' ', opening.loc.start.column);
if (astUtil.isNodeFirstInLine(context, node)) {
return fixer.replaceTextRange(
[node.range[0] - node.loc.start.column, node.range[0]],
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

'use strict';

const repeat = require('string.prototype.repeat');

const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
const getText = require('../util/eslint').getText;
Expand Down Expand Up @@ -130,7 +132,8 @@ module.exports = {
data: msgContext,
fix(fixer) {
return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]],
Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
repeat(indentType === 'space' ? ' ' : '\t', needed)
);
},
});
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'use strict';

const matchAll = require('string.prototype.matchall');
const repeat = require('string.prototype.repeat');

const astUtil = require('../util/ast');
const docsUrl = require('../util/docsUrl');
Expand Down Expand Up @@ -109,7 +110,7 @@ module.exports = {
* @private
*/
function getFixerFunction(node, needed) {
const indent = Array(needed + 1).join(indentChar);
const indent = repeat(indentChar, needed);

if (node.type === 'JSXText' || node.type === 'Literal') {
return function fix(fixer) {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"prop-types": "^15.8.1",
"resolve": "^2.0.0-next.5",
"semver": "^6.3.1",
"string.prototype.matchall": "^4.0.11"
"string.prototype.matchall": "^4.0.11",
"string.prototype.repeat": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.24.7",
Expand Down Expand Up @@ -103,7 +104,8 @@
".eslintrc",
".editorconfig",
"tsconfig.json",
".markdownlint*"
".markdownlint*",
"types"
]
}
}
3 changes: 3 additions & 0 deletions types/string.prototype.repeat/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'string.prototype.repeat' {
export = typeof Function.call.bind(String.prototype.repeat);
}

0 comments on commit eda77f3

Please sign in to comment.