Skip to content

Commit

Permalink
Add plugin stats to count number of times eslint-disable is used. (#1…
Browse files Browse the repository at this point in the history
…21818)

* add eslint disable count stats

* address code review comments
  • Loading branch information
stacey-gammon authored Dec 22, 2021
1 parent 0a75d42 commit 4305732
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
29 changes: 25 additions & 4 deletions packages/kbn-docs-utils/src/api_docs/build_api_docs_cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { writeDeprecationDocByApi } from './mdx/write_deprecations_doc_by_api';
import { writeDeprecationDocByPlugin } from './mdx/write_deprecations_doc_by_plugin';
import { writePluginDirectoryDoc } from './mdx/write_plugin_directory_doc';
import { collectApiStatsForPlugin } from './stats';
import { countEslintDisableLine, EslintDisableCounts } from './count_eslint_disable';

function isStringArray(arr: unknown | string[]): arr is string[] {
return Array.isArray(arr) && arr.every((p) => typeof p === 'string');
Expand Down Expand Up @@ -78,17 +79,19 @@ export function runBuildApiDocsCli() {

const reporter = CiStatsReporter.fromEnv(log);

const allPluginStats = plugins.reduce((acc, plugin) => {
const allPluginStats: { [key: string]: PluginMetaInfo & ApiStats & EslintDisableCounts } = {};
for (const plugin of plugins) {
const id = plugin.manifest.id;
const pluginApi = pluginApiMap[id];
acc[id] = {

allPluginStats[id] = {
...(await countEslintDisableLine(plugin.directory)),
...collectApiStatsForPlugin(pluginApi, missingApiItems, referencedDeprecations),
owner: plugin.manifest.owner,
description: plugin.manifest.description,
isPlugin: plugin.isPlugin,
};
return acc;
}, {} as { [key: string]: PluginMetaInfo & ApiStats });
}

writePluginDirectoryDoc(outputFolder, pluginApiMap, allPluginStats, log);

Expand Down Expand Up @@ -142,6 +145,24 @@ export function runBuildApiDocsCli() {
group: 'References to deprecated APIs',
value: pluginStats.deprecatedAPIsReferencedCount,
},
{
id,
meta: { pluginTeam },
group: 'ESLint disabled line counts',
value: pluginStats.eslintDisableLineCount,
},
{
id,
meta: { pluginTeam },
group: 'ESLint disabled in files',
value: pluginStats.eslintDisableFileCount,
},
{
id,
meta: { pluginTeam },
group: 'Total ESLint disabled count',
value: pluginStats.eslintDisableFileCount + pluginStats.eslintDisableLineCount,
},
]);

const getLink = (d: ApiDeclaration) =>
Expand Down
25 changes: 25 additions & 0 deletions packages/kbn-docs-utils/src/api_docs/count_eslint_disable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { countEslintDisableLine } from './count_eslint_disable';

/* eslint-disable no-console */

it('countEsLintDisableLine', async () => {
console.log('This is a test');

// eslint-disable-next-line prefer-const
let test: string = '';

const counts = await countEslintDisableLine(__filename);
expect(counts.eslintDisableLineCount).toBe(1);
expect(counts.eslintDisableFileCount).toBe(1);

// To avoid unused warning.
return test;
});
34 changes: 34 additions & 0 deletions packages/kbn-docs-utils/src/api_docs/count_eslint_disable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { execSync } from 'child_process';

export interface EslintDisableCounts {
eslintDisableLineCount: number;
eslintDisableFileCount: number;
}

export async function countEslintDisableLine(path: string): Promise<EslintDisableCounts> {
const disableCountOutputs = await Promise.all([
execSync(`grep -rE 'eslint-disable-next-line|eslint-disable-line' ${path} | wc -l`),
execSync(`grep -rE 'eslint-disable ' ${path} | wc -l`),
]);
const eslintDisableLineCount = Number.parseInt(disableCountOutputs[0].toString(), 10);

if (eslintDisableLineCount === undefined || isNaN(eslintDisableLineCount)) {
throw new Error(`Parsing ${disableCountOutputs[0]} failed to product a valid number`);
}

const eslintDisableFileCount = Number.parseInt(disableCountOutputs[1].toString(), 10);

if (eslintDisableFileCount === undefined || isNaN(eslintDisableFileCount)) {
throw new Error(`Parsing ${disableCountOutputs[1]} failed to product a valid number`);
}

return { eslintDisableFileCount, eslintDisableLineCount };
}

0 comments on commit 4305732

Please sign in to comment.