-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Avoid null coalescing and optional chaining (merge #1905)
- Loading branch information
Showing
6 changed files
with
109 additions
and
3 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,35 @@ | ||
'use strict'; | ||
|
||
// Agoric still uses Endo dependencies under an emulation of ESM we call RESM | ||
// because it is invoked with `node -r esm`. | ||
// RESM does not support ?? nor ?. operators, so we must avoid them expressly. | ||
// TODO remove when https://github.com/Agoric/agoric-sdk/issues/8671 | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'disallow nullish coalescing.', | ||
category: 'ES2020', | ||
recommended: false, | ||
url: 'https://github.com/endojs/endo/blob/master/packages/eslint-plugin/lib/rules/no-nullish-coalescing.js', | ||
}, | ||
fixable: null, | ||
messages: { | ||
forbidden: 'ES2020 nullish coalescing is forbidden.', | ||
}, | ||
schema: [], | ||
type: 'problem', | ||
}, | ||
create(context) { | ||
return { | ||
LogicalExpression(node) { | ||
if (node.operator === '??') { | ||
context.report({ | ||
node, | ||
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,55 @@ | ||
/** @author Yosuke Ota <https://github.com/ota-meshi> */ | ||
'use strict'; | ||
|
||
// Agoric still uses Endo dependencies under an emulation of ESM we call RESM | ||
// because it is invoked with `node -r esm`. | ||
// RESM does not support ?? nor ?. operators, so we must avoid them expressly. | ||
// TODO remove when https://github.com/Agoric/agoric-sdk/issues/8671 | ||
|
||
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(); | ||
|
||
/** | ||
* @param {Token} token The token to check. | ||
* @returns {boolean} whether the token is a `?.` token. | ||
*/ | ||
function isQuestionDotToken(token) { | ||
return ( | ||
token.value === '?.' && | ||
(token.type === 'Punctuator' || // espree has been parsed well. | ||
// [email protected] 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
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