Files
meteor/lib/rules/no-zero-timeout.js
Dominik Ferber bbf9f22109 refactor(): Determine env only once per file
Also fixes a bug in audit-argument-checks where it would show errors in non-meteor blocks.

BREAKING CHANGE: Envs specified through comments are now restricted to client and server.
2015-10-17 23:28:42 +02:00

50 lines
1.3 KiB
JavaScript

/**
* @fileoverview Prevent usage of Meteor.setTimeout with zero delay
* @author Dominik Ferber
*/
import {NON_METEOR} from '../util/environment'
import {getExecutors} from '../util'
import {isMeteorCall} from '../util/ast'
// -----------------------------------------------------------------------------
// Rule Definition
// -----------------------------------------------------------------------------
module.exports = getMeta => context => {
const {env} = getMeta(context)
// -------------------------------------------------------------------------
// Public
// -------------------------------------------------------------------------
if (env === NON_METEOR) {
return {}
}
return {
CallExpression: function (node) {
if (isMeteorCall(node, 'setTimeout')) {
const executors = getExecutors(env, context.getAncestors())
if (!executors.has('browser') && !executors.has('cordova')) {
return
}
if (node.arguments.length === 1) {
context.report(node, 'Implicit timeout of 0')
} else if (
node.arguments.length > 1 && node.arguments[1].type === 'Literal' && node.arguments[1].value === 0
) {
context.report(node, 'Timeout of 0. Use `Meteor.defer` instead')
}
}
}
}
}
module.exports.schema = []