UNPKG

1.38 kBJavaScriptView Raw
1const { multilineFixer } = require('./es-beautifier-common.js');
2
3const findOpeningToken = sourceCode =>
4 node => sourceCode.getFirstToken(node);
5
6const findClosingToken = sourceCode =>
7 node => sourceCode.getLastToken(node);
8
9const create = (context) => {
10 const option = context.options[0] || {
11 allowSingleLine: true,
12 maxAttributesInSingleLine: 3,
13 maxLenInSingleLine: 80,
14 };
15 const allowSingleLine = option.allowSingleLine;
16 const maxChildren = option.maxAttributesInSingleLine;
17 const maxLen = option.maxLenInSingleLine;
18
19 const sourceCode = context.getSourceCode();
20
21 const enterJSXOpeningElement = multilineFixer({
22 allowSingleLine,
23 maxChildren,
24 maxLen,
25 context,
26 sourceCode,
27 childrenName: 'attributes',
28 message: 'Attribute in JSX should be on a new line.',
29 findOpeningToken,
30 findClosingToken,
31 });
32
33 return {
34 JSXOpeningElement: enterJSXOpeningElement,
35 };
36};
37
38module.exports = {
39 meta: {
40 docs: {
41 description: 'enforce multi-line attributes in JSX',
42 category: 'Stylistic Issues',
43 },
44 fixable: 'whitespace',
45 schema: [{
46 type: 'object',
47 properties: {
48 allowSingleLine: { type: 'boolean' },
49 maxAttributesInSingleLine: { type: 'integer' },
50 maxLenInSingleLine: { type: 'integer' },
51 },
52 additionalProperties: false,
53 }],
54 },
55 create,
56};