Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upNo useless expressions #690
Comments
dcousens
added
feature request
enhancement
labels
Nov 18, 2016
This comment has been minimized.
This comment has been minimized.
|
I would open an issue on ESLint about adding a rule for this, but I don't think it's possible for them to reliably detect that |
feross
closed this
Nov 23, 2016
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Nice find, going to re-open until we've released v9. |
feross
reopened this
Nov 24, 2016
This comment has been minimized.
This comment has been minimized.
|
This rule has a lot of false positives with silly testing frameworks that do:
People need to stop being so clever. All this fancy stuff just causes more problems than it solves. How is this: expect(err.message).to.equal('You had one job')better than this? assert.equal(err.message, 'You had one job')Grrr... |
This comment has been minimized.
This comment has been minimized.
|
Oh dear, does |
This comment has been minimized.
This comment has been minimized.
|
And guess what happens when you write |
This comment has been minimized.
This comment has been minimized.
|
We could add this rule and encourage users to move away from that horrible api though? |
This comment has been minimized.
This comment has been minimized.
|
Yeah, maybe we should just ship this. I don't want to scare too many people off of |
This comment has been minimized.
This comment has been minimized.
|
What were your final thoughts @feross ? |
This comment has been minimized.
This comment has been minimized.
|
I think we should ship this in the beta for v9 and see how it goes. |
dcousens
added this to the
standard v9 milestone
Jan 24, 2017
This comment has been minimized.
This comment has been minimized.
|
@feross, I added this to the v9 milestone |
This comment has been minimized.
This comment has been minimized.
|
Question: This rule has two options that relax parts of the rule. Should we enable them? Without trySelectWire(false) || trySelectWire(true)Without isWebSeed ? piece.cancelRemaining(reservation) : piece.cancel(reservation)More examples of both: !valid ? reject(new HARError(validate.errors)) : resolve(data)opts.alwaysOnTop ? emitBlur() : hideWindow()oldRelease && oldRelease()I would argue that all these should be |
This comment has been minimized.
This comment has been minimized.
|
My vote is to enable, I use both of those patterns often. If I had to pick only one to enable I'd pick the ternary. I've flip-flopped on preference for |
This comment has been minimized.
This comment has been minimized.
|
With no exceptions:
With both exceptions:
Of the 14 failures:
Here are the bugs: if (Array.isArray(objects)) {
return objects.some(function (object) { object.type && object.type !== type })
}if (/use strict/.test(line)) line += '\n' + preamble
else line + preamble + '\n' + lineself.resetTimer |
This comment has been minimized.
This comment has been minimized.
|
@feross anything interesting in the 12 that only passed with both exceptions enabled? |
This comment has been minimized.
This comment has been minimized.
|
@timoxley No -- the 3 examples in my previous comment were representative (#690 (comment)). |
This comment has been minimized.
This comment has been minimized.
|
@feross i.e. no bugs getting through due to relaxed rule is good |
This comment has been minimized.
This comment has been minimized.
|
For users of Chai, here's an explanation of why Chai's behavior is unsafe and an anti-pattern: Beware of libraries that assert on property access. Fortunately, there is a workaround that doesn't require giving up Chai. Install In short, your tests will go from: expect(true).to.be.true
foo.should.be.okTo this: expect(true).to.be.true()
foo.should.be.ok()And just like that, your tests will be much more robust! (Many thanks to @erinspice for this tip.) If you cannot change your tests now, feel free to continue using If you wish to upgrade and temporarily disable this rule, add a comment to the top of each test file: /* eslint-disable no-unused-expressions */ |
This comment has been minimized.
This comment has been minimized.
if (Array.isArray(objects)) {
return objects.some(function (object) { object.type && object.type !== type })
}Haha, this gets me way too often. Looking forward to it. |
feross
added a commit
to standard/eslint-config-standard
that referenced
this issue
Feb 9, 2017
This comment has been minimized.
This comment has been minimized.
|
This will be part of standard v9. I enabled both exceptions (short circuiting and ternary expressions), so it should only catch bugs and unsafe test frameworks. |
feross
closed this
Feb 9, 2017
feross
added a commit
to standard/eslint-config-standard
that referenced
this issue
Feb 9, 2017
This comment has been minimized.
This comment has been minimized.
wmhilton
commented
Feb 10, 2017
|
I love that one of the main decision factors for standard style is preventing buggy code. <3 |
This comment has been minimized.
This comment has been minimized.
bcoe
commented
May 2, 2017
•
|
this rule breaks one of the common approaches that folks use when invoking yargs: yargs.argvthis would require that I need to update 100s of tests to continue using standard: /Users/benjamincoe/bcoe/yargs/test/command.js:33:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:78:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:104:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:129:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:509:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:527:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:549:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:586:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:602:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:619:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:713:5: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:719:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:729:5: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:792:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:835:5: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:850:7: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:873:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:884:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:896:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:907:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:918:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:930:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:944:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:955:9: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:972:13: Expected an assignment or function call and instead saw an expression.
/Users/benjamincoe/bcoe/yargs/test/command.js:974:9: Expected an assignment or function call and instead saw an expression.I'm less concerned about this, and more concerned that I'd need to update all of the existing documentation -- to point folks towards code that plays nice with standard. I'm a huge advocate of standard, and have been using it for everything I write for the past couple years, but this rule specifically is making me a bit gun shy about upgrading. |
This comment has been minimized.
This comment has been minimized.
|
@bcoe, but, you understand that |
This comment has been minimized.
This comment has been minimized.
bcoe
commented
May 2, 2017
•
|
@dcousens #!/usr/bin/env node
var argv = require('yargs')
.count('verbose')
.alias('v', 'verbose')
.argvJust had |
This comment has been minimized.
This comment has been minimized.
|
If assigned as above, there is no issue. For |
This comment has been minimized.
This comment has been minimized.
bcoe
commented
May 2, 2017
|
@dcousens this comes up a lot when you're writing a CLI using yargs, e.g.: require('yargs') // eslint-disable-line
.usage('$0 <command>')
.commandDir('../lib/commands')
.help()
.alias('h', 'help')
.version()
.strict()
.demandCommand(1)
.argvor, require('yargs') // eslint-disable-line
.command(['serve', '*'], 'start the pkg-info-cache server', () => {}, (argv) => {
const logger = bole(serviceName)
bootstrapLogger()
logger.info(argv)
server(argv)
})
.option('port', {
describe: 'port to run server on',
default: process.env.PORT || 9999
})
.option('host', {
describe: 'host to bind on',
default: '0.0.0.0'
})
.argvbecause yargs handles the flow control; one of the first things I do now when creating a bin in a project is to cargo cult the |
This comment has been minimized.
This comment has been minimized.
knksmith57
commented
Sep 15, 2017
|
@bcoe fwiw just got bit hard by this; my google foo wasn't strong enough to find this thread (and not for lack of trying). resorting to also a huge proponent of standard; literally forced it on every project I've ever had influence over. have to say tho, requiring all that ^^ just to prevent a very idiomatic pattern from failing the build w/out becoming intrusive on teammates leaves a bad taste in my mouth. for all the time and debate standard has saved (and aims to save!) devs, this one was a big step back. hope y'all reconsider your position on this one. /cc @feross |
This comment has been minimized.
This comment has been minimized.
|
As of Yargs 9.0.0 this is no longer a problem Instead of using the getter require('yargs')
.command('hello <name>', 'say hello to <name>', {}, yargs => {
console.log(`hello, ${yargs.name}!`)
})
.demandCommand(1)
.help()
- .argv
+ .parse()/cc @knksmith57 |
kasbah
added a commit
to mcous/gerber-plotter
that referenced
this issue
Apr 7, 2018
Nightpanda
added a commit
to Nightpanda/task-time-manager
that referenced
this issue
May 4, 2018
feross
added
enhancement
and removed
enhancement
labels
May 10, 2018
This comment has been minimized.
This comment has been minimized.
searls
commented
Jul 12, 2018
This is the system working, I think |
dcousens commentedNov 18, 2016
•
edited
I recently had something bite me in some of my own code, and I wonder if we can't avoid this happening:
That should have been
o.value = 3, but, alas, something went wrong that day.Is there an existing eslint rule for this?
Any reason not to have it enabled?