Skip to content

Commit

Permalink
Inline rule in index
Browse files Browse the repository at this point in the history
  • Loading branch information
Glinkis committed Jul 9, 2021
1 parent d122864 commit 7747e01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 57 deletions.
56 changes: 55 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
// @ts-check
"use strict"

module.exports = {
rules: {
"call-in-function": require("./lib/rules/call-in-function")
/**
* @type {import('eslint').Rule.RuleModule}
*/
"call-in-function": {
create(context) {
const [options] = context.options

/** @type {string[]} */
const functions = options.functions || []
/** @type {string[]} */
const methods = options.methods || []

return {
CallExpression(node) {
if (node.type !== "CallExpression") return

const { callee } = node

const descriptor = { node, message: "" }

switch (callee.type) {
case "Identifier": {
if (!functions.includes(callee.name)) return
descriptor.message = `Function '${callee.name}' is must be called inside a function.`
break
}
case "MemberExpression": {
const { property } = callee
if (property.type !== "Identifier") return
if (!methods.includes(property.name)) return
descriptor.message = `Method '${property.name}' is must be called inside a function.`
break
}
default:
return
}

let parent = node.parent

// Look for a function declaration anywhere in the tree.
while (parent.parent) {
if (parent.type.match(/Function/)) {
return
}
parent = parent.parent
}

context.report(descriptor)
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ const parserOptions = {
}

const ruleTester = new RuleTester({ parserOptions })
const rule = require("./lib/rules/call-in-function")
const rules = require("./").rules

ruleTester.run("call-in-function", rule, {
ruleTester.run("call-in-function", rules["call-in-function"], {
valid: [
{
code: `
Expand Down
54 changes: 0 additions & 54 deletions lib/rules/call-in-function.js

This file was deleted.

0 comments on commit 7747e01

Please sign in to comment.