Skip to content

Commit

Permalink
collect telemetry for ESLint rules (#34564)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
kyliau committed Feb 24, 2022
1 parent 58ab94b commit fbb8536
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
27 changes: 27 additions & 0 deletions packages/next/lib/eslint/runLintCheck.ts
Expand Up @@ -25,6 +25,14 @@ type Config = {
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 = [
{ file: 'eslint', pkg: 'eslint' },
{ file: 'eslint-config-next', pkg: 'eslint-config-next' },
Expand Down Expand Up @@ -126,6 +134,7 @@ async function lint(
let eslint = new ESLint(options)

let nextEslintPluginIsEnabled = false
const nextRulesEnabled = new Map<string, Severity>()
const pagesDirRules = ['@next/next/no-html-link-for-pages']

for (const configFile of [eslintrcFile, pkgJsonPath]) {
Expand All @@ -137,6 +146,23 @@ async function lint(

if (completeConfig.plugins?.includes('@next/next')) {
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
}
}
Expand Down Expand Up @@ -210,6 +236,7 @@ async function lint(
nextEslintPluginErrorsCount: formattedResult.totalNextPluginErrorCount,
nextEslintPluginWarningsCount:
formattedResult.totalNextPluginWarningCount,
nextRulesEnabled: Object.fromEntries(nextRulesEnabled),
},
}
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions packages/next/telemetry/events/build.ts
Expand Up @@ -34,6 +34,9 @@ export type EventLintCheckCompleted = {
nextEslintPluginVersion?: string | null
nextEslintPluginErrorsCount?: number
nextEslintPluginWarningsCount?: number
nextRulesEnabled: {
[ruleName: `@next/next/${string}`]: 'off' | 'warn' | 'error'
}
}

export function eventLintCheckCompleted(event: EventLintCheckCompleted): {
Expand Down
8 changes: 6 additions & 2 deletions test/integration/telemetry/test/index.test.js
Expand Up @@ -529,7 +529,7 @@ describe('Telemetry CLI', () => {
})
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)
.pop()

Expand All @@ -541,6 +541,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/)
expect(event1).toMatch(/"nextEslintPluginErrorsCount": \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]+?)}/
.exec(stderr)
Expand Down Expand Up @@ -594,7 +596,7 @@ describe('Telemetry CLI', () => {
})
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)
.pop()

Expand All @@ -606,6 +608,8 @@ describe('Telemetry CLI', () => {
expect(event1).toMatch(/"nextEslintPluginVersion": ".*?\..*?\..*?"/)
expect(event1).toMatch(/"nextEslintPluginErrorsCount": \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 () => {
Expand Down

0 comments on commit fbb8536

Please sign in to comment.