Skip to content

Commit

Permalink
feat: Fix or disable rules re babel#90
Browse files Browse the repository at this point in the history
- Disable `array-callback-return`
- Disable `react/require-render-return`
- Monkeypatch `fp/no-nil`

Re wcjohnson/lightscript#90
  • Loading branch information
wcjohnson committed Oct 2, 2018
1 parent 753d3ae commit 28932c6
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 9 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Issues: https://github.com/wcjohnson/lightscript/issues
$ (npm bin)/eslint --ext .js,.lsc src/
```

`@lightscript/eslint-plugin` is fully compatible with all other ESLint plugins and can lint JavaScript as well as LightScript code.
`@lightscript/eslint-plugin` is compatible with most other ESLint plugins and can lint JavaScript as well as LightScript code.

## The Details

Expand Down Expand Up @@ -155,7 +155,16 @@ Warn when a `,` is unnecessary according to LightScript list separator rules.
If ESLint is hoisted outside of the root `node_modules` folder of your package, it will not be able to find the plugin and you will encounter "can't find plugin" errors. To fix this, use the `nohoist` option of your monorepo tool to ensure ESLint is not hoisted out of your package.

- `no-extra-semi` rule:
The `no-extra-semi` rule has a bug when iterating over whiteblock code. When linting LightScript files, this rule is automatically disabled. We recommend the `@lightscript/unnecessary-semi` rule instead.
When linting LightScript files, this rule is automatically disabled. We recommend the `@lightscript/unnecessary-semi` rule instead.

- `array-callback-return` rule:
When linting LightScript files, this rule is automatically disabled. This rule
is broken pending changes to ESLint's code path analysis; see
[this issue](https://github.com/eslint/eslint/issues/10823)

- `react/require-render-return` rule:
When linting LightScript files, this rule is automatically disabled. Because
of LightScript's implicit returns, this rule is almost always superfluous.

Flow:
> Check out [eslint-plugin-flowtype](https://github.com/gajus/eslint-plugin-flowtype): An `eslint` plugin that makes flow type annotations global variables and marks declarations as used. Solves the problem of false positives with `no-undef` and `no-unused-vars`.
Expand Down
4 changes: 4 additions & 0 deletions src/rules/patched.lsc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Patches for core ESLint rules
import noUseBeforeDefine from './patched/noUseBeforeDefine'
import restSpreadSpacing from './patched/rest-spread-spacing'
import fpNoNil from './patched/fp-no-nil'

patchedRules = {}

Expand Down Expand Up @@ -33,4 +34,7 @@ patches = {
"no-use-before-define": patchIfLsc(noUseBeforeDefine)
"rest-spread-spacing": patchIfLsc(restSpreadSpacing)
"no-extra-semi": patchIfLsc(nullRule)
"array-callback-return": patchIfLsc(nullRule)
"fp/no-nil": patchIfLsc(fpNoNil)
"react/require-render-return": patchIfLsc(nullRule)
}
90 changes: 90 additions & 0 deletions src/rules/patched/fp-no-nil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

function isComparison(node) {
return node.parent &&
node.parent.type === 'BinaryExpression' &&
(['==', '!=', '===', '!=='].indexOf(node.parent.operator) > -1)
}

function reportUseOutsideOfComparison(context, node) {
if (!isComparison(node)) {
context.report({
node,
message: 'Unallowed use of `null` or `undefined`'
});
}
}

function endsWithReturnStatement(body) {
return (
body.length > 0 &&
body[body.length - 1].type === 'ReturnStatement'
)
}

const create = function (context) {
function reportFunctions(node) {
// LSC: if function has an implicit return, good enough for me
if( context.parserServices.getImplicitlyReturnedNodes(node) ) {
return;
}

if (node.body.type === 'BlockStatement' &&
!endsWithReturnStatement(node.body.body)
) {
context.report({
node,
message: 'Function must end with a return statement, so that it doesn\'t return `undefined`'
});
}
}

const visitor = {
Literal(node) {
if (node.value === null) {
reportUseOutsideOfComparison(context, node);
}
},
Identifier(node) {
if (node.name === 'undefined') {
reportUseOutsideOfComparison(context, node);
}
},
VariableDeclarator(node) {
if (node.init === null) {
context.report({
node,
message: 'Variable must be initialized, so that it doesn\'t evaluate to `undefined`'
});
}
},
ReturnStatement(node) {
if (node.argument === null) {
context.report({
node,
message: 'Return statement must return an explicit value, so that it doesn\'t evaluate to `undefined`'
});
}
},
ArrowFunctionExpression: reportFunctions,
FunctionDeclaration: reportFunctions,
FunctionExpression: reportFunctions
};

for(const k of context.parserServices.aliasesFor("Function")) {
visitor[k] = reportFunctions;
}

return visitor;
};

module.exports = {
create,
meta: {
docs: {
description: 'Forbid the use of `null` and `undefined`.',
recommended: 'error',
url: 'https://github.com/jfmengels/eslint-plugin-fp/tree/master/docs/rules/no-nil.md'
}
}
};
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[
"3:11 Expected to return a value in arrow function. array-callback-return"
]
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
f() -> 1
f()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
f() ->
1
f()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
f() ->
for x in []: x
f()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
"1:1 Function must end with a return statement, so that it doesn't return `undefined` fp/no-nil"
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"no-unused-vars": 1,
"no-undef": 1,
"array-callback-return": 1,
"react/require-render-return": 1
"react/require-render-return": 1,
"fp/no-nil": 1
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
[
"3:1 Your render method should have return statement react/require-render-return"
]
[]

0 comments on commit 28932c6

Please sign in to comment.