33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
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(', ')}`);
|
|
});
|