Skip to content

Commit

Permalink
fix(eslint-plugin): do not use .at(), Node 14 does not support it (#6402
Browse files Browse the repository at this point in the history
)

* fix(key-spacing): do not use .at(), Node 14 does not support it

* 🚨 Coverage on at()

* Apply suggestions from code review

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
coyotte508 and JoshuaKGoldberg committed Jan 31, 2023
1 parent fbe811c commit 077ed1b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/eslint-plugin/src/rules/key-spacing.ts
Expand Up @@ -14,6 +14,16 @@ const baseSchema = Array.isArray(baseRule.meta.schema)
? baseRule.meta.schema[0]
: baseRule.meta.schema;

/**
* TODO: replace with native .at() once Node 14 stops being supported
*/
function at<T>(arr: T[], position: number): T | undefined {
if (position < 0) {
return arr[arr.length + position];
}
return arr[position];
}

export default util.createRule<Options, MessageIds>({
name: 'key-spacing',
meta: {
Expand Down Expand Up @@ -41,7 +51,7 @@ export default util.createRule<Options, MessageIds>({
function adjustedColumn(position: TSESTree.Position): number {
const line = position.line - 1; // position.line is 1-indexed
return util.getStringLength(
sourceCode.lines[line].slice(0, position.column),
at(sourceCode.lines, line)!.slice(0, position.column),
);
}

Expand Down Expand Up @@ -87,7 +97,7 @@ export default util.createRule<Options, MessageIds>({
return code.slice(
0,
sourceCode.getTokenAfter(
node.parameters.at(-1)!,
at(node.parameters, -1)!,
util.isClosingBracketToken,
)!.range[1] - node.range[0],
);
Expand All @@ -102,7 +112,7 @@ export default util.createRule<Options, MessageIds>({
return getLastTokenBeforeColon(
node.type !== AST_NODE_TYPES.TSIndexSignature
? node.key
: node.parameters.at(-1)!,
: at(node.parameters, -1)!,
).loc.end;
}

Expand Down Expand Up @@ -202,7 +212,7 @@ export default util.createRule<Options, MessageIds>({
if (
leadingComments.length &&
leadingComments[0].loc.start.line - groupEndLine <= 1 &&
candidateValueStartLine - leadingComments.at(-1)!.loc.end.line <= 1
candidateValueStartLine - at(leadingComments, -1)!.loc.end.line <= 1
) {
for (let i = 1; i < leadingComments.length; i++) {
if (
Expand Down Expand Up @@ -373,7 +383,7 @@ export default util.createRule<Options, MessageIds>({
let prevNode: TSESTree.Node | undefined = undefined;

for (const node of members) {
let prevAlignedNode = currentAlignGroup.at(-1);
let prevAlignedNode = at(currentAlignGroup, -1);
if (prevAlignedNode !== prevNode) {
prevAlignedNode = undefined;
}
Expand Down

0 comments on commit 077ed1b

Please sign in to comment.