add explicit button type regression test
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readdir, readFile } from 'node:fs/promises';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const root = new URL('../src/', import.meta.url);
|
||||
|
||||
async function jsxFiles(directory) {
|
||||
const entries = await readdir(directory, { withFileTypes: true });
|
||||
const files = [];
|
||||
for (const entry of entries) {
|
||||
const path = join(directory, entry.name);
|
||||
if (entry.isDirectory()) files.push(...await jsxFiles(path));
|
||||
else if (entry.isFile() && entry.name.endsWith('.jsx')) files.push(path);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
test('every JSX button declares an explicit button or submit type', async () => {
|
||||
const files = await jsxFiles(root.pathname);
|
||||
const violations = [];
|
||||
for (const file of files) {
|
||||
const source = await readFile(file, 'utf8');
|
||||
for (const match of source.matchAll(/<button\b[^>]*>/gs)) {
|
||||
const tag = match[0];
|
||||
const line = source.slice(0, match.index).split('\n').length;
|
||||
const type = /\btype\s*=\s*["'](button|submit)["']/.exec(tag)?.[1];
|
||||
if (!type) violations.push(`${file}:${line}`);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(violations, [], `type eksik düğmeler: ${violations.join(', ')}`);
|
||||
});
|
||||
Reference in New Issue
Block a user