Skip to content

Commit 074b490

Browse files
committed
feat: eslint rule message improvements
1 parent c94b8b1 commit 074b490

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

packages/eslint-plugin-sdk/src/rules/no-unsafe-random-apis.js

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,33 @@
88
* random number generation in certain contexts (e.g., React Server Components with caching).
99
*/
1010

11-
// APIs that should be wrapped with runInRandomSafeContext
11+
// APIs that should be wrapped with runInRandomSafeContext, with their specific messages
1212
const UNSAFE_MEMBER_CALLS = [
13-
{ object: 'Date', property: 'now' },
14-
{ object: 'Math', property: 'random' },
15-
{ object: 'performance', property: 'now' },
16-
{ object: 'crypto', property: 'randomUUID' },
17-
{ object: 'crypto', property: 'getRandomValues' },
13+
{
14+
object: 'Date',
15+
property: 'now',
16+
messageId: 'unsafeDateNow',
17+
},
18+
{
19+
object: 'Math',
20+
property: 'random',
21+
messageId: 'unsafeMathRandom',
22+
},
23+
{
24+
object: 'performance',
25+
property: 'now',
26+
messageId: 'unsafePerformanceNow',
27+
},
28+
{
29+
object: 'crypto',
30+
property: 'randomUUID',
31+
messageId: 'unsafeCryptoRandomUUID',
32+
},
33+
{
34+
object: 'crypto',
35+
property: 'getRandomValues',
36+
messageId: 'unsafeCryptoGetRandomValues',
37+
},
1838
];
1939

2040
module.exports = {
@@ -29,8 +49,16 @@ module.exports = {
2949
fixable: null,
3050
schema: [],
3151
messages: {
32-
unsafeRandomApi:
33-
'{{ api }} should be wrapped with runInRandomSafeContext() to ensure safe random/time value generation. Use: runInRandomSafeContext(() => {{ api }}). You can disable this rule with an eslint-disable comment if this usage is intentional.',
52+
unsafeDateNow:
53+
'`Date.now()` should be replaced with `safeDateNow()` from `@sentry/core` to ensure safe time value generation. You can disable this rule with an eslint-disable comment if this usage is intentional.',
54+
unsafeMathRandom:
55+
'`Math.random()` should be replaced with `safeMathRandom()` from `@sentry/core` to ensure safe random value generation. You can disable this rule with an eslint-disable comment if this usage is intentional.',
56+
unsafePerformanceNow:
57+
'`performance.now()` should be wrapped with `runInRandomSafeContext()` to ensure safe time value generation. Use: `runInRandomSafeContext(() => performance.now())`. You can disable this rule with an eslint-disable comment if this usage is intentional.',
58+
unsafeCryptoRandomUUID:
59+
'`crypto.randomUUID()` should be wrapped with `runInRandomSafeContext()` to ensure safe random value generation. Use: `runInRandomSafeContext(() => crypto.randomUUID())`. You can disable this rule with an eslint-disable comment if this usage is intentional.',
60+
unsafeCryptoGetRandomValues:
61+
'`crypto.getRandomValues()` should be wrapped with `runInRandomSafeContext()` to ensure safe random value generation. Use: `runInRandomSafeContext(() => crypto.getRandomValues(...))`. You can disable this rule with an eslint-disable comment if this usage is intentional.',
3462
},
3563
},
3664
create: function (context) {
@@ -104,17 +132,12 @@ module.exports = {
104132
}
105133

106134
// Check if this is one of the unsafe APIs
107-
const isUnsafeApi = UNSAFE_MEMBER_CALLS.some(
108-
api => api.object === objectName && api.property === propertyName,
109-
);
135+
const unsafeApi = UNSAFE_MEMBER_CALLS.find(api => api.object === objectName && api.property === propertyName);
110136

111-
if (isUnsafeApi && !isInsideRunInRandomSafeContext(node)) {
137+
if (unsafeApi && !isInsideRunInRandomSafeContext(node)) {
112138
context.report({
113139
node,
114-
messageId: 'unsafeRandomApi',
115-
data: {
116-
api: `${objectName}.${propertyName}()`,
117-
},
140+
messageId: unsafeApi.messageId,
118141
});
119142
}
120143
}

packages/eslint-plugin-sdk/test/lib/rules/no-unsafe-random-apis.test.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ describe('no-unsafe-random-apis', () => {
6161
code: 'const time = Date.now()',
6262
errors: [
6363
{
64-
messageId: 'unsafeRandomApi',
65-
data: { api: 'Date.now()' },
64+
messageId: 'unsafeDateNow',
6665
},
6766
],
6867
},
@@ -71,8 +70,7 @@ describe('no-unsafe-random-apis', () => {
7170
code: 'const random = Math.random()',
7271
errors: [
7372
{
74-
messageId: 'unsafeRandomApi',
75-
data: { api: 'Math.random()' },
73+
messageId: 'unsafeMathRandom',
7674
},
7775
],
7876
},
@@ -81,8 +79,7 @@ describe('no-unsafe-random-apis', () => {
8179
code: 'const perf = performance.now()',
8280
errors: [
8381
{
84-
messageId: 'unsafeRandomApi',
85-
data: { api: 'performance.now()' },
82+
messageId: 'unsafePerformanceNow',
8683
},
8784
],
8885
},
@@ -91,8 +88,7 @@ describe('no-unsafe-random-apis', () => {
9188
code: 'const uuid = crypto.randomUUID()',
9289
errors: [
9390
{
94-
messageId: 'unsafeRandomApi',
95-
data: { api: 'crypto.randomUUID()' },
91+
messageId: 'unsafeCryptoRandomUUID',
9692
},
9793
],
9894
},
@@ -101,8 +97,7 @@ describe('no-unsafe-random-apis', () => {
10197
code: 'const bytes = crypto.getRandomValues(new Uint8Array(16))',
10298
errors: [
10399
{
104-
messageId: 'unsafeRandomApi',
105-
data: { api: 'crypto.getRandomValues()' },
100+
messageId: 'unsafeCryptoGetRandomValues',
106101
},
107102
],
108103
},
@@ -111,8 +106,7 @@ describe('no-unsafe-random-apis', () => {
111106
code: 'function getTime() { return Date.now(); }',
112107
errors: [
113108
{
114-
messageId: 'unsafeRandomApi',
115-
data: { api: 'Date.now()' },
109+
messageId: 'unsafeDateNow',
116110
},
117111
],
118112
},
@@ -121,8 +115,7 @@ describe('no-unsafe-random-apis', () => {
121115
code: 'const getTime = () => Date.now()',
122116
errors: [
123117
{
124-
messageId: 'unsafeRandomApi',
125-
data: { api: 'Date.now()' },
118+
messageId: 'unsafeDateNow',
126119
},
127120
],
128121
},
@@ -131,8 +124,7 @@ describe('no-unsafe-random-apis', () => {
131124
code: 'someOtherWrapper(() => Date.now())',
132125
errors: [
133126
{
134-
messageId: 'unsafeRandomApi',
135-
data: { api: 'Date.now()' },
127+
messageId: 'unsafeDateNow',
136128
},
137129
],
138130
},
@@ -141,12 +133,10 @@ describe('no-unsafe-random-apis', () => {
141133
code: 'const a = Date.now(); const b = Math.random();',
142134
errors: [
143135
{
144-
messageId: 'unsafeRandomApi',
145-
data: { api: 'Date.now()' },
136+
messageId: 'unsafeDateNow',
146137
},
147138
{
148-
messageId: 'unsafeRandomApi',
149-
data: { api: 'Math.random()' },
139+
messageId: 'unsafeMathRandom',
150140
},
151141
],
152142
},

0 commit comments

Comments
 (0)