xojs / xo Goto Github PK
View Code? Open in Web Editor NEW❤️ JavaScript/TypeScript linter (ESLint wrapper) with great defaults
License: MIT License
❤️ JavaScript/TypeScript linter (ESLint wrapper) with great defaults
License: MIT License
i have a js/ folder where i want to lint with
"esnext": true,
"envs": [
"browser"
]
and then i have my gulpfile folder where i want to lint with
"esnext": false,
"envs": []
this works with .jshint files because i can have multiple of them in the different folders. but with xo the configuration is stored in the package.json which i tried duplicating with different rules into the different folders but it didn't work.
so i'm wondering if theres a solution already? seems like a common case.
Does an appropriate formatter for the rules defined in xo
already exist ?
Something like standard-format for standard.
Config:
"xo": {
"overrides": [
{
"files": "src/**/*.spec.js",
"rules": {
"no-unused-expressions": 0
}
}
],
}
foo.spec.js
:
...
expect(checkbox.checked).to.be.false
Lint:
error Expected an assignment or function call and instead saw an expression no-unused-expressions
/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/cli.js:76
throw err;
^
TypeError: Cannot read property 'type' of undefined
at /usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/rules/quote-props.js:116:20
at Array.forEach (native)
at checkConsistency (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/rules/quote-props.js:112:25)
at EventEmitter.ObjectExpression (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/rules/quote-props.js:156:17)
at EventEmitter.emit (events.js:129:20)
at Controller.controller.traverse.enter (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/eslint.js:824:25)
at Controller.__execute (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/node_modules/estraverse/estraverse.js:397:31)
at Controller.traverse (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/node_modules/estraverse/estraverse.js:495:28)
at EventEmitter.module.exports.api.verify (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/eslint.js:817:24)
at processText (/usr/local/Cellar/nvm/0.25.4/versions/node/v0.12.7/lib/node_modules/xo/node_modules/eslint/lib/cli-engine.js:199:27)
As you might have figured out from the paths, I'm using node v0.12.7
(had to make a switch from iojs) and I'm using OS X (Yosemite).
Removing said options will successfully lint, or better said unsuccessfully since it errors on bunch of es2015 syntax.
In jshint and eslint you can configure exported variables with inline comments. in the XO linter the exported configuration that is described here: http://eslint.org/docs/rules/no-unused-vars.html doesnt seem to work. the /* globals */ configuration works fine however so it doesnt seem to be an issue of inline configurations in general.
/* exported Model */
var Model = 13;
linting it from the terminal with xo file.js thorws the error:
3:5 error "Model" is defined but never used no-unused-vars
i tried to lint the same file with the current eslint linter. that one worked fine. it threw an error without the /* exported Model */, and worked when i plugged it back in.
I use spaces, so runnign xo --space
works fine.
However, when trying to throw this into my package.json
, this doesn't seem to accept args like ESLINT would:
"xo": {
"envs": ["node", "mocha"],
"rules": {
"indent": [2, 2, {"SwitchCase": 1}],
}
}
This isn't applying two-space indentation.
Am I missing something?
I was just wondering the best way to integrate ESLint plugins to XO.
Let's say I want to use eslint-plugin-no-use-extend-native
along with XO, what should I do? Does XO simply invokes ESLint and things work as expected or is there any trick that I need to do?
This file passes:
https://github.com/sindresorhus/ava/blob/master/test/fixture/async-await.js
But if I copy it to the root of the project it fails:
Line 5:
test('async function', async function (t) {
If I take out that test, we get Parsing error: Unexpected identifier
for the next test on this line:
test('arrow async function', async t => {
I didn't see any special eslint
/xo
settings in this project or ava
, such as an automatic ignore for /fixtures
.
This might be related to avajs/ava#92, or visa versa.
Would love to be able to turn off the requirement of semicolons. (I like all the other rules, including tabs, so I can't use feross/standard)
Is this planned/would a PR be accepted?
It is relatively new:
See: eslint/eslint#1158
joakimbeng/unistyle@4dfa6bc#commitcomment-13087626
Right now you have to do directory/**
to ignore all files in a directory, but would be nice, and probably more natural, if you could ignore a directroy directly by using directroy
.
package json xo enviroment set, but not working
something this line broken #L37-L49
Using latest xo (master branch), I'm unable to customize babel/object-curly-spacing
rule via shareable config.
package.json:
{
"extends": "my-config"
}
my-config:
{
"rules": {
"babel/object-curly-spacing": 0
}
}
file.js:
{ key: 'value' }
Given these files, xo throws error:
If I override babel/object-curly-spacing
in package.json, it works as expected and I no longer get errors:
{
"rules": {
"babel/object-curly-spacing": 0
}
}
Related: https://github.com/sindresorhus/xo/blob/master/options-manager.js#L98
There is a $60.00 open bounty on this issue. Add more on Issuehunt.
- Checkout the Issuehunt explorer to discover more funded issues.
- Need some help from other developers? Add your repositories on Issuehunt to raise funds.
If I have a JSX file importing a component from another file:
import Menu from 'app/components/menu';
I seem to get the following error:
"Menu" is defined but never used (no-unused-vars)
However I use it in my JSX:
render() {
return (
<div>
{this.state.loggedIn ? <Menu/> : null}
{this.props.children}
</div>
);
}
Could be adapted from this one: https://github.com/feross/standard/pull/345/files
Need help to adapt and ensure it's working from someone using WebStorm.
The first one that springs to mind is babel-eslint
(for those ES2015+ features). Maybe something like this?
"xo": {
"parser": "babel-eslint"
}
I get TypeError: report._getFormatter is not a function
when trying to use the stdin
option. I guess it's because you aren't setting it here.
The consistent-this
rule will throw in this case:
obj.foo = function () {
var self = this;
return through.obj(function () {
var stream = this; // can't use self here or we'll shadow our previous variable
});
};
Looks like a pretty frequent pattern we encounter in node code.
We can probably work around the rule in a couple ways, but sometime it won't work to keep context or bind arguments.
I'm checking parity between xo and eslint warnings and I get 58 warning / errors with xo and 1 with eslint. Obviously, most of these are because xo has its own rules. I'd like to override all of these with my own rules from my shareable config, but can't seem to get that to happen, even individual rules are not being overridden when I use my custom shareable config.
//package.json
"xo": {
"envs": [
"node",
"mocha"
],
"extend": "myConfig",
"rules": {}
}
can I nullify the rules, like above?
Thanks!
Not trying to shamelessly plug my own stuff, but would be nice to have fly-xo included in the plugins section since there is one for fly-ava in AVA's.
If not, no problem 👍
The problem I have is quite simple: my package.json
contains a XO configuration, but when eslintConfig
is also added to package.json
, the last one take precedence over XO.
{
"name": "my-awesome-project",
"eslintConfig": {
"extends": ["xo", "xo-react"]
},
"xo": {
"esnext": true
}
}
This configuration will load the XO defaults options, but the esnext
option will be ignored. In order to fix this, https://github.com/sindresorhus/xo/blob/master/index.js#L66 needs to allow the extends
option to also include the eslintConfig
.
Or, even better, I want to be able to specify the extends
option straight into XO, like this:
{
"name": "my-awesome-project",
"xo": {
"esnext": true,
"extends": ["xo-react"]
}
}
which is equivalent of
{
"name": "my-awesome-project",
"xo": {
"extends": ["esnext", "xo-react"]
}
}
We use TypeScript instead of ECMA 6 and mainly rely on tslint: https://github.com/palantir/tslint
However we have some .js files and it can be cool to have one tool to take care of consistency.
Currently no-unused-vars
doesn't allow function arguments that isn't used. This is a problem when used for example with express which checks fn.length
to determine what to do.
The following is considered an error:
function (err, req, res, next) {
res.status(500).send('An error occurred')
}
That's because it doesn't use the next
argument. But if I would remove it express would no longer consider this an error handler.
While I do consider it bad to depend on fn.length
the reality is that arguable to most used node.js library does it.
no-unused-var
can be specified to allow unused arguments, without affecting the other functionallity:
[ 2, { "vars": "all", "args": "none" } ]
Fix is awesome but it doesn't add e.g. trailing commas or fix indentation. I presume these need to be fixed in the eslint plugins? Any hints?
If you set the space
property without setting the rules
property in the xo
configuration in the package.json, the following error is thrown:
opts._config.rules.indent = [2, spaces, {SwitchCase: 1}];
TypeError: Cannot set property 'indent' of undefined
at handleOpts (node_modules/xo/index.js:58:29)
This occurred with the following config:
"xo": {
"esnext": true,
"envs": ["browser", "mocha"],
"space": 2
}
Adjusting the configuration to include an empty rules object fixes it:
"xo": {
"esnext": true,
"envs": ["browser", "mocha"],
"space": 2,
"rules": {}
}
Hello,
Sorry for this (maybe stupid) question but if I want to use react plugin did I need to npm install eslint-config-xo-react xo
? Or just npm install xo
is sufficient ?
Thanks :)
I'm working on upgrading to ESLint 2. I've already upgraded eslint-config-xo with required changes and some new rules.
I've hit two issues. Tests don't pass and not exactly sure why. And it seems to be a problem with babel-eslint
as async functions no longer parse (Just try running latest master on boxen-cli). Would appreciate some help getting this sorted out as I'm out of time and won't be able to get to this right now.
Stack trace for the failing tests:
TypeError: /Users/sindresorhus/dev/xo/test/options-manager.js: Cannot read property 'start' of undefined
at /Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/whitespace.js:29:19
at Whitespace._findToken (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/whitespace.js:99:30)
at Whitespace.getNewlinesBefore (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/whitespace.js:28:22)
at CodeGenerator.printComment (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:321:34)
at CodeGenerator.printComments (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:377:12)
at CodeGenerator.printLeadingComments (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:231:10)
at CodeGenerator.print (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:81:10)
at CodeGenerator.printJoin (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:193:12)
at CodeGenerator.printList (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:258:17)
at CodeGenerator.ObjectExpression (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/generators/types.js:48:10)
at CodeGenerator._print (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:155:17)
at CodeGenerator.print (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:91:10)
at CodeGenerator.printJoin (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:193:12)
at CodeGenerator.printList (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:258:17)
at CodeGenerator.CallExpression (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/generators/expressions.js:142:8)
at CodeGenerator._print (/Users/sindresorhus/dev/xo/node_modules/babel-generator/lib/printer.js:155:17)
From previous event:
at Api.run (/Users/sindresorhus/dev/xo/node_modules/ava/api.js:148:4)
at Object.<anonymous> (/Users/sindresorhus/dev/xo/node_modules/ava/cli.js:115:5)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
The rule radix
requires that parseInt
has the second parameter, a radix. It is intended to disallow octal notation, but has the side effect of not allowing auto base-10/base-16 determination in the event the second parameter is absent.
Could we make it a warning?
For cases like sindresorhus/load-grunt-tasks@e758ed4
It's easy to miss things while writing up example code and it may be worth investigating how we could lint those.
var obj = {
get something() {
}
};
Gets me this cryptic error:
19:3 error Expected indentation of 1 characters but found 0 indent
this is my configuration
"xo" : {
"space" : 4,
"overrides" : {
"test" : "test/*.spec.js",
"globals" : [ "it" , "describe" ]
}
}
and I am getting
test/test.spec.js
8:1 error "describe" is not defined no-undef
9:5 error "it" is not defined no-undef
To avoid all those pesky babel and electron build dirs.
While not directly related to xo, is there a way to open all files with problems found by xo,
xo app/**/*.js | subl *
… or something like that?
standard has a RULES.md as an overview of what's enforced. Can xo
have something similar? I'd like to know how xo differs from, say, semistandard.
In-Editor linter plugins like SublimeLinter-contrib-eslint_d require a .eslintrc
to be present in the project directory to work. Would it be possible for xo to create one, maybe through a postinstall
npm script?
So I just made a comment here about consistent-this hindering actual functionality - something code styling should do conservatively. Here is something else pretty relevant.
Ternary operations can be performed safely and visibly if nested. As well, there now becomes an issue of branching.
Take the following code:
result.file = opts.dash ? '-' : platform === 'linux' ?
'/dev/stdin' : '/proc/self/fd/0';
or even this
result.file = opts.dash
? '-'
: platform === 'linux'
? '/proc/self/fd/0'
: '/dev/stdin';
But this results in the following error:
index.js
27:17 error Do not nest ternary expressions no-nested-ternary
Seriously? Here is the alternative.
var inputPath = platform === 'linux' ? '/dev/stdin' : '/proc/self/fd/0';
result.file = opts.dash ? '-' : inputPath;
Which is not correct logic (inputPath
should only be evaluated if opts.dash
is not truthy).
The other alternative is
var inputPath;
if (opts.dash) {
inputPath = '-';
} else {
inputPath = platform === 'linux' ? '/dev/stdin' : '/proc/self/fd/0';
}
Which, come on, seriously? >.> So unnecessary. A nested ternary operation was just fine and readable. I understand this is something I can disable in the package.json but I want to be as, uh... "orthodox" as possible. Plus, turning it off will not (should not) interfere with any existing code.
Need a badge so projects can let contributors know which code style the project uses.
I'm probably gonna do something custom, like I did for awesome.
I won't get to this in the near term, so for now I would recommend using this:
[![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
Someone needs to do a Syntastic plugin.
https://github.com/scrooloose/syntastic/wiki/Syntax-Checker-Guide
i might suggest that not caring about code style/formatting is a better way to go
why people get so hung up on something so pointless i would not know
code is a big messy garden/wilderness
so many people will do it so many ways
parser doesn't care, why should i? i don't uses spaces or tabs
what do they do? why even bother typing them in
if you are reading code you are kind of lost
readability and maintainability is a myth
who cares less -- wins
I've installed xo globally and then used xo --init
to add xo as a local dependency to my project, as the documentation suggest.
However I'm having issues when referencing xo from within an npm run task, for example;
package.json
"scripts": {
"lint": "xo *.js **/*.js --space"
}
When runningnpm run lint
I'm receiving errors about various packages not being available, initially it was eslint-config-xo
;
Error: Cannot read config package: eslint-config-xo
Error: Cannot find module 'eslint-config-xo'
I installed that as a development dependency but was then told;
Error: Cannot find module 'eslint-plugin-babel'
Which I installed, but then received an error message saying that eslint-plugin-no-empty-blocks
cannot be found and this has continued indefinitely with various other eslint modules.
It doesn't feel right to have to add every eslint config module to my dev dependencies to allow xo to work locally. Am I doing something wrong?
As mentioned in issue #15, there is no error handling when reading package.json
(index.js:40), it's ignored and execution continues without user knowing what happened.
It would be great to put a message to notify the user something bad happened and maybe even stop the execution. I'm not sure about the last one, but in my opinion, incorrect npm config isn't something one would even want.
I'm willing to work on it after we clarify what exactly should happen.
Latest xo has problems with detecting usage of React components over the code (no-unused-vars
). Even though components are used in code, xo reports them as defined but never used
.
example.js:
const React = require('react');
const Button = React.createClass({});
const Container = React.createClass({
render: function () {
return <Button />;
}
});
package.json:
{
"scripts": {
"test": "xo"
},
"devDependencies": {
"xo": "^0.12.1"
}
}
$ npm test
Right now, setting the style to tabs-not-spaces and running xo --fix
will result in a mess of mixed tabs and spaces if there were spaces. You also need to run it multiple times, once for each level of tabs that needs fixing.
The reason is that the eslint fix simply fixes the "expected 1 tab indentation" errors which occur at every block indent.
I am not sure that eslint is equipped to handle the possibly correct fix, which is to give an error on each line that does not have the exact required number of tabs/spaces. This possible fix also impacts non-uniform indentation, so not sure if that's desirable
So possibly XO needs to help out a little with a whitespace conversion pre-run and a recursive run until all the whitespace errors are resolved.
Reading the FAQ you said that standard
didn't pick the most popular style, but spaces are widely preferred over tabs. Can you make this switch so it can just be "the default standard"? :)
For example:
# echo "let foo; console.log(foo);" | xo --stdin --esnext
<text>
1:2 error Unexpected token let
Looks like the cli isn't passing the options to lintText()
:
https://github.com/sindresorhus/xo/blob/master/cli.js#L74
no-empty-labels
was removed from the latest ESLint in favor of no-labels
.
Should be removed from XO whenever they release and we update.
ESLint 1.2.0 came with a new ES2015 rule called prefer-reflect
that suggests Reflect
where applicable.
The only method I'm unsure about is preferring Reflect.deleteProperty
over the delete
keyword.
xojs/eslint-config-xo@8b16afc#diff-52091282b1c3f2e056102160d5de3ec2R35
Thoughts?
Also, does it make sense to use Reflect over the other methods, like call
, apply
, etc?
Right now it's not possible to lint anything in a fixtures
directory, even if you want to. There should be a way to enable that.
I tried this:
{
"xo": {
"ignore": [
"!{test/,}fixture{s,}/**"
]
}
}
But no luck
IssueHunt has been backed by the following sponsors. Become a sponsor
When I install some npm packages, there are messages like:
npm ERR! git fetch -a origin (git://github.com/sindresorhus/xo.git) fatal: read error: ...
Please don't access anything other than NPM registry. Since NPM is not only used in Internet, but also in some private network where there is no access to github.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.