-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patheslint-local-rules.js
More file actions
151 lines (132 loc) · 4.92 KB
/
Copy patheslint-local-rules.js
File metadata and controls
151 lines (132 loc) · 4.92 KB
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
'use strict';
function validateFirstLine(firstLine, context, node) {
const isValidFirstLine = [
'// SPDX-License-Identifier: GPL-3.0-only',
'// SPDX-License-Identifier: MIT',
].some(header => firstLine.includes(header));
if (!isValidFirstLine) {
context.report({
node,
message:
'The first line should be "// SPDX-License-Identifier: GPL-3.0-only" or "// SPDX-License-Identifier: MIT"',
loc: {line: 1, column: 0},
fix(fixer) {
const correctFirstLine =
'// SPDX-License-Identifier: GPL-3.0-only';
if (firstLine.startsWith('// SPDX')) {
return fixer.replaceTextRange(
[0, firstLine.length],
correctFirstLine,
);
} else {
return fixer.insertTextBeforeRange(
[0, 0],
correctFirstLine + '\n',
);
}
},
});
}
}
const fileCopyRightTextRegExp = new RegExp(
/SPDX-FileCopyrightText: Copyright 2021-\d+ Panther Protocol Foundation/,
);
function validateSecondLine(secondLine, context, node) {
const isValidSecondLine = fileCopyRightTextRegExp.test(secondLine);
const year = new Date().getFullYear() - 2000;
const correctSecondLine = `// SPDX-FileCopyrightText: Copyright 2021-${year} Panther Protocol Foundation`;
if (!isValidSecondLine) {
context.report({
node,
message: `The second line should be "${correctSecondLine}"`,
loc: {line: 2, column: 0},
fix(fixer) {
const sourceCode = context.getSourceCode();
const lines = sourceCode.lines;
if (secondLine.startsWith('// SPDX-FileCopyrightText:')) {
const modifiedLines = [
...lines.slice(0, 1),
correctSecondLine,
...lines.slice(2),
];
const modifiedCode = modifiedLines.join('\n');
return fixer.replaceTextRange(
[0, sourceCode.text.length],
modifiedCode,
);
} else {
const sourceCode = context.getSourceCode();
const lines = sourceCode.lines;
const modifiedLines = [
...lines.slice(0, 1),
correctSecondLine,
...lines.slice(1),
];
const modifiedCode = modifiedLines.join('\n');
return fixer.replaceTextRange(
[0, sourceCode.text.length],
modifiedCode,
);
}
},
});
}
}
function validateThirdLine(thirdLine, context, node) {
const isValidThirdLine = thirdLine.trim().length === 0;
if (!isValidThirdLine) {
context.report({
node,
message: `The third line should be empty`,
loc: {line: 3, column: 0},
fix(fixer) {
const sourceCode = context.getSourceCode();
const lines = sourceCode.lines;
const modifiedLines = [
...lines.slice(0, 2),
'',
...lines.slice(2),
];
const modifiedCode = modifiedLines.join('\n');
return fixer.replaceTextRange(
[0, sourceCode.text.length],
modifiedCode,
);
},
});
}
}
module.exports = {
'license-header': {
meta: {
type: 'problem',
docs: {
description: 'enforce header comments',
category: 'Possible Errors',
},
fixable: 'code',
},
create: function (context) {
const options = context.options[0] ?? {};
const ignorePaths = options.ignorePaths || [];
const filePath = context.getFilename();
if (ignorePaths.some(path => filePath.includes(path))) {
return {};
}
return {
Program: function (node) {
const lines = context.getSourceCode().lines;
const firstLine = lines[0] ?? '';
const secondLine = lines[1] ?? '';
const thirdLine = lines[2] ?? '';
if (firstLine.startsWith('#!/usr/bin/env')) {
return;
}
validateFirstLine(firstLine, context, node);
validateSecondLine(secondLine, context, node);
validateThirdLine(thirdLine, context, node);
},
};
},
},
};