UNPKG

react-magnetic-di

Version:
45 lines (43 loc) 1.42 kB
"use strict"; var _require = require('../utils'), getDiIdentifier = _require.getDiIdentifier, isDiStatement = _require.isDiStatement; module.exports = { meta: { type: 'problem', docs: { description: 'Enforce di() call expression at the top of the block', category: 'Possible Errors', recommended: true }, // fixable: 'code', schema: [], messages: { wrongOrder: 'di() calls should be defined at the top of their scope ' + 'to avoid partial replacements and variables clashing' } }, create: function create(context) { var diIdentifier = null; return { ImportDeclaration: function ImportDeclaration(node) { if (!diIdentifier) diIdentifier = getDiIdentifier(node); }, BlockStatement: function BlockStatement(node) { if (!diIdentifier) return; (node.body || []).forEach(function (statement, i) { if (!isDiStatement(statement, diIdentifier) || i === 0) return; // collect all nodes before the current one that are not directives var prevNodes = node.body.filter(function (s, index) { return index < i && !s.directive; }); if (prevNodes.length && !isDiStatement(prevNodes[0], diIdentifier)) { context.report({ node: statement, messageId: 'wrongOrder' }); } }); } }; } };