@@ -36,6 +36,7 @@ async function main(ruleId) {
36
36
const ruleFile = path . resolve ( __dirname , `../lib/rules/${ ruleId } .js` )
37
37
const testFile = path . resolve ( __dirname , `../tests/lib/rules/${ ruleId } .js` )
38
38
const docFile = path . resolve ( __dirname , `../docs/rules/${ ruleId } .md` )
39
+ const changesetFile = path . resolve ( __dirname , `../.changeset/${ ruleId } .md` )
39
40
40
41
prompts . intro ( "Create the new rule!" )
41
42
@@ -47,10 +48,10 @@ async function main(ruleId) {
47
48
value : "global-object" ,
48
49
label : "The rule forbids the use of global objects." ,
49
50
} ,
50
- // {
51
- // value: "static-properties",
52
- // label: "The rule forbids the use of static properties.",
53
- // },
51
+ {
52
+ value : "static-properties" ,
53
+ label : "The rule forbids the use of static properties." ,
54
+ } ,
54
55
// {
55
56
// value: "prototype-properties",
56
57
// label: "The rule forbids the use of prototype properties.",
@@ -131,20 +132,32 @@ async function main(ruleId) {
131
132
. filter ( ( s ) => s )
132
133
}
133
134
135
+ const BUILDERS = {
136
+ "global-object" : buildGlobalObjectRuleResources ,
137
+ "static-properties" : buildStaticPropertiesRuleResources ,
138
+ "nonstandard-static-properties" :
139
+ buildNonStandardStaticPropertiesRuleResources ,
140
+ "nonstandard-prototype-properties" :
141
+ buildNonStandardPrototypePropertiesRuleResources ,
142
+ default : buildDefaultResources ,
143
+ }
144
+
134
145
const resources =
135
- kind === "global-object"
136
- ? buildGlobalObjectRuleResources ( resourceOptions )
137
- : kind === "nonstandard-static-properties"
138
- ? buildNonStandardStaticPropertiesRuleResources ( resourceOptions )
139
- : kind === "nonstandard-prototype-properties"
140
- ? buildNonStandardPrototypePropertiesRuleResources (
141
- resourceOptions ,
142
- )
143
- : buildDefaultResources ( resourceOptions )
146
+ BUILDERS [ kind ] ?. ( resourceOptions ) ??
147
+ buildDefaultResources ( resourceOptions )
144
148
145
149
fs . writeFileSync ( ruleFile , resources . rule )
146
150
fs . writeFileSync ( testFile , resources . test )
147
151
fs . writeFileSync ( docFile , resources . doc )
152
+ fs . writeFileSync (
153
+ changesetFile ,
154
+ `---
155
+ "eslint-plugin-es-x": minor
156
+ ---
157
+
158
+ Add \`es-x/${ ruleId } \` rule
159
+ ` ,
160
+ )
148
161
149
162
cp . execSync ( `code "${ ruleFile } "` )
150
163
cp . execSync ( `code "${ testFile } "` )
@@ -243,6 +256,134 @@ let ${object.toLowerCase()} = new ${object}()
243
256
}
244
257
}
245
258
259
+ function buildStaticPropertiesRuleResources ( { ruleId, object, properties } ) {
260
+ const promptObject = globalThis [ object ]
261
+ const exampleProperty = promptObject
262
+ ? Object . getOwnPropertyNames ( promptObject ) [ 0 ]
263
+ : "example"
264
+ const propertyType =
265
+ promptObject && promptObject [ properties [ 0 ] ]
266
+ ? typeof promptObject [ properties [ 0 ] ]
267
+ : "function"
268
+ const kind =
269
+ propertyType === "function"
270
+ ? [ "method" , "methods" ]
271
+ : [ "property" , "properties" ]
272
+ const propertiesString =
273
+ properties . length > 1 ? `{${ properties . join ( "," ) } }` : properties [ 0 ]
274
+ let propertiesName = `\`${ object } .${ properties [ properties . length - 1 ] } \` ${ kind [ 0 ] } `
275
+ if ( properties . length > 1 ) {
276
+ propertiesName = `${ properties
277
+ . slice ( 0 , - 1 )
278
+ . map ( ( p ) => `\`${ object } .${ p } \`` )
279
+ . join (
280
+ ", " ,
281
+ ) } , and \`${ object } .${ properties [ properties . length - 1 ] } \` ${ kind [ 1 ] } `
282
+ }
283
+
284
+ return {
285
+ rule : `"use strict"
286
+
287
+ const {
288
+ defineStaticPropertiesHandler,
289
+ } = require("../util/define-static-properties-handler")
290
+
291
+ module.exports = {
292
+ meta: {
293
+ docs: {
294
+ description: "disallow the \`${ object } .${ propertiesString } \` ${ kind [ 0 ] } ",
295
+ category: "ES${ maxESVersion } ",
296
+ recommended: false,
297
+ url: "",
298
+ },
299
+ fixable: null,
300
+ messages: {
301
+ forbidden: "ES${ maxESVersion } '{{name}}' ${ kind [ 0 ] } is forbidden.",
302
+ },
303
+ schema: [
304
+ {
305
+ type: "object",
306
+ properties: {
307
+ allowTestedProperty: { type: "boolean" },
308
+ },
309
+ additionalProperties: false,
310
+ },
311
+ ],
312
+ type: "problem",
313
+ },
314
+ create(context) {
315
+ return defineStaticPropertiesHandler(context, {
316
+ "${ object } ": { ${ properties . map ( ( p ) => `"${ p } ": "${ propertyType } "` ) . join ( ",\n" ) } },
317
+ })
318
+ },
319
+ }
320
+ ` ,
321
+ test : `"use strict"
322
+
323
+ const RuleTester = require("../../tester")
324
+ const rule = require("../../../lib/rules/${ ruleId } .js")
325
+
326
+ new RuleTester().run("${ ruleId } ", rule, {
327
+ valid: [
328
+ "${ object } ",
329
+ "${ object } .${ exampleProperty } ",
330
+ ${ properties . map ( ( p ) => `"let ${ object } = 0; ${ object } .${ p } "` ) . join ( ",\n" ) }
331
+ ],
332
+ invalid: [
333
+ ${ properties
334
+ . map (
335
+ ( p ) => `{
336
+ code: "${ object } .${ p } ",
337
+ errors: ["ES${ maxESVersion } '${ object } .${ p } ' ${ kind [ 0 ] } is forbidden."],
338
+ }` ,
339
+ )
340
+ . join ( ",\n" ) }
341
+ ],
342
+ })
343
+ ` ,
344
+ doc : `# es-x/${ ruleId }
345
+ >
346
+
347
+ This rule reports ES${ maxESVersion } [${ propertiesName } ]($$LINK$$) as errors.
348
+
349
+ ## 💡 Examples
350
+
351
+ ⛔ Examples of **incorrect** code for this rule:
352
+
353
+ <eslint-playground type="bad">
354
+
355
+ \`\`\`js
356
+ /*eslint es-x/${ ruleId } : error */
357
+ ${ properties . map ( ( p ) => `${ object } .${ p } ();` ) . join ( "\n" ) }
358
+ \`\`\`
359
+
360
+ </eslint-playground>
361
+
362
+ ## 🔧 Options
363
+
364
+ This rule has an option.
365
+
366
+ \`\`\`jsonc
367
+ {
368
+ "rules": {
369
+ "es-x/${ ruleId } ": [
370
+ "error",
371
+ {
372
+ "allowTestedProperty": false
373
+ }
374
+ ]
375
+ }
376
+ }
377
+ \`\`\`
378
+
379
+ ### allowTestedProperty: boolean
380
+
381
+ Configure the allowTestedProperty mode for only this rule.
382
+ This is prior to the \`settings['es-x'].allowTestedProperty\` setting.
383
+ ` ,
384
+ }
385
+ }
386
+
246
387
function buildNonStandardStaticPropertiesRuleResources ( { ruleId, object } ) {
247
388
const camelObject = camelCase ( object )
248
389
return {
0 commit comments