Skip to content

Commit

Permalink
feat(eslint-plugin): [object-curly-spacing] add MappedType support
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasKunnen committed Mar 21, 2021
1 parent 08a5448 commit 2fb235f
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions packages/eslint-plugin/src/rules/object-curly-spacing.ts
Expand Up @@ -63,7 +63,7 @@ export default createRule<Options, MessageIds>({
* @param token The token to use for the report.
*/
function reportNoBeginningSpace(
node: TSESTree.TSTypeLiteral,
node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral,
token: TSESTree.Token,
): void {
const nextToken = context
Expand All @@ -89,7 +89,7 @@ export default createRule<Options, MessageIds>({
* @param token The token to use for the report.
*/
function reportNoEndingSpace(
node: TSESTree.TSTypeLiteral,
node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral,
token: TSESTree.Token,
): void {
const previousToken = context
Expand All @@ -115,7 +115,7 @@ export default createRule<Options, MessageIds>({
* @param token The token to use for the report.
*/
function reportRequiredBeginningSpace(
node: TSESTree.TSTypeLiteral,
node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral,
token: TSESTree.Token,
): void {
context.report({
Expand All @@ -137,7 +137,7 @@ export default createRule<Options, MessageIds>({
* @param token The token to use for the report.
*/
function reportRequiredEndingSpace(
node: TSESTree.TSTypeLiteral,
node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral,
token: TSESTree.Token,
): void {
context.report({
Expand All @@ -162,7 +162,7 @@ export default createRule<Options, MessageIds>({
* @param last The last token to check (should be closing brace)
*/
function validateBraceSpacing(
node: TSESTree.TSTypeLiteral,
node: TSESTree.TSMappedType | TSESTree.TSTypeLiteral,
first: TSESTree.Token,
second: TSESTree.Token | TSESTree.Comment,
penultimate: TSESTree.Token | TSESTree.Comment,
Expand All @@ -175,7 +175,10 @@ export default createRule<Options, MessageIds>({

const openingCurlyBraceMustBeSpaced =
options.arraysInObjectsException &&
secondType === AST_NODE_TYPES.TSIndexSignature
[
AST_NODE_TYPES.TSMappedType,
AST_NODE_TYPES.TSIndexSignature,
].includes(secondType)
? !options.spaced
: options.spaced;

Expand All @@ -197,15 +200,19 @@ export default createRule<Options, MessageIds>({
isClosingBracketToken(penultimate)) ||
(options.objectsInObjectsException &&
isClosingBraceToken(penultimate));
const penultimateType =
shouldCheckPenultimate &&
sourceCode.getNodeByRangeIndex(penultimate.range[0])!.type;
const penultimateType = shouldCheckPenultimate
? sourceCode.getNodeByRangeIndex(penultimate.range[0])!.type
: undefined;

const closingCurlyBraceMustBeSpaced =
(options.arraysInObjectsException &&
penultimateType === AST_NODE_TYPES.TSTupleType) ||
(options.objectsInObjectsException &&
penultimateType === AST_NODE_TYPES.TSTypeLiteral)
penultimateType !== undefined &&
[
AST_NODE_TYPES.TSMappedType,
AST_NODE_TYPES.TSTypeLiteral,
].includes(penultimateType))
? !options.spaced
: options.spaced;

Expand Down Expand Up @@ -246,6 +253,18 @@ export default createRule<Options, MessageIds>({
const rules = baseRule.create(context);
return {
...rules,
TSMappedType(node: TSESTree.TSMappedType): void {
const first = sourceCode.getFirstToken(node)!;
const last = sourceCode.getLastToken(node)!;
const second = sourceCode.getTokenAfter(first, {
includeComments: true,
})!;
const penultimate = sourceCode.getTokenBefore(last, {
includeComments: true,
})!;

validateBraceSpacing(node, first, second, penultimate, last);
},
TSTypeLiteral(node: TSESTree.TSTypeLiteral): void {
if (node.members.length === 0) {
return;
Expand Down

0 comments on commit 2fb235f

Please sign in to comment.