File: prefer-placeholders.md

package info (click to toggle)
node-eslint-plugin-eslint-plugin 2.3.0%2B~0.3.0-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 652 kB
  • sloc: javascript: 5,372; makefile: 34; sh: 1
file content (64 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# disallow template literals as report messages (prefer-placeholders)

Report messages in rules can have placeholders surrounded by curly brackets.

```js
context.report({
  node,
  message: '{{disallowedNode}} nodes are not allowed.',
  data: { disallowedNode: node.type }
});
```

Using placeholders is often preferred over using dynamic report messages, for a few reasons:

* They can help enforce a separation of the message and the data.
* It will be easier to migrate when ESLint starts supporting placing lint messages in metadata (see [#6740](https://github.com/eslint/eslint/issues/6740))

## Rule Details

This rule aims to report string concatenation and template literals in report messages.

Examples of **incorrect** code for this rule:

```js
/* eslint eslint-plugin/prefer-placeholders: error */

module.exports = {
  create(context) {
    context.report({
      node,
      message: `The node ${node.name} is not allowed to be used.`
    });

    context.report({
      node,
      message: 'The node ' + node.name + ' is not allowed to be used.'
    });
  }
};
```

Examples of **correct** code for this rule:

```js
/* eslint eslint-plugin/prefer-placeholders: error */

module.exports = {
  create(context) {
    context.report({
      node,
      message: 'The node {{name}} is not allowed to be used.',
      data: { name: node.name }
    });
  }
};
```

## When Not To Use It

If you need to use string concatenation in your report messages for some reason, don't turn on this rule.

## Further Reading

* [context.report() API](http://eslint.org/docs/developer-guide/working-with-rules#contextreport)