From ff5af1f7ff714ceb2ca88cc236e38b695741d24f Mon Sep 17 00:00:00 2001 From: Dominik Ferber Date: Sun, 18 Oct 2015 19:53:48 +0200 Subject: [PATCH] fix(rules): Ignore locus checks with unkown expressions Instead of aborting when finding an if-statement with unkown statements in the ancestors of any node, ignore that if-statement. This way the linter will cover more code. Assumes the code never mixes locus checks with other expressions. --- README.md | 2 ++ .../executors/filterExecutorsByAncestors.js | 4 ---- tests/lib/rules/audit-argument-checks.js | 2 +- tests/lib/rules/pubsub.js | 2 +- .../executors/filterExecutorsByAncestors.js | 23 ++++--------------- 5 files changed, 9 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 5c5e082481..33dce34b7f 100755 --- a/README.md +++ b/README.md @@ -99,6 +99,8 @@ Executors (client, browser, server, cordova) are detected by looking at the loca if ((Meteor.isClient || Meteor.isCordova) && !Meteor.isServer) { .. } ``` +If a test in a locus check is paired with any other expression that specific locus check will be ignored. + ### Package files ESLint-plugin-Meteor is not able to detect where files in inlined packages are going to be executed. It needs hints to work around this. See [this guide](docs/SETUP_METEOR_PROJECT.md#packages) for details. diff --git a/lib/util/executors/filterExecutorsByAncestors.js b/lib/util/executors/filterExecutorsByAncestors.js index 43244d018a..3c4e329ffa 100644 --- a/lib/util/executors/filterExecutorsByAncestors.js +++ b/lib/util/executors/filterExecutorsByAncestors.js @@ -21,10 +21,6 @@ export default function filterExecutorsByAncestors (originalExecutors, ancestors } else { invariant(false, 'Block is neither consequent nor alternate of parent') } - } else { - - // can not determine executors, because of unresolvable if-statement - return new Set() } } } diff --git a/tests/lib/rules/audit-argument-checks.js b/tests/lib/rules/audit-argument-checks.js index dab294f59c..5202af71a9 100644 --- a/tests/lib/rules/audit-argument-checks.js +++ b/tests/lib/rules/audit-argument-checks.js @@ -97,7 +97,7 @@ ruleTester.run('audit-argument-checks', rule(() => ({env: SERVER})), { Meteor.methods({ foo () {}, foo2 (bar) { - if (true) { + if (!Meteor.isServer) { check(bar, Meteor.any) } } diff --git a/tests/lib/rules/pubsub.js b/tests/lib/rules/pubsub.js index c0c8e365da..a57631d63d 100644 --- a/tests/lib/rules/pubsub.js +++ b/tests/lib/rules/pubsub.js @@ -119,7 +119,7 @@ ruleTester.run('pubsub - universal', rule(() => ({env: UNIVERSAL})), { } `, ` - if (Meteor.Client) { + if (Meteor.isClient) { if (Meteor.isServer) { Meteor.subscribe() // not checked because unreachable } diff --git a/tests/lib/util/executors/filterExecutorsByAncestors.js b/tests/lib/util/executors/filterExecutorsByAncestors.js index c6663b3c1f..f55a704196 100644 --- a/tests/lib/util/executors/filterExecutorsByAncestors.js +++ b/tests/lib/util/executors/filterExecutorsByAncestors.js @@ -158,33 +158,20 @@ describe('filterExecutorsByAncestors', function () { assert.ok(result.has('server')) }) - it('returns no executors when an unresolvable IfStatement is in ancestors', function () { + it('ignores unresolvable IfStatements is in ancestors', function () { const consequent = {type: 'BlockStatement'} - const ifConsequent = { - test: { - type: 'MemberExpression', - object: { - type: 'Identifier', - name: 'Meteor' - }, - property: { - type: 'Identifier', - name: 'isClient' - } - }, - consequent: consequent - } const result = filterExecutorsByAncestors(new Set(['browser', 'server']), [ {type: 'Program'}, { type: 'IfStatement', test: {type: 'Identifier'}, - consequent: ifConsequent + consequent: consequent }, - ifConsequent, consequent ]) - assert.equal(result.size, 0) + assert.equal(result.size, 2) + assert.ok(result.has('browser')) + assert.ok(result.has('server')) }) })