This repository has been archived by the owner on Oct 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Core API for Session (session) | ||
|
||
Prevent misusage of [Session](http://docs.meteor.com/#/full/session). | ||
|
||
|
||
## Rule Details | ||
|
||
This rule aims to prevent errors when using Publications and Subscriptions. It verifies `Session` is used in the correct environments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
Session.set('foo') | ||
|
||
``` | ||
|
||
```js | ||
|
||
Session.setDefault('foo') | ||
|
||
``` | ||
|
||
|
||
```js | ||
|
||
Session.set('foo', true, 'bar') | ||
|
||
``` | ||
|
||
|
||
```js | ||
|
||
if (Meteor.isServer) { | ||
Session.set('foo') | ||
} | ||
|
||
``` | ||
|
||
|
||
```js | ||
|
||
Session.get('foo', true) | ||
|
||
``` | ||
|
||
|
||
```js | ||
|
||
Session.get() | ||
|
||
``` | ||
|
||
|
||
```js | ||
|
||
Session.equals('foo') | ||
|
||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
|
||
Session.set('foo', true) | ||
|
||
``` | ||
|
||
```js | ||
|
||
Session.setDefault('foo', true) | ||
|
||
``` | ||
|
||
```js | ||
|
||
Session.get('foo') | ||
|
||
``` | ||
|
||
```js | ||
|
||
Session.equals('foo', true) | ||
|
||
``` | ||
|
||
### Options | ||
|
||
#### no-equal | ||
|
||
By default this rule does not warn when trying to call `Session.equal`. Usually a call to `Session.equals` is meant instead. | ||
To warn when using `Session.equal`, configure the rule as | ||
|
||
``` | ||
session: [2, "no-equal"] | ||
``` | ||
|
||
With this configuration, the rule will warn on this pattern: | ||
|
||
```js | ||
|
||
Session.equal('foo', 'bar') | ||
|
||
``` | ||
|
||
## When Not To Use It | ||
|
||
Disable this rule if you are using [no-session](./no-session.md). | ||
|
||
## Further Reading | ||
|
||
- http://docs.meteor.com/#/full/session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* @fileoverview Core API for Session | ||
* @author Dominik Ferber | ||
* @copyright 2015 Dominik Ferber. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Rule Definition | ||
// ----------------------------------------------------------------------------- | ||
|
||
import {NON_METEOR, SERVER} from '../util/environment' | ||
import {getExecutors} from '../util' | ||
import {getPropertyName} from '../util/ast' | ||
|
||
module.exports = getMeta => context => { | ||
const {env} = getMeta(context) | ||
|
||
// --------------------------------------------------------------------------- | ||
// Public | ||
// --------------------------------------------------------------------------- | ||
|
||
if (env === NON_METEOR || env === SERVER) { | ||
return {} | ||
} | ||
|
||
return { | ||
|
||
CallExpression: function (node) { | ||
if ( | ||
node.callee.type === 'MemberExpression' && | ||
node.callee.object.type === 'Identifier' && | ||
node.callee.object.name === 'Session' | ||
) { | ||
const executors = getExecutors(env, context.getAncestors()) | ||
if (executors.size === 0) { | ||
return | ||
} | ||
if (executors.has('server')) { | ||
context.report(node, 'Allowed on client only') | ||
return | ||
} | ||
|
||
switch (getPropertyName(node.callee.property)) { | ||
case 'set': | ||
case 'setDefault': | ||
case 'equals': | ||
if (node.arguments.length !== 2) { | ||
context.report(node, 'Expected two arguments') | ||
} | ||
break | ||
case 'get': | ||
if (node.arguments.length !== 1) { | ||
context.report(node, 'Expected one argument') | ||
} | ||
break | ||
case 'equal': | ||
if (context.options.length > 0 && context.options[0] === 'no-equal') { | ||
context.report(node.callee.property, 'Did you mean "Session.equals" instead?') | ||
} | ||
break | ||
} | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
module.exports.schema = [ | ||
{ | ||
enum: ['equal', 'no-equal'] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/** | ||
* @fileoverview Core API for Session | ||
* @author Dominik Ferber | ||
* @copyright 2015 Dominik Ferber. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Requirements | ||
// ----------------------------------------------------------------------------- | ||
|
||
import {NON_METEOR, UNIVERSAL, CLIENT, SERVER} from '../../../dist/util/environment' | ||
const rule = require('../../../dist/rules/session') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const commonValidCode = [ | ||
'x()', | ||
'Session.set("foo", true)', | ||
'Session.setDefault("foo", true)', | ||
'Session.get("foo")', | ||
'Session.equals("foo", true)', | ||
{ | ||
code: `Session.equal('foo', 'bar')`, | ||
options: ['equal'] | ||
}, | ||
` | ||
if (Meteor.isServer) { | ||
Session.set('foo') | ||
} | ||
` | ||
] | ||
|
||
const commonInvalidCode = [ | ||
{ | ||
code: `Session.set('foo')`, | ||
errors: [{message: 'Expected two arguments', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: `Session.setDefault('foo')`, | ||
errors: [{message: 'Expected two arguments', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: `Session.set('foo', true, 'bar')`, | ||
errors: [{message: 'Expected two arguments', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: ` | ||
Session.get('foo', true) | ||
`, | ||
errors: [{message: 'Expected one argument', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: `Session.get()`, | ||
errors: [{message: 'Expected one argument', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: `Session.equals('foo')`, | ||
errors: [{message: 'Expected two arguments', type: 'CallExpression'}] | ||
}, | ||
{ | ||
code: `Session.equal('foo', 'bar')`, | ||
options: ['no-equal'], | ||
errors: [{message: 'Did you mean "Session.equals" instead?', type: 'Identifier'}] | ||
} | ||
] | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Tests | ||
// ----------------------------------------------------------------------------- | ||
|
||
const ruleTester = new RuleTester() | ||
|
||
ruleTester.run('session', rule(() => ({env: CLIENT})), { | ||
valid: [ | ||
...commonValidCode | ||
], | ||
|
||
invalid: [ | ||
...commonInvalidCode | ||
] | ||
}) | ||
|
||
ruleTester.run('session', rule(() => ({env: UNIVERSAL})), { | ||
valid: [ | ||
` | ||
if (Meteor.isClient) { | ||
Session.set('foo', true) | ||
} | ||
`, | ||
` | ||
if (Meteor.isCordova) { | ||
Session.set('foo', true) | ||
} | ||
` | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `Session.set('foo', true)`, | ||
errors: [{message: 'Allowed on client only', type: 'CallExpression'}] | ||
} | ||
] | ||
}) | ||
|
||
ruleTester.run('session', rule(() => ({env: SERVER})), { | ||
valid: [ | ||
...commonValidCode, | ||
...commonInvalidCode | ||
], | ||
|
||
invalid: [] | ||
}) | ||
|
||
ruleTester.run('session', rule(() => ({env: NON_METEOR})), { | ||
valid: [ | ||
...commonValidCode, | ||
...commonInvalidCode | ||
], | ||
|
||
invalid: [] | ||
}) |