UNPKG

1.61 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.schema = undefined;
7
8var _lodash = require('lodash');
9
10var _lodash2 = _interopRequireDefault(_lodash);
11
12var _utilities = require('./../utilities');
13
14function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
16var looksLikeFlowFileAnnotation = function looksLikeFlowFileAnnotation(comment) {
17 return (/@(?:no)?flow/i.test(comment)
18 );
19};
20
21var schema = exports.schema = [{
22 enum: ['always']
23}];
24
25exports.default = function (context) {
26 var checkThisFile = !_lodash2.default.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || (0, _utilities.isFlowFile)(context);
27
28 if (!checkThisFile) {
29 return {};
30 }
31
32 var always = context.options[0] === 'always';
33
34 return {
35 Program: function Program(node) {
36 var firstToken = node.tokens[0];
37
38 var potentialFlowFileAnnotation = _lodash2.default.find(context.getAllComments(), function (comment) {
39 return looksLikeFlowFileAnnotation(comment.value);
40 });
41
42 if (potentialFlowFileAnnotation) {
43 if (firstToken && firstToken.start < potentialFlowFileAnnotation.start) {
44 context.report(potentialFlowFileAnnotation, 'Flow file annotation not at the top of the file.');
45 }
46
47 if (!(0, _utilities.isFlowFileAnnotation)(potentialFlowFileAnnotation.value)) {
48 context.report(potentialFlowFileAnnotation, 'Malformed flow file annotation.');
49 }
50 } else if (always) {
51 context.report(node, 'Flow file annotation is missing.');
52 }
53 }
54 };
55};
\No newline at end of file