This repository has been archived by the owner on Sep 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheslint-local-rules.js
156 lines (136 loc) · 4.23 KB
/
eslint-local-rules.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const IMPORT = 'IMPORT';
const EXPORT_NAMED = 'EXPORT_NAMED';
const DEFAULT_PROPS = 2;
/**
* Enforce placing import and export variables on separate lines
* @param node
* @param context
* @param moduleType - export or import
* @returns {{range: int[], text: string}|null}
*/
function lintModuleVariablesNewline(node, context, moduleType) {
const minProperties = context.options[0].minProperties || DEFAULT_PROPS;
const defaultCorrection = context.options[0].countDefault ? 0 : 1;
if (node.specifiers.length < minProperties) {
return null;
}
const sourceCode = context.getSourceCode();
const moduleTypeNameForMessage = moduleType === IMPORT ? 'imported' : 'exported';
let moduleVariables = null;
if (node.specifiers) {
// for import
moduleVariables = node.specifiers;
} else if (node.declaration.type === 'ObjectExpression') {
// for some exports (export { a, b, c }
moduleVariables = node.declaration.properties
}
if (!moduleVariables) {
return null;
}
const report = (fixer) => context.report({
fix: fixer,
message: `Each ${moduleTypeNameForMessage} variable should starts with a new line`,
node,
});
for (let i = 1; i < moduleVariables.length; i++) {
const firstTokenOfCurrentProperty = sourceCode.getFirstToken(moduleVariables[i]);
if (moduleVariables[i].loc.start.line === moduleVariables[i - 1].loc.start.line) {
const nodeSource = context.getSourceCode();
const containsDefault = node.specifiers.find((t) => t.type === 'ImportDefaultSpecifier')
? defaultCorrection
: 0;
const namedImportAfterDefault = moduleType === IMPORT
&& node.specifiers[i].type === 'ImportSpecifier'
&& (
node.specifiers[i - 1]
&& node.specifiers[i - 1].type === 'ImportDefaultSpecifier'
);
if (node.specifiers.length < minProperties + containsDefault) {
return null;
}
if (namedImportAfterDefault) {
const endOfDefaultImport = node.specifiers[i - 1].range[1];
const beginningOfNamedImport = node.specifiers[i].range[0];
const brace = nodeSource.tokensAndComments.find(
(token) => token.type === 'Punctuator'
&& token.value === '{'
&& token.range[0] >= endOfDefaultImport
&& token.range[1] <= beginningOfNamedImport
);
const rangeAfterBrace = [brace.range[0], brace.range[1]];
report((fixer) => fixer.replaceTextRange(rangeAfterBrace, '{\n'));
} else {
const comma = nodeSource.getTokenBefore(firstTokenOfCurrentProperty);
const rangeAfterComma = [comma.range[1], firstTokenOfCurrentProperty.range[0]];
// don't fix if comments between the comma and the next property.
if (sourceCode.text.slice(rangeAfterComma[0], rangeAfterComma[1]).trim()) {
return null;
}
report((fixer) => fixer.replaceTextRange(rangeAfterComma, '\n'));
}
}
}
}
const schema = {
additionalProperties: false,
properties: {
countDefault: {
type: 'boolean'
},
minProperties: {
type: 'number',
},
},
type: 'object',
};
const exportDeclarationNewline = {
create: (context) => ({
ExportNamedDeclaration(node) {
lintModuleVariablesNewline(node, context, EXPORT_NAMED)
},
}),
meta: {
fixable: true,
schema: [
schema
],
},
};
const importDeclarationNewline = {
create: (context) => ({
ImportDeclaration(node) {
lintModuleVariablesNewline(node, context, IMPORT)
},
}),
meta: {
fixable: true,
schema: [
schema
],
},
};
// eslint-disable-next-line no-undef
module.exports = {
'export-declaration-newline': exportDeclarationNewline,
'import-declaration-newline': importDeclarationNewline,
// 'test-rule': {
// meta: {
// docs: {
// description: 'disallow identifiers',
// category: 'Possible Errors',
// recommended: false,
// },
// schema: [],
// },
// create: function(context) {
// return {
// Identifier: function(node) {
// context.report({
// node: node,
// message: 'Identifiers not allowed for Super Important reasons.',
// });
// },
// };
// },
// },
};