eslint-plugin-ft-flow
Version:
Flowtype linting rules for ESLint by flow-typed
65 lines (57 loc) • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var looksLikeFlowFileAnnotation = function looksLikeFlowFileAnnotation(comment) {
return /^[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*@(?:no)?flow((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])|[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF])*/.test(comment);
};
var schema = [{
"enum": ['always', 'always-windows', 'never'],
type: 'string'
}];
var create = function create(context) {
var mode = context.options[0];
var never = mode === 'never';
var newline = mode === 'always-windows' ? '\r\n' : '\n';
return {
Program: function Program(node) {
var sourceCode = context.getSourceCode();
var potentialFlowFileAnnotation = sourceCode.getAllComments().find(function (comment) {
return looksLikeFlowFileAnnotation(comment.value);
});
if (potentialFlowFileAnnotation) {
var line = potentialFlowFileAnnotation.loc.end.line;
var nextLineIsEmpty = sourceCode.lines[line] === '';
if (!never && !nextLineIsEmpty) {
context.report({
fix: function fix(fixer) {
return fixer.insertTextAfter(potentialFlowFileAnnotation, newline);
},
message: 'Expected newline after flow annotation',
node: node
});
}
if (never && nextLineIsEmpty) {
context.report({
fix: function fix(fixer) {
var lineBreak = sourceCode.text[potentialFlowFileAnnotation.range[1]];
return fixer.replaceTextRange([potentialFlowFileAnnotation.range[1], potentialFlowFileAnnotation.range[1] + (lineBreak === '\r' ? 2 : 1)], '');
},
message: 'Expected no newline after flow annotation',
node: node
});
}
}
}
};
};
var _default = {
create: create,
meta: {
fixable: 'code',
schema: schema
}
};
exports["default"] = _default;
module.exports = exports.default;