-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add
es/no-optional-chaining
rule (#34)
- Loading branch information
Showing
5 changed files
with
181 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,28 @@ | ||
# disallow optional chaining (es/no-optional-chaining) | ||
|
||
This rule reports ES2020 [Optional Chaining](https://github.com/tc39/proposal-optional-chaining) as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
<eslint-playground type="bad" code="/*eslint es/no-optional-chaining: error */ | ||
var x = a?.b | ||
var x = a?.[b] | ||
foo?.() | ||
" /> | ||
|
||
👌 Examples of **correct** code for this rule: | ||
|
||
<eslint-playground type="good" code="/*eslint es/no-optional-chaining: error */ | ||
var x = a != null ? a.b : undefined | ||
var x = a && a.b | ||
var x = a != null ? a[b] : undefined | ||
var x = a && a[b] | ||
foo && foo() | ||
" /> | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/lib/rules/no-optional-chaining.js) | ||
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/tests/lib/rules/no-optional-chaining.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
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,61 @@ | ||
/** | ||
* @author Yosuke Ota <https://github.com/ota-meshi> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "disallow optional chaining.", | ||
category: "ES2020", | ||
recommended: false, | ||
url: | ||
"http://mysticatea.github.io/eslint-plugin-es/rules/no-optional-chaining.html", | ||
}, | ||
fixable: null, | ||
messages: { | ||
forbidden: "ES2020 optional chaining is forbidden.", | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
create(context) { | ||
const sourceCode = context.getSourceCode() | ||
|
||
/** | ||
* Checks if the given token is a `?.` token or not. | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} `true` if the token is a `?.` token. | ||
*/ | ||
function isQuestionDotToken(token) { | ||
return ( | ||
token.value === "?." && | ||
(token.type === "Punctuator" || // espree has been parsed well. | ||
// espree@7.1.0 doesn't parse "?." tokens well. Therefore, get the string from the source code and check it. | ||
sourceCode.getText(token) === "?.") | ||
) | ||
} | ||
|
||
return { | ||
"CallExpression[optional=true]"(node) { | ||
context.report({ | ||
node: sourceCode.getTokenAfter( | ||
node.callee, | ||
isQuestionDotToken | ||
), | ||
messageId: "forbidden", | ||
}) | ||
}, | ||
"MemberExpression[optional=true]"(node) { | ||
context.report({ | ||
node: sourceCode.getTokenAfter( | ||
node.object, | ||
isQuestionDotToken | ||
), | ||
messageId: "forbidden", | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
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 @@ | ||
/** | ||
* @author Yosuke Ota <https://github.com/ota-meshi> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
"use strict" | ||
|
||
const RuleTester = require("../../tester") | ||
const rule = require("../../../lib/rules/no-optional-chaining.js") | ||
|
||
if (!RuleTester.isSupported(2020)) { | ||
//eslint-disable-next-line no-console | ||
console.log("Skip the tests of no-optional-chaining.") | ||
return | ||
} | ||
|
||
new RuleTester().run("no-optional-chaining", rule, { | ||
valid: ["var x = a.b", "var x = a[b]", "foo()"], | ||
invalid: [ | ||
{ | ||
code: "var x = a?.b", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 10, | ||
endColumn: 12, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "var x = a?.[b]", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 10, | ||
endColumn: 12, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "foo?.()", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 4, | ||
endColumn: 6, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "var x = ((a?.b)?.c)?.()", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 12, | ||
endColumn: 14, | ||
}, | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 16, | ||
endColumn: 18, | ||
}, | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 20, | ||
endColumn: 22, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "var x = a/*?.*/?.b", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 16, | ||
endColumn: 18, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: "var x = '?.'?.['?.']", | ||
errors: [ | ||
{ | ||
message: "ES2020 optional chaining is forbidden.", | ||
column: 13, | ||
endColumn: 15, | ||
}, | ||
], | ||
}, | ||
], | ||
}) |