Repository navigation
chore: Replace AGENTS.md rules with lints and tests #27188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+387
−43
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Checks literal `sentry.origin` values against the trace origin spec: | ||
| * https://develop.sentry.dev/sdk/telemetry/traces/trace-origin/ | ||
| * | ||
| * It checks string literals written to an origin attribute, and string | ||
| * literals assigned to a constant whose name ends in `ORIGIN`. Origins | ||
| * built with a template literal or imported from elsewhere are not | ||
| * checked. Whether the `category` part is the span op category | ||
| * (`auto.queue.kafkajs.producer`, not `auto.kafkajs.producer`) needs a | ||
| * human. | ||
| */ | ||
|
|
||
| const ORIGIN_KEYS = new Set(['SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN', 'SENTRY_ORIGIN', 'sentry.origin']); | ||
| const ORIGIN_FORMAT = /^(auto|manual)(\.[a-zA-Z0-9_]+){0,3}$/; | ||
| const TOOLING_PARTS = new Set(['otel', 'orchestrion']); | ||
| const ORIGIN_CONSTANT = /(^|_)ORIGIN$/; | ||
|
|
||
| function getKeyName(node, computed) { | ||
| if (computed && node.type === 'Identifier') { | ||
| return node.name; | ||
| } | ||
| if (node.type === 'Literal' && typeof node.value === 'string') { | ||
| return node.value; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'Enforce the trace origin format for literal `sentry.origin` values.', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| badFormat: | ||
| "Span origin '{{origin}}' must be '<type>.<category>.<integration_name>.<integration_part>': type 'auto' or 'manual', at most four parts, only [a-zA-Z0-9_] in a part. See https://develop.sentry.dev/sdk/telemetry/traces/trace-origin/", | ||
| toolingPart: | ||
| "Span origin '{{origin}}' names tooling ('{{part}}'). The origin identifies the integration, not how it is built.", | ||
| }, | ||
| }, | ||
| create(context) { | ||
| function check(valueNode) { | ||
| if (!valueNode || valueNode.type !== 'Literal' || typeof valueNode.value !== 'string') { | ||
| return; | ||
| } | ||
| const origin = valueNode.value; | ||
| if (!ORIGIN_FORMAT.test(origin)) { | ||
| context.report({ node: valueNode, messageId: 'badFormat', data: { origin } }); | ||
| return; | ||
| } | ||
| const part = origin.split('.').find(p => TOOLING_PARTS.has(p)); | ||
| if (part) { | ||
| context.report({ node: valueNode, messageId: 'toolingPart', data: { origin, part } }); | ||
| } | ||
| } | ||
|
Check notice on line 58 in packages/eslint-plugin-sdk/src/rules/valid-span-origin.js
|
||
|
|
||
| return { | ||
| // const ORIGIN = 'auto.cache.cloudflare.kv'; | ||
| VariableDeclarator(node) { | ||
| if (node.id.type === 'Identifier' && ORIGIN_CONSTANT.test(node.id.name) && !ORIGIN_KEYS.has(node.id.name)) { | ||
| check(node.init); | ||
| } | ||
| }, | ||
| Property(node) { | ||
| if (ORIGIN_KEYS.has(getKeyName(node.key, node.computed))) { | ||
| check(node.value); | ||
| } | ||
| }, | ||
| // span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, '...') | ||
| CallExpression(node) { | ||
| const callee = node.callee; | ||
| if ( | ||
| callee.type === 'MemberExpression' && | ||
| callee.property.type === 'Identifier' && | ||
| callee.property.name === 'setAttribute' && | ||
| node.arguments.length >= 2 && | ||
| ORIGIN_KEYS.has(getKeyName(node.arguments[0], true)) | ||
| ) { | ||
| check(node.arguments[1]); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
61 changes: 61 additions & 0 deletions
61
packages/eslint-plugin-sdk/test/lib/rules/valid-span-origin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { RuleTester } from 'eslint'; | ||
| import { describe, test } from 'vitest'; | ||
| // @ts-expect-error untyped module | ||
| import rule from '../../../src/rules/valid-span-origin'; | ||
|
|
||
| describe('valid-span-origin', () => { | ||
| test('ruleTester', () => { | ||
| const ruleTester = new RuleTester({ | ||
| parserOptions: { | ||
| ecmaVersion: 2020, | ||
| }, | ||
| }); | ||
|
|
||
| ruleTester.run('valid-span-origin', rule, { | ||
| valid: [ | ||
| { code: "({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.queue.kafkajs.producer' });" }, | ||
| { code: "({ [SENTRY_ORIGIN]: 'auto.http.node' });" }, | ||
| { code: "({ 'sentry.origin': 'manual' });" }, | ||
| { code: "span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.pg');" }, | ||
| { code: '({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: origin });' }, | ||
| { code: "({ origin: 'not.checked' });" }, | ||
| { code: "span.setAttribute('other', 'a-b');" }, | ||
| { code: "const ORIGIN = 'auto.cache.cloudflare.kv';" }, | ||
| { code: "const OPENAI_ORIGIN = 'auto.ai.openai';" }, | ||
| { code: "const SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN = 'sentry.origin';" }, | ||
| { code: "const ORIGINAL_ENTRY_QUERY = '?sentry-cloudflare-original';" }, | ||
| { code: 'const ORIGIN = `auto.${op}.angular`;' }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.http.node.server.extra' });", | ||
| errors: [{ messageId: 'badFormat' }], | ||
| }, | ||
| { | ||
| code: "({ [SENTRY_ORIGIN]: 'auto.http.my-lib' });", | ||
| errors: [{ messageId: 'badFormat' }], | ||
| }, | ||
| { | ||
| code: "({ 'sentry.origin': 'node.http' });", | ||
| errors: [{ messageId: 'badFormat' }], | ||
| }, | ||
| { | ||
| code: "span.setAttribute('sentry.origin', 'auto.http..node');", | ||
| errors: [{ messageId: 'badFormat' }], | ||
| }, | ||
| { | ||
| code: "const ORIGIN = 'auto.cache.my-lib';", | ||
| errors: [{ messageId: 'badFormat' }], | ||
| }, | ||
| { | ||
| code: "const MONGO_ORIGIN = 'auto.db.otel.mongo';", | ||
| errors: [{ messageId: 'toolingPart', data: { origin: 'auto.db.otel.mongo', part: 'otel' } }], | ||
| }, | ||
| { | ||
| code: "({ [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.otel.mongo' });", | ||
| errors: [{ messageId: 'toolingPart', data: { origin: 'auto.db.otel.mongo', part: 'otel' } }], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { execFileSync } from 'node:child_process'; | ||
| import { readdirSync, readFileSync, existsSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const ROOT = join(import.meta.dirname, '..'); | ||
|
|
||
| // Cloudflare bundlers set `workerd`, `worker` and `browser`, and the first | ||
| // matching key wins. A subpath with a worker entry needs `workerd` before | ||
| // `browser`, or Cloudflare gets the browser entry. Where `worker` goes is a | ||
| // per-package choice: webpack `target: 'webworker'` builds for browser Web | ||
| // Workers set `worker` and `browser` too. | ||
| function findMissingWorkerd(name, exportsField) { | ||
| const found = []; | ||
| for (const [subpath, conditions] of Object.entries(exportsField)) { | ||
| if (!conditions || typeof conditions !== 'object') continue; | ||
| const keys = Object.keys(conditions); | ||
| const browser = keys.indexOf('browser'); | ||
| if (browser === -1 || !keys.some(key => key === 'worker' || key === 'workerd')) continue; | ||
| const workerd = keys.indexOf('workerd'); | ||
| if (workerd === -1 || workerd > browser) { | ||
| found.push(`${name} ${subpath}`); | ||
| } | ||
| } | ||
| return found; | ||
| } | ||
|
|
||
| describe('package exports', () => { | ||
| it('put `workerd` before `browser` in subpaths with a worker entry', () => { | ||
| const found = []; | ||
| for (const dir of readdirSync(join(ROOT, 'packages'))) { | ||
| const file = join(ROOT, 'packages', dir, 'package.json'); | ||
| if (!existsSync(file)) continue; | ||
| const pkg = JSON.parse(readFileSync(file, 'utf8')); | ||
| if (pkg.exports && typeof pkg.exports === 'object') { | ||
| found.push(...findMissingWorkerd(pkg.name, pkg.exports)); | ||
| } | ||
| } | ||
| expect(found).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('dev-packages tests', () => { | ||
| // New tests must exercise the default, span streaming. Do not add files | ||
| // to the baseline; remove them when they stop setting `traceLifecycle`. | ||
| it('do not set `traceLifecycle` outside the baseline', () => { | ||
| const baseline = new Set(JSON.parse(readFileSync(join(ROOT, 'scripts/trace-lifecycle-baseline.json'), 'utf8'))); | ||
| const files = execFileSync('git', ['grep', '--untracked', '-l', 'traceLifecycle', '--', 'dev-packages'], { | ||
| cwd: ROOT, | ||
| encoding: 'utf8', | ||
| }) | ||
| .trim() | ||
| .split('\n'); | ||
| expect(files.filter(file => !baseline.has(file))).toEqual([]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.