Skip to content

Commit eda77f3

Browse files
committed
[Refactor] avoid making a holey array
1 parent 3c1d520 commit eda77f3

File tree

6 files changed

+21
-8
lines changed

6 files changed

+21
-8
lines changed

lib/rules/jsx-closing-bracket-location.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
'use strict';
77

88
const has = require('object.hasown/polyfill')();
9+
const repeat = require('string.prototype.repeat');
10+
911
const docsUrl = require('../util/docsUrl');
1012
const getSourceCode = require('../util/eslint').getSourceCode;
1113
const report = require('../util/report');
@@ -168,7 +170,7 @@ module.exports = {
168170
function getIndentation(tokens, expectedLocation, correctColumn) {
169171
const newColumn = correctColumn || 0;
170172
let indentation;
171-
let spaces = [];
173+
let spaces = '';
172174
switch (expectedLocation) {
173175
case 'props-aligned':
174176
indentation = /^\s*/.exec(getSourceCode(context).lines[tokens.lastProp.firstLine - 1])[0];
@@ -182,9 +184,9 @@ module.exports = {
182184
}
183185
if (indentation.length + 1 < newColumn) {
184186
// Non-whitespace characters were included in the column offset
185-
spaces = new Array(+correctColumn + 1 - indentation.length);
187+
spaces = repeat(' ', +correctColumn - indentation.length);
186188
}
187-
return indentation + spaces.join(' ');
189+
return indentation + spaces;
188190
}
189191

190192
/**

lib/rules/jsx-closing-tag-location.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
'use strict';
77

8+
const repeat = require('string.prototype.repeat');
9+
810
const astUtil = require('../util/ast');
911
const docsUrl = require('../util/docsUrl');
1012
const report = require('../util/report');
@@ -53,7 +55,7 @@ module.exports = {
5355
node,
5456
loc: node.loc,
5557
fix(fixer) {
56-
const indent = Array(opening.loc.start.column + 1).join(' ');
58+
const indent = repeat(' ', opening.loc.start.column);
5759
if (astUtil.isNodeFirstInLine(context, node)) {
5860
return fixer.replaceTextRange(
5961
[node.range[0] - node.loc.start.column, node.range[0]],

lib/rules/jsx-indent-props.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
'use strict';
3232

33+
const repeat = require('string.prototype.repeat');
34+
3335
const astUtil = require('../util/ast');
3436
const docsUrl = require('../util/docsUrl');
3537
const getText = require('../util/eslint').getText;
@@ -130,7 +132,8 @@ module.exports = {
130132
data: msgContext,
131133
fix(fixer) {
132134
return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]],
133-
Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
135+
repeat(indentType === 'space' ? ' ' : '\t', needed)
136+
);
134137
},
135138
});
136139
}

lib/rules/jsx-indent.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
'use strict';
3232

3333
const matchAll = require('string.prototype.matchall');
34+
const repeat = require('string.prototype.repeat');
3435

3536
const astUtil = require('../util/ast');
3637
const docsUrl = require('../util/docsUrl');
@@ -109,7 +110,7 @@ module.exports = {
109110
* @private
110111
*/
111112
function getFixerFunction(node, needed) {
112-
const indent = Array(needed + 1).join(indentChar);
113+
const indent = repeat(indentChar, needed);
113114

114115
if (node.type === 'JSXText' || node.type === 'Literal') {
115116
return function fix(fixer) {

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"prop-types": "^15.8.1",
4343
"resolve": "^2.0.0-next.5",
4444
"semver": "^6.3.1",
45-
"string.prototype.matchall": "^4.0.11"
45+
"string.prototype.matchall": "^4.0.11",
46+
"string.prototype.repeat": "^1.0.0"
4647
},
4748
"devDependencies": {
4849
"@babel/core": "^7.24.7",
@@ -103,7 +104,8 @@
103104
".eslintrc",
104105
".editorconfig",
105106
"tsconfig.json",
106-
".markdownlint*"
107+
".markdownlint*",
108+
"types"
107109
]
108110
}
109111
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'string.prototype.repeat' {
2+
export = typeof Function.call.bind(String.prototype.repeat);
3+
}

0 commit comments

Comments
 (0)