1 | 'use strict'
|
2 |
|
3 | const getDocsUrl = require('./lib/get-docs-url')
|
4 | const {
|
5 | isPromiseConstructorWithInlineExecutor,
|
6 | } = require('./lib/is-promise-constructor')
|
7 |
|
8 | module.exports = {
|
9 | meta: {
|
10 | type: 'suggestion',
|
11 | docs: {
|
12 | url: getDocsUrl('param-names'),
|
13 | },
|
14 | schema: [
|
15 | {
|
16 | type: 'object',
|
17 | properties: {
|
18 | resolvePattern: { type: 'string' },
|
19 | rejectPattern: { type: 'string' },
|
20 | },
|
21 | additionalProperties: false,
|
22 | },
|
23 | ],
|
24 | },
|
25 | create(context) {
|
26 | const options = context.options[0] || {}
|
27 | const resolvePattern = new RegExp(
|
28 | options.resolvePattern || '^_?resolve$',
|
29 | 'u'
|
30 | )
|
31 | const rejectPattern = new RegExp(options.rejectPattern || '^_?reject$', 'u')
|
32 |
|
33 | return {
|
34 | NewExpression(node) {
|
35 | if (isPromiseConstructorWithInlineExecutor(node)) {
|
36 | const params = node.arguments[0].params
|
37 |
|
38 | if (!params || !params.length) {
|
39 | return
|
40 | }
|
41 |
|
42 | const resolveParamName = params[0] && params[0].name
|
43 | if (resolveParamName && !resolvePattern.test(resolveParamName)) {
|
44 | context.report({
|
45 | node: params[0],
|
46 | message:
|
47 | 'Promise constructor parameters must be named to match "{{ resolvePattern }}"',
|
48 | data: {
|
49 | resolvePattern: resolvePattern.source,
|
50 | },
|
51 | })
|
52 | }
|
53 | const rejectParamName = params[1] && params[1].name
|
54 | if (rejectParamName && !rejectPattern.test(rejectParamName)) {
|
55 | context.report({
|
56 | node: params[1],
|
57 | message:
|
58 | 'Promise constructor parameters must be named to match "{{ rejectPattern }}"',
|
59 | data: {
|
60 | rejectPattern: rejectPattern.source,
|
61 | },
|
62 | })
|
63 | }
|
64 | }
|
65 | },
|
66 | }
|
67 | },
|
68 | }
|