UNPKG

eslint-plugin-roku

Version:

The ESLint custom plugin with rules and parser for .brs files

86 lines (85 loc) 4.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const os_1 = require("os"); const context_globals_1 = require("./context-globals"); class BaseUninitializedRule { meta() { return { docs: { category: 'Possible Errors', description: this.description(), recommended: true, }, messages: { NOT_DECLARED: this.message(), }, schema: [], }; } create() { const hasValidParent = this.hasValidParent; return (context) => { return { 'Identifier:exit'(node) { if (!context_globals_1.globals.find(g => g.toLowerCase() === node.name.toLowerCase()) && node.parent && hasValidParent(node)) { if (!context_globals_1.findAllDeclaredVariables(context.getScope()).find(v => v === node.name) && !context_globals_1.isInForEach(node, node.name) && !context_globals_1.isInForLoop(node, node.name) && !context_globals_1.getFileGlobals(context).find(c => c.toLowerCase() === node.name.toLowerCase())) { context.report({ data: { name: node.name }, loc: node.loc, messageId: 'NOT_DECLARED', node, suggest: [{ desc: 'Add to globals comment if this is a sub/function from a dependency.', fix: (fixer) => { const existingComments = context_globals_1.getProgramGlobalComments(context); if (existingComments.length > 0) { if (context_globals_1.getGlobalsFromComment(existingComments[0]).length > 0) { return fixer.insertTextAfter(existingComments[0], ', ' + node.name); } else { return fixer.insertTextAfter(existingComments[0], ' ' + node.name); } } else { return fixer.insertTextBefore(context.getAncestors()[0], `' globals ${node.name}${os_1.EOL}`); } } }] }); } } }, AssignmentExpression(node) { if (node.operator.type === 'EQUAL' && node.left.type === 'Identifier') { context_globals_1.addDeclaredVariable(node.left.name, context); } }, Parameter(node) { context_globals_1.addDeclaredVariable(node.name.name, context); }, Program(node) { node.body .filter(b => b.type === 'FunctionDeclaration' || b.type === 'SubDeclaration') .map(f => f.id.name) .forEach(f => { context_globals_1.addDeclaredVariable(f, context); }); }, FunctionExpression(node) { context_globals_1.addParamsToScope(node, context); }, SubExpression(node) { context_globals_1.addParamsToScope(node, context); }, }; }; } } exports.default = BaseUninitializedRule;