Skip to content

Commit 07b07f0

Browse files
fix: allow linterOptions in blockESLintIntake (#2169)
## PR Checklist - [x] Addresses an existing open issue: fixes #2168 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Previously the logic was to check if the first object expression after `ignores` has the five expected properties. Now it searches for the first matching object expression. 🎁
1 parent e3f5639 commit 07b07f0

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

src/blocks/blockESLintIntake.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,49 @@ describe(blockESLintIntake, () => {
9393
});
9494

9595
it.each([
96+
[
97+
"rules group after linterOptions and member expression extends",
98+
`
99+
export default tseslint.config(
100+
{ ignores: ["lib", "node_modules", "pnpm-lock.yaml"] },
101+
{ linterOptions: { reportUnusedDisableDirectives: "error" } },
102+
eslint.configs.recommended,
103+
comments.recommended,
104+
{
105+
extends: [tseslint.configs.strictTypeChecked],
106+
files: ["**/*.{js,ts}"],
107+
languageOptions: { /* ... */ },
108+
rules: {
109+
"@typescript-eslint/prefer-nullish-coalescing": [
110+
"error",
111+
{ ignorePrimitives: true },
112+
],
113+
"@typescript-eslint/restrict-template-expressions": [
114+
"error",
115+
{ allowBoolean: true },
116+
],
117+
},
118+
settings: { /* ... */ }
119+
}
120+
);`,
121+
{
122+
ignores: ["lib", "node_modules", "pnpm-lock.yaml"],
123+
rules: [
124+
{
125+
entries: {
126+
"@typescript-eslint/prefer-nullish-coalescing": [
127+
"error",
128+
{ ignorePrimitives: true },
129+
],
130+
"@typescript-eslint/restrict-template-expressions": [
131+
"error",
132+
{ allowBoolean: true },
133+
],
134+
},
135+
},
136+
],
137+
},
138+
],
96139
[
97140
"non-commented group in rules",
98141
`

src/blocks/eslint/blockESLintIntake.ts

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,22 @@ export function blockESLintIntake(sourceText: string) {
139139

140140
function getConfigRulesObject(nodes: TSESTree.Node[]) {
141141
const configObject = nodes.find(
142-
(node) => node.type === AST_NODE_TYPES.ObjectExpression,
142+
(node): node is TSESTree.ObjectExpression =>
143+
node.type === AST_NODE_TYPES.ObjectExpression &&
144+
areArraysEqual(
145+
node.properties.map((property) =>
146+
property.type === AST_NODE_TYPES.Property &&
147+
property.key.type === AST_NODE_TYPES.Identifier
148+
? property.key.name
149+
: undefined,
150+
),
151+
["extends", "files", "languageOptions", "rules", "settings"],
152+
),
143153
);
144154
if (!configObject) {
145155
return undefined;
146156
}
147157

148-
const configPropertyKeys = configObject.properties.map((property) =>
149-
property.type === AST_NODE_TYPES.Property &&
150-
property.key.type === AST_NODE_TYPES.Identifier
151-
? property.key.name
152-
: undefined,
153-
);
154-
if (
155-
!areArraysEqual(configPropertyKeys, [
156-
"extends",
157-
"files",
158-
"languageOptions",
159-
"rules",
160-
"settings",
161-
])
162-
) {
163-
return undefined;
164-
}
165-
166158
const rulesObject = configObject.properties[3];
167159
return (
168160
rulesObject.type === AST_NODE_TYPES.Property &&

0 commit comments

Comments
 (0)