diff --git a/lib/rules/jsx-closing-bracket-location.js b/lib/rules/jsx-closing-bracket-location.js index 26556d6117..18e0a59feb 100644 --- a/lib/rules/jsx-closing-bracket-location.js +++ b/lib/rules/jsx-closing-bracket-location.js @@ -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'); @@ -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]; @@ -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 + 1 - indentation.length); } - return indentation + spaces.join(' '); + return indentation + spaces; } /** diff --git a/lib/rules/jsx-closing-tag-location.js b/lib/rules/jsx-closing-tag-location.js index 5d5a95cb7b..70c78a6987 100644 --- a/lib/rules/jsx-closing-tag-location.js +++ b/lib/rules/jsx-closing-tag-location.js @@ -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'); @@ -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 + 1); if (astUtil.isNodeFirstInLine(context, node)) { return fixer.replaceTextRange( [node.range[0] - node.loc.start.column, node.range[0]], diff --git a/lib/rules/jsx-indent-props.js b/lib/rules/jsx-indent-props.js index 050e721df3..7347a5e17c 100644 --- a/lib/rules/jsx-indent-props.js +++ b/lib/rules/jsx-indent-props.js @@ -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; @@ -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 + 1) + ); }, }); } diff --git a/lib/rules/jsx-indent.js b/lib/rules/jsx-indent.js index 27397172c8..ee012f343e 100644 --- a/lib/rules/jsx-indent.js +++ b/lib/rules/jsx-indent.js @@ -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'); @@ -109,7 +110,7 @@ module.exports = { * @private */ function getFixerFunction(node, needed) { - const indent = Array(needed + 1).join(indentChar); + const indent = repeat(indentChar, needed + 1); if (node.type === 'JSXText' || node.type === 'Literal') { return function fix(fixer) { diff --git a/package.json b/package.json index 6446e53be2..570331196c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -103,7 +104,8 @@ ".eslintrc", ".editorconfig", "tsconfig.json", - ".markdownlint*" + ".markdownlint*", + "types" ] } } diff --git a/types/string.prototype.repeat/index.d.ts b/types/string.prototype.repeat/index.d.ts new file mode 100644 index 0000000000..f240d9301f --- /dev/null +++ b/types/string.prototype.repeat/index.d.ts @@ -0,0 +1,3 @@ +declare module 'string.prototype.repeat' { + export = typeof Function.call.bind(String.prototype.repeat); +}