Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): do not use .at(), Node 14 does not support it #6402

Merged
merged 3 commits into from Jan 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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