This repository has been archived by the owner on Jul 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
license-header.js
107 lines (94 loc) · 3.28 KB
/
license-header.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
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @noflow
*/
'use strict';
/* eslint comma-dangle: [1, always-multiline], prefer-object-spread/prefer-object-spread: 0 */
const FAKE_DISABLE_RE = /\s*eslint-disable\s+nuclide-internal\/license-header\s*/;
const SHEBANG_RE = /^#!\/usr\/bin\/env node\n/;
const FLOW_FORMAT_AND_TRANSPILE = `\
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @format
*/
`;
const NO_FLOW_AND_NO_TRANSPILE = `\
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @noflow
*/
'use strict';
/* eslint comma-dangle: [1, always-multiline], prefer-object-spread/prefer-object-spread: 0 */
`;
module.exports = function(context) {
// "eslint-disable" disables rules after it. Since the directives have to go
// first, we can't use that mechanism to disable this check.
const comments = context.getAllComments();
for (let i = 0; i < comments.length; i++) {
if (FAKE_DISABLE_RE.test(comments[i].value)) {
return {};
}
}
return {
Program(node) {
const sourceCode = context.getSourceCode();
const source = sourceCode.text;
const flowHeader = FLOW_FORMAT_AND_TRANSPILE;
if (source.startsWith(flowHeader)) {
return;
}
const noFlowHeader = NO_FLOW_AND_NO_TRANSPILE;
if (source.replace(SHEBANG_RE, '').startsWith(noFlowHeader)) {
return;
}
let fix;
// The modules folder has a special license that shouldn't be blindly applied.
const comment = context.getSourceCode().getAllComments()[0];
if (
comment != null &&
comment.type === 'Block' &&
comment.loc.start.line === 1
) {
if (comment.value.includes('@flow')) {
fix = fixer => fixer.replaceText(comment, flowHeader.trim());
} else if (comment.value.includes('@noflow')) {
// TODO: replace the stuff after the docblock.
// It should be pretty obvious to the user, though.
fix = fixer => fixer.replaceText(comment, noFlowHeader.trim());
} else {
// Default to the @flow header.
fix = fixer => fixer.insertTextBeforeRange([0, 0], flowHeader);
}
} else if (!source.match(SHEBANG_RE)) {
fix = fixer => fixer.insertTextBeforeRange([0, 0], flowHeader);
}
context.report({
node,
message: 'Expected a license header',
fix,
});
},
};
};
module.exports.schema = [];
module.exports.FLOW_FORMAT_AND_TRANSPILE = FLOW_FORMAT_AND_TRANSPILE;
module.exports.NO_FLOW_AND_NO_TRANSPILE = NO_FLOW_AND_NO_TRANSPILE;