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: wrong report for object option in jsonc/object-curly-spacing #283

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tidy-flowers-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": patch
---

fix: wrong report for object option in `jsonc/object-curly-spacing`
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ module.exports = {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-shadow": "off",
complexity: "off",
"one-var": "off",
"no-invalid-this": "off",
// Repo rule
Expand Down
82 changes: 36 additions & 46 deletions lib/rules/array-bracket-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ export default createRule("array-bracket-spacing", {
singleElementException: isOptionSet("singleValue"),
objectsInArraysException: isOptionSet("objectsInArrays"),
arraysInArraysException: isOptionSet("arraysInArrays"),
isOpeningBracketMustBeSpaced(node: AST.JSONArrayExpression) {
if (options.singleElementException && node.elements.length === 1) {
return !options.spaced;
}
const firstElement = node.elements[0];
return firstElement &&
((options.objectsInArraysException && isObjectType(firstElement)) ||
(options.arraysInArraysException && isArrayType(firstElement)))
? !options.spaced
: options.spaced;
},
isClosingBracketMustBeSpaced(node: AST.JSONArrayExpression) {
if (options.singleElementException && node.elements.length === 1) {
return !options.spaced;
}
const lastElement = node.elements[node.elements.length - 1];
return lastElement &&
((options.objectsInArraysException && isObjectType(lastElement)) ||
(options.arraysInArraysException && isArrayType(lastElement)))
? !options.spaced
: options.spaced;
},
};

/**
Expand Down Expand Up @@ -189,57 +211,25 @@ export default createRule("array-bracket-spacing", {
const second = sourceCode.getFirstToken(node as any, 1)!;
const last = sourceCode.getLastToken(node as any)!;
const penultimate = sourceCode.getTokenBefore(last)!;
const firstElement = node.elements[0];
const lastElement = node.elements[node.elements.length - 1];

const openingBracketMustBeSpaced =
(firstElement &&
options.objectsInArraysException &&
isObjectType(firstElement)) ||
(firstElement &&
options.arraysInArraysException &&
isArrayType(firstElement)) ||
(options.singleElementException && node.elements.length === 1)
? !options.spaced
: options.spaced;

const closingBracketMustBeSpaced =
(lastElement &&
options.objectsInArraysException &&
isObjectType(lastElement)) ||
(lastElement &&
options.arraysInArraysException &&
isArrayType(lastElement)) ||
(options.singleElementException && node.elements.length === 1)
? !options.spaced
: options.spaced;

if (isTokenOnSameLine(first, second)) {
if (
openingBracketMustBeSpaced &&
!sourceCode.isSpaceBetweenTokens(first, second)
)
reportRequiredBeginningSpace(node, first);

if (
!openingBracketMustBeSpaced &&
sourceCode.isSpaceBetweenTokens(first, second)
)
reportNoBeginningSpace(node, first);
if (options.isOpeningBracketMustBeSpaced(node)) {
if (!sourceCode.isSpaceBetween(first, second))
reportRequiredBeginningSpace(node, first);
} else {
if (sourceCode.isSpaceBetween(first, second))
reportNoBeginningSpace(node, first);
}
}

if (first !== penultimate && isTokenOnSameLine(penultimate, last)) {
if (
closingBracketMustBeSpaced &&
!sourceCode.isSpaceBetweenTokens(penultimate, last)
)
reportRequiredEndingSpace(node, last);

if (
!closingBracketMustBeSpaced &&
sourceCode.isSpaceBetweenTokens(penultimate, last)
)
reportNoEndingSpace(node, last);
if (options.isClosingBracketMustBeSpaced(node)) {
if (!sourceCode.isSpaceBetween(penultimate, last))
reportRequiredEndingSpace(node, last);
} else {
if (sourceCode.isSpaceBetween(penultimate, last))
reportNoEndingSpace(node, last);
}
}
}

Expand Down
69 changes: 33 additions & 36 deletions lib/rules/object-curly-spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ export default createRule("object-curly-spacing", {
spaced,
arraysInObjectsException: isOptionSet("arraysInObjects"),
objectsInObjectsException: isOptionSet("objectsInObjects"),
isOpeningCurlyBraceMustBeSpaced(_second: Token | Comment) {
return options.spaced;
},
isClosingCurlyBraceMustBeSpaced(penultimate: Token | Comment) {
const targetPenultimateType =
options.arraysInObjectsException && isClosingBracketToken(penultimate)
? "JSONArrayExpression"
: options.objectsInObjectsException &&
isClosingBraceToken(penultimate)
? "JSONObjectExpression"
: null;
const node = sourceCode.getNodeByRangeIndex(
penultimate.range![0],
) as AST.JSONNode | null;

return targetPenultimateType && node?.type === targetPenultimateType
? !options.spaced
: options.spaced;
},
};

/**
Expand Down Expand Up @@ -185,47 +204,25 @@ export default createRule("object-curly-spacing", {
last: Token,
) {
if (isTokenOnSameLine(first, second)) {
const firstSpaced = sourceCode.isSpaceBetweenTokens(
first,
second as Token,
);

if (options.spaced && !firstSpaced)
reportRequiredBeginningSpace(node, first);
const firstSpaced = sourceCode.isSpaceBetween(first, second as any);

if (!options.spaced && firstSpaced && second.type !== "Line")
reportNoBeginningSpace(node, first);
if (options.isOpeningCurlyBraceMustBeSpaced(second)) {
if (!firstSpaced) reportRequiredBeginningSpace(node, first);
} else {
if (firstSpaced && second.type !== "Line") {
reportNoBeginningSpace(node, first);
}
}
}

if (isTokenOnSameLine(penultimate, last)) {
const shouldCheckPenultimate =
(options.arraysInObjectsException &&
isClosingBracketToken(penultimate)) ||
(options.objectsInObjectsException &&
isClosingBraceToken(penultimate));
const penultimateType =
shouldCheckPenultimate &&
sourceCode.getNodeByRangeIndex(penultimate.range[0])!.type;

const closingCurlyBraceMustBeSpaced =
(options.arraysInObjectsException &&
penultimateType === "ArrayExpression") ||
(options.objectsInObjectsException &&
(penultimateType === "ObjectExpression" ||
penultimateType === "ObjectPattern"))
? !options.spaced
: options.spaced;

const lastSpaced = sourceCode.isSpaceBetweenTokens(
penultimate as Token,
last,
);

if (closingCurlyBraceMustBeSpaced && !lastSpaced)
reportRequiredEndingSpace(node, last);
const lastSpaced = sourceCode.isSpaceBetween(penultimate as any, last);

if (!closingCurlyBraceMustBeSpaced && lastSpaced)
reportNoEndingSpace(node, last);
if (options.isClosingCurlyBraceMustBeSpaced(penultimate)) {
if (!lastSpaced) reportRequiredEndingSpace(node, last);
} else {
if (lastSpaced) reportNoEndingSpace(node, last);
}
}
}

Expand Down
Loading
Loading