Skip to content

Commit

Permalink
feat(eslint-plugin)!: Disallow ?? and ?. operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Dec 19, 2023
1 parent 3622b48 commit 15b543a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/eslint-plugin/lib/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,10 @@ module.exports = {
rules: {
'@endo/assert-fail-as-throw': 'error',
'guard-for-in': 'error',
// 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.
'@endo/no-optional-chaining': 'error',
'@endo/no-nullish-coalescing': 'error',
},
};
35 changes: 35 additions & 0 deletions packages/eslint-plugin/lib/rules/no-nullish-coalescing.js
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',
});
}
},
};
},
};
55 changes: 55 additions & 0 deletions packages/eslint-plugin/lib/rules/no-optional-chaining.js
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',
});
},
};
},
};

0 comments on commit 15b543a

Please sign in to comment.