forked from lightscript/lightscript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fix or disable rules re babel#90
- Disable `array-callback-return` - Disable `react/require-render-return` - Monkeypatch `fp/no-nil` Re wcjohnson/lightscript#90
- Loading branch information
Showing
12 changed files
with
122 additions
and
9 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
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,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' | ||
} | ||
} | ||
}; |
4 changes: 1 addition & 3 deletions
4
test/fixtures/lightscript/brokenRules/regression/#90/array-callback-return/output.json
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
[ | ||
"3:11 Expected to return a value in arrow function. array-callback-return" | ||
] | ||
[] |
2 changes: 2 additions & 0 deletions
2
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/negative-oneline/input.js
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,2 @@ | ||
f() -> 1 | ||
f() |
1 change: 1 addition & 0 deletions
1
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/negative-oneline/output.json
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 @@ | ||
[] |
3 changes: 3 additions & 0 deletions
3
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/negative-twoline/input.js
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,3 @@ | ||
f() -> | ||
1 | ||
f() |
1 change: 1 addition & 0 deletions
1
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/negative-twoline/output.json
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 @@ | ||
[] |
3 changes: 3 additions & 0 deletions
3
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/positive/input.js
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,3 @@ | ||
f() -> | ||
for x in []: x | ||
f() |
3 changes: 3 additions & 0 deletions
3
test/fixtures/lightscript/brokenRules/regression/#90/fp-no-nil/positive/output.json
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,3 @@ | ||
[ | ||
"1:1 Function must end with a return statement, so that it doesn't return `undefined` fp/no-nil" | ||
] |
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
4 changes: 1 addition & 3 deletions
4
test/fixtures/lightscript/brokenRules/regression/#90/require-render-return/output.json
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
[ | ||
"3:1 Your render method should have return statement react/require-render-return" | ||
] | ||
[] |