cursor-lint 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli.js +1 -1
- package/src/index.js +141 -2
package/package.json
CHANGED
package/src/cli.js
CHANGED
package/src/index.js
CHANGED
|
@@ -76,7 +76,7 @@ async function lintMdcFile(filePath) {
|
|
|
76
76
|
if (!fm.data.description) {
|
|
77
77
|
issues.push({ severity: 'warning', message: 'Missing description in frontmatter', hint: 'Add a description so Cursor knows when to apply this rule' });
|
|
78
78
|
}
|
|
79
|
-
if (fm.data.globs && typeof fm.data.globs === 'string' && fm.data.globs.includes(',')) {
|
|
79
|
+
if (fm.data.globs && typeof fm.data.globs === 'string' && fm.data.globs.includes(',') && !fm.data.globs.trim().startsWith('[')) {
|
|
80
80
|
issues.push({ severity: 'error', message: 'Globs should be YAML array, not comma-separated string', hint: 'Use globs:\\n - "*.ts"\\n - "*.tsx"' });
|
|
81
81
|
}
|
|
82
82
|
}
|
|
@@ -141,7 +141,146 @@ async function lintProject(dir) {
|
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
// Conflict detection across .mdc files
|
|
145
|
+
const conflicts = detectConflicts(dir);
|
|
146
|
+
if (conflicts.length > 0) {
|
|
147
|
+
results.push({
|
|
148
|
+
file: path.join(dir, '.cursor/rules/'),
|
|
149
|
+
issues: conflicts,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
144
153
|
return results;
|
|
145
154
|
}
|
|
146
155
|
|
|
147
|
-
|
|
156
|
+
function parseGlobs(globVal) {
|
|
157
|
+
if (!globVal) return [];
|
|
158
|
+
if (typeof globVal === 'string') {
|
|
159
|
+
// Handle both YAML array syntax and comma-separated
|
|
160
|
+
const trimmed = globVal.trim();
|
|
161
|
+
if (trimmed.startsWith('[')) {
|
|
162
|
+
// ["*.ts", "*.tsx"] format
|
|
163
|
+
return trimmed.slice(1, -1).split(',').map(g => g.trim().replace(/^["']|["']$/g, '')).filter(Boolean);
|
|
164
|
+
}
|
|
165
|
+
return trimmed.split(',').map(g => g.trim().replace(/^["']|["']$/g, '')).filter(Boolean);
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(globVal)) return globVal;
|
|
168
|
+
return [];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function globsOverlap(globsA, globsB) {
|
|
172
|
+
// If either has no globs (alwaysApply), they overlap with everything
|
|
173
|
+
if (globsA.length === 0 || globsB.length === 0) return true;
|
|
174
|
+
|
|
175
|
+
for (const a of globsA) {
|
|
176
|
+
for (const b of globsB) {
|
|
177
|
+
// Exact match
|
|
178
|
+
if (a === b) return true;
|
|
179
|
+
// Both are wildcards covering same extension
|
|
180
|
+
const extA = a.match(/^\*\.(\w+)$/);
|
|
181
|
+
const extB = b.match(/^\*\.(\w+)$/);
|
|
182
|
+
if (extA && extB && extA[1] === extB[1]) return true;
|
|
183
|
+
// One is a superset pattern like **/*.ts
|
|
184
|
+
if (a.includes('**') || b.includes('**')) {
|
|
185
|
+
const extA2 = a.match(/\*\.(\w+)$/);
|
|
186
|
+
const extB2 = b.match(/\*\.(\w+)$/);
|
|
187
|
+
if (extA2 && extB2 && extA2[1] === extB2[1]) return true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function extractDirectives(content) {
|
|
195
|
+
// Extract actionable instructions from rule body (after frontmatter)
|
|
196
|
+
const body = content.replace(/^---[\s\S]*?---\n?/, '').toLowerCase();
|
|
197
|
+
const directives = [];
|
|
198
|
+
|
|
199
|
+
// Look for contradictory patterns: "always use X" vs "never use X"
|
|
200
|
+
const alwaysMatch = body.match(/always\s+use\s+(\S+)/g) || [];
|
|
201
|
+
const neverMatch = body.match(/never\s+use\s+(\S+)/g) || [];
|
|
202
|
+
const preferMatch = body.match(/prefer\s+(\S+)/g) || [];
|
|
203
|
+
const avoidMatch = body.match(/avoid\s+(\S+)/g) || [];
|
|
204
|
+
const doNotMatch = body.match(/do\s+not\s+use\s+(\S+)/g) || [];
|
|
205
|
+
|
|
206
|
+
for (const m of alwaysMatch) directives.push({ type: 'require', subject: m.replace(/^always\s+use\s+/, '') });
|
|
207
|
+
for (const m of neverMatch) directives.push({ type: 'forbid', subject: m.replace(/^never\s+use\s+/, '') });
|
|
208
|
+
for (const m of preferMatch) directives.push({ type: 'prefer', subject: m.replace(/^prefer\s+/, '') });
|
|
209
|
+
for (const m of avoidMatch) directives.push({ type: 'avoid', subject: m.replace(/^avoid\s+/, '') });
|
|
210
|
+
for (const m of doNotMatch) directives.push({ type: 'forbid', subject: m.replace(/^do\s+not\s+use\s+/, '') });
|
|
211
|
+
|
|
212
|
+
return directives;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function detectConflicts(dir) {
|
|
216
|
+
const rulesDir = path.join(dir, '.cursor', 'rules');
|
|
217
|
+
if (!fs.existsSync(rulesDir) || !fs.statSync(rulesDir).isDirectory()) return [];
|
|
218
|
+
|
|
219
|
+
const files = fs.readdirSync(rulesDir).filter(f => f.endsWith('.mdc'));
|
|
220
|
+
if (files.length < 2) return [];
|
|
221
|
+
|
|
222
|
+
const parsed = [];
|
|
223
|
+
for (const file of files) {
|
|
224
|
+
const filePath = path.join(rulesDir, file);
|
|
225
|
+
const content = fs.readFileSync(filePath, 'utf-8');
|
|
226
|
+
const fm = parseFrontmatter(content);
|
|
227
|
+
const globs = fm.data ? parseGlobs(fm.data.globs) : [];
|
|
228
|
+
const alwaysApply = fm.data && fm.data.alwaysApply;
|
|
229
|
+
const directives = extractDirectives(content);
|
|
230
|
+
parsed.push({ file, filePath, globs, alwaysApply, directives, content });
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const issues = [];
|
|
234
|
+
|
|
235
|
+
// Check for duplicate alwaysApply rules with overlapping globs
|
|
236
|
+
for (let i = 0; i < parsed.length; i++) {
|
|
237
|
+
for (let j = i + 1; j < parsed.length; j++) {
|
|
238
|
+
const a = parsed[i];
|
|
239
|
+
const b = parsed[j];
|
|
240
|
+
|
|
241
|
+
// Check glob overlap
|
|
242
|
+
const aGlobs = a.alwaysApply && a.globs.length === 0 ? [] : a.globs;
|
|
243
|
+
const bGlobs = b.alwaysApply && b.globs.length === 0 ? [] : b.globs;
|
|
244
|
+
const overlap = globsOverlap(aGlobs, bGlobs);
|
|
245
|
+
|
|
246
|
+
if (!overlap) continue;
|
|
247
|
+
|
|
248
|
+
// Check for contradictory directives
|
|
249
|
+
for (const dA of a.directives) {
|
|
250
|
+
for (const dB of b.directives) {
|
|
251
|
+
if (dA.subject !== dB.subject) continue;
|
|
252
|
+
|
|
253
|
+
const contradicts =
|
|
254
|
+
(dA.type === 'require' && (dB.type === 'forbid' || dB.type === 'avoid')) ||
|
|
255
|
+
(dA.type === 'forbid' && (dB.type === 'require' || dB.type === 'prefer')) ||
|
|
256
|
+
(dA.type === 'prefer' && dB.type === 'forbid') ||
|
|
257
|
+
(dA.type === 'avoid' && dB.type === 'require');
|
|
258
|
+
|
|
259
|
+
if (contradicts) {
|
|
260
|
+
issues.push({
|
|
261
|
+
severity: 'error',
|
|
262
|
+
message: `Conflicting rules: ${a.file} says "${dA.type} ${dA.subject}" but ${b.file} says "${dB.type} ${dB.subject}"`,
|
|
263
|
+
hint: 'Conflicting directives confuse the model. Remove or reconcile one of these rules.',
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// Check for duplicate glob coverage (both alwaysApply targeting same files)
|
|
270
|
+
if (a.alwaysApply && b.alwaysApply && a.globs.length > 0 && b.globs.length > 0) {
|
|
271
|
+
const sharedGlobs = a.globs.filter(g => b.globs.includes(g));
|
|
272
|
+
if (sharedGlobs.length > 0) {
|
|
273
|
+
issues.push({
|
|
274
|
+
severity: 'warning',
|
|
275
|
+
message: `Overlapping globs: ${a.file} and ${b.file} both target ${sharedGlobs.join(', ')}`,
|
|
276
|
+
hint: 'Multiple rules targeting the same files may cause unpredictable behavior. Consider merging them.',
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return issues;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
module.exports = { lintProject, lintMdcFile, lintCursorrules, detectConflicts };
|