From 89170cfc4d5d9fd1859776a129da0b9fdf65fcd0 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Tue, 31 Jan 2023 16:43:03 +0100 Subject: [PATCH 1/3] fix(key-spacing): do not use .at(), Node 14 does not support it --- .../eslint-plugin/src/rules/key-spacing.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index 587d2674f4f..9f89d3bd92a 100644 --- a/packages/eslint-plugin/src/rules/key-spacing.ts +++ b/packages/eslint-plugin/src/rules/key-spacing.ts @@ -14,6 +14,16 @@ const baseSchema = Array.isArray(baseRule.meta.schema) ? baseRule.meta.schema[0] : baseRule.meta.schema; +/** + * To replace with native .at() once Node 14 stops being supported + */ +function at(arr: T[], position: number): T | undefined { + if (position < 0) { + return arr[arr.length + position]; + } + return arr[position]; +} + export default util.createRule({ name: 'key-spacing', meta: { @@ -87,7 +97,7 @@ export default util.createRule({ return code.slice( 0, sourceCode.getTokenAfter( - node.parameters.at(-1)!, + at(node.parameters, -1)!, util.isClosingBracketToken, )!.range[1] - node.range[0], ); @@ -102,7 +112,7 @@ export default util.createRule({ return getLastTokenBeforeColon( node.type !== AST_NODE_TYPES.TSIndexSignature ? node.key - : node.parameters.at(-1)!, + : at(node.parameters, -1)!, ).loc.end; } @@ -202,7 +212,7 @@ export default util.createRule({ 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 ( @@ -373,7 +383,7 @@ export default util.createRule({ 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; } From 9b9129346791e1c3dc2b6a7e5a88f3fc3b250c34 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Tue, 31 Jan 2023 17:02:16 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9A=A8=20Coverage=20on=20at()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/eslint-plugin/src/rules/key-spacing.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index 9f89d3bd92a..b2546a784fb 100644 --- a/packages/eslint-plugin/src/rules/key-spacing.ts +++ b/packages/eslint-plugin/src/rules/key-spacing.ts @@ -51,7 +51,8 @@ export default util.createRule({ 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), + // use at() just for codecov, so it has a positive position case + at(sourceCode.lines, line)!.slice(0, position.column), ); } From f222453e2cab39957ced0500475adfe3c1c56b9a Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 31 Jan 2023 11:07:12 -0500 Subject: [PATCH 3/3] Apply suggestions from code review --- packages/eslint-plugin/src/rules/key-spacing.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/key-spacing.ts b/packages/eslint-plugin/src/rules/key-spacing.ts index b2546a784fb..2562107ee05 100644 --- a/packages/eslint-plugin/src/rules/key-spacing.ts +++ b/packages/eslint-plugin/src/rules/key-spacing.ts @@ -15,7 +15,7 @@ const baseSchema = Array.isArray(baseRule.meta.schema) : baseRule.meta.schema; /** - * To replace with native .at() once Node 14 stops being supported + * TODO: replace with native .at() once Node 14 stops being supported */ function at(arr: T[], position: number): T | undefined { if (position < 0) { @@ -51,7 +51,6 @@ export default util.createRule({ function adjustedColumn(position: TSESTree.Position): number { const line = position.line - 1; // position.line is 1-indexed return util.getStringLength( - // use at() just for codecov, so it has a positive position case at(sourceCode.lines, line)!.slice(0, position.column), ); }