Betterer: Add our own regexp matcher (#79448)

* Add betterer rule for gf-form

* Betterer: Write generic regexp matcher

* Add space

* Update lockfile

* Update comments

* Add comment with notice
This commit is contained in:
Tobias Skarhed
2023-12-15 11:12:56 +01:00
committed by GitHub
parent ce1450d4d3
commit 8d529bc4cb
4 changed files with 1259 additions and 13 deletions

View File

@@ -12,12 +12,18 @@ const eslintPathsToIgnore = [
'public/app/plugins/panel/graph', // will be removed alongside angular
];
// Avoid using functions that report the position of the issues, as this causes a lot of merge conflicts
export default {
'better eslint': () =>
countEslintErrors()
.include('**/*.{ts,tsx}')
.exclude(new RegExp(eslintPathsToIgnore.join('|'))),
'no undocumented stories': () => countUndocumentedStories().include('**/!(*.internal).story.tsx'),
'no gf-form usage': () =>
regexp(
/gf-form/gm,
'gf-form usage has been deprecated. Use a component from @grafana/ui or custom CSS instead.'
).include('**/*.{ts,tsx,html}'),
};
function countUndocumentedStories() {
@@ -38,6 +44,28 @@ function countUndocumentedStories() {
});
}
/**
* Generic regexp pattern matcher, similar to @betterer/regexp.
* The only difference is that the positions of the errors are not reported, as this may cause a lot of merge conflicts.
*/
function regexp(pattern: RegExp, issueMessage: string) {
return new BettererFileTest(async (filePaths, fileTestResult) => {
await Promise.all(
filePaths.map(async (filePath) => {
const fileText = await fs.readFile(filePath, 'utf8');
const matches = fileText.match(pattern);
if (matches) {
// File contents doesn't matter, since we're not reporting the position
const file = fileTestResult.addFile(filePath, '');
matches.forEach(() => {
file.addIssue(0, 0, issueMessage);
});
}
})
);
});
}
function countEslintErrors() {
return new BettererFileTest(async (filePaths, fileTestResult, resolver) => {
const { baseDirectory } = resolver;