Unverified Commit fbb85369 authored by Keen Yee Liau's avatar Keen Yee Liau Committed by GitHub

collect telemetry for ESLint rules (#34564)

This commit adds functionality to track usage of `@next/next/*` ESLint rules and their severity levels.

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Documentation added
- [x] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 58ab94be
...@@ -25,6 +25,14 @@ type Config = { ...@@ -25,6 +25,14 @@ type Config = {
rules: { [key: string]: Array<number | string> } rules: { [key: string]: Array<number | string> }
} }
// 0 is off, 1 is warn, 2 is error. See https://eslint.org/docs/user-guide/configuring/rules#configuring-rules
const VALID_SEVERITY = ['off', 'warn', 'error'] as const
type Severity = typeof VALID_SEVERITY[number]
function isValidSeverity(severity: string): severity is Severity {
return VALID_SEVERITY.includes(severity as Severity)
}
const requiredPackages = [ const requiredPackages = [
{ file: 'eslint', pkg: 'eslint' }, { file: 'eslint', pkg: 'eslint' },
{ file: 'eslint-config-next', pkg: 'eslint-config-next' }, { file: 'eslint-config-next', pkg: 'eslint-config-next' },
...@@ -126,6 +134,7 @@ async function lint( ...@@ -126,6 +134,7 @@ async function lint(
let eslint = new ESLint(options) let eslint = new ESLint(options)
let nextEslintPluginIsEnabled = false let nextEslintPluginIsEnabled = false
const nextRulesEnabled = new Map<string, Severity>()
const pagesDirRules = ['@next/next/no-html-link-for-pages'] const pagesDirRules = ['@next/next/no-html-link-for-pages']
for (const configFile of [eslintrcFile, pkgJsonPath]) { for (const configFile of [eslintrcFile, pkgJsonPath]) {
...@@ -137,6 +146,23 @@ async function lint( ...@@ -137,6 +146,23 @@ async function lint(
if (completeConfig.plugins?.includes('@next/next')) { if (completeConfig.plugins?.includes('@next/next')) {
nextEslintPluginIsEnabled = true nextEslintPluginIsEnabled = true
for (const [name, [severity]] of Object.entries(completeConfig.rules)) {
if (!name.startsWith('@next/next/')) {
continue
}
if (
typeof severity === 'number' &&
severity >= 0 &&
severity < VALID_SEVERITY.length
) {
nextRulesEnabled.set(name, VALID_SEVERITY[severity])
} else if (
typeof severity === 'string' &&
isValidSeverity(severity)
) {
nextRulesEnabled.set(name, severity)
}
}
break break
} }
} }
...@@ -210,6 +236,7 @@ async function lint( ...@@ -210,6 +236,7 @@ async function lint(
nextEslintPluginErrorsCount: formattedResult.totalNextPluginErrorCount, nextEslintPluginErrorsCount: formattedResult.totalNextPluginErrorCount,
nextEslintPluginWarningsCount: nextEslintPluginWarningsCount:
formattedResult.totalNextPluginWarningCount, formattedResult.totalNextPluginWarningCount,
nextRulesEnabled: Object.fromEntries(nextRulesEnabled),
}, },
} }
} catch (err) { } catch (err) {
......
...@@ -34,6 +34,9 @@ export type EventLintCheckCompleted = { ...@@ -34,6 +34,9 @@ export type EventLintCheckCompleted = {
nextEslintPluginVersion?: string | null nextEslintPluginVersion?: string | null
nextEslintPluginErrorsCount?: number nextEslintPluginErrorsCount?: number
nextEslintPluginWarningsCount?: number nextEslintPluginWarningsCount?: number
nextRulesEnabled: {
[ruleName: `@next/next/${string}`]: 'off' | 'warn' | 'error'
}
} }
export function eventLintCheckCompleted(event: EventLintCheckCompleted): { export function eventLintCheckCompleted(event: EventLintCheckCompleted): {
......
...@@ -529,7 +529,7 @@ describe('Telemetry CLI', () => { ...@@ -529,7 +529,7 @@ describe('Telemetry CLI', () => {
}) })
await fs.remove(path.join(appDir, '.eslintrc')) await fs.remove(path.join(appDir, '.eslintrc'))
const event1 = /NEXT_LINT_CHECK_COMPLETED[\s\S]+?{([\s\S]+?)}/ const event1 = /NEXT_LINT_CHECK_COMPLETED[\s\S]+?{([\s\S}]+?)^}/m
.exec(stderr) .exec(stderr)
.pop() .pop()
...@@ -541,6 +541,8 @@ describe('Telemetry CLI', () => { ...@@ -541,6 +541,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/) expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/)
expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/)
expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/)
expect(event1).toMatch(`"nextRulesEnabled": {`)
expect(event1).toMatch(/"@next\/next\/.+?": "(off|warn|error)"/)
const event2 = /NEXT_BUILD_FEATURE_USAGE[\s\S]+?{([\s\S]+?)}/ const event2 = /NEXT_BUILD_FEATURE_USAGE[\s\S]+?{([\s\S]+?)}/
.exec(stderr) .exec(stderr)
...@@ -594,7 +596,7 @@ describe('Telemetry CLI', () => { ...@@ -594,7 +596,7 @@ describe('Telemetry CLI', () => {
}) })
await fs.remove(path.join(appDir, '.eslintrc')) await fs.remove(path.join(appDir, '.eslintrc'))
const event1 = /NEXT_LINT_CHECK_COMPLETED[\s\S]+?{([\s\S]+?)}/ const event1 = /NEXT_LINT_CHECK_COMPLETED[\s\S]+?{([\s\S]+?)^}/m
.exec(stderr) .exec(stderr)
.pop() .pop()
...@@ -606,6 +608,8 @@ describe('Telemetry CLI', () => { ...@@ -606,6 +608,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/) expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/)
expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginErrorsCount": \d{1,}/)
expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/) expect(event1).toMatch(/"nextEslintPluginWarningsCount": \d{1,}/)
expect(event1).toMatch(`"nextRulesEnabled": {`)
expect(event1).toMatch(/"@next\/next\/.+?": "(off|warn|error)"/)
}) })
it('emits telemery for usage of image, script & dynamic', async () => { it('emits telemery for usage of image, script & dynamic', async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment