From 0908b6c7b7dd01a96cc55535491887fdfca88416 Mon Sep 17 00:00:00 2001 From: Dominik Ferber Date: Wed, 21 Oct 2015 17:46:03 +0200 Subject: [PATCH] feat(globals): Enable setting collection globals through settings Settings can now be used to tell the linter about collections. It marks them as globals for now. In the future such variables will be linted in special ways by the rule collections, which does not exist yet. --- docs/rules/globals.md | 14 +++++++++++++- lib/rules/globals.js | 6 ++++++ tests/lib/rules/globals.js | 15 ++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) 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})), {