diff --git a/docs/rules/globals.md b/docs/rules/globals.md index 1efefba5ff..11c849b0b6 100644 --- a/docs/rules/globals.md +++ b/docs/rules/globals.md @@ -3,7 +3,7 @@ This rule defines global variables based on the environment the file is executed in. This rule never emits warnings on its own. It is meant to be used with ESLint's `no-undef`. -Do not use the Meteor environment. +This rule also marks collections defined through settings as globals. ## Rule Details @@ -22,6 +22,18 @@ Do not use the Meteor environment (`env: meteor` in `.eslintrc` or `$ eslint ./ } ``` +Collections defined in `.eslintrc`'s settings will be marked as globals as well. + +```js + +settings: { + meteor: { + collections: ['Posts', 'Items'] // all universal collections + } +} + +``` + ## Further Reading - http://eslint.org/docs/1.0.0/rules/no-undef diff --git a/lib/rules/globals.js b/lib/rules/globals.js index 2c91442a4e..b83a89cae8 100644 --- a/lib/rules/globals.js +++ b/lib/rules/globals.js @@ -49,6 +49,12 @@ module.exports = getMeta => context => { variables.push(generateGlobalVariable(globalVar, globalScope)) } }) + + // add Collections to globals + const {collections = []} = context.settings.meteor + collections.map(collection => { + variables.push(generateGlobalVariable(collection, globalScope)) + }) } } } diff --git a/tests/lib/rules/globals.js b/tests/lib/rules/globals.js index 3c337fda06..4b5cd1d76d 100644 --- a/tests/lib/rules/globals.js +++ b/tests/lib/rules/globals.js @@ -20,14 +20,11 @@ import {SERVER, PACKAGE, NON_METEOR} from '../../../dist/util/environment' const ruleTester = new RuleTester() ruleTester.run('globals', rule(() => ({env: SERVER})), { - valid: ['Session.set("hi", true)'], invalid: [] - }) ruleTester.run('globals', rule(() => ({env: PACKAGE})), { - valid: [ ` /* eslint-meteor-env client, server */ @@ -40,10 +37,18 @@ ruleTester.run('globals', rule(() => ({env: PACKAGE})), { ` /* eslint-meteor-env server */ Session.set("hi", true) - ` + `, + { + code: 'Users.find()', + settings: {meteor: {collections: ['Users']}} + }, + { + code: 'Users.find()', + settings: {meteor: {}} + } ], - invalid: [] + invalid: [] }) ruleTester.run('globals', rule(() => ({env: NON_METEOR})), {