-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref: Avoid optional chaining & add eslint rule (#6777)
As this is transpiled to a rather verbose form. Co-authored-by: Lukas Stracke <lukas.stracke@sentry.io>
- Loading branch information
Showing
27 changed files
with
162 additions
and
54 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
61 changes: 61 additions & 0 deletions
61
packages/eslint-plugin-sdk/src/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* @fileoverview Rule to disallow using optional chaining - because this is transpiled into verbose code. | ||
* @author Francesco Novy | ||
* | ||
* Based on https://github.com/facebook/lexical/pull/3233 | ||
*/ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'disallow usage of optional chaining', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
messages: { | ||
forbidden: 'Avoid using optional chaining', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
|
||
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({ | ||
messageId: 'forbidden', | ||
node: sourceCode.getTokenAfter(node.callee, isQuestionDotToken), | ||
}); | ||
}, | ||
'MemberExpression[optional=true]'(node) { | ||
context.report({ | ||
messageId: 'forbidden', | ||
node: sourceCode.getTokenAfter(node.object, isQuestionDotToken), | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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
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
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
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
Oops, something went wrong.