Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): prevent top-level effects #101

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/eslint-plugin/lib/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
module.exports = {
plugins: ['@jessie.js'],
processor: '@jessie.js/use-jessie',
globals: {
harden: false,
assert: false,
},
};
92 changes: 92 additions & 0 deletions packages/eslint-plugin/lib/rules/no-top-level-effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* global module, require */
/**
* @author Michael FIG
* See LICENSE file in root direcotry for full license.
*/

'use strict';

const nono = `not allowed in Jessie`;

/**
* Imports a specific module.
*
* @param {...string} moduleNames - module names to import.
* @returns {object|null} The imported object, or null.
*/
function safeRequire(...moduleNames) {
for (const moduleName of moduleNames) {
try {
// eslint-disable-next-line import/no-dynamic-require,global-require
return require(moduleName);
} catch (_err) {
// Ignore.
}
}
return null;
}

const CodePathAnalyzer = safeRequire(
'eslint/lib/linter/code-path-analysis/code-path-analyzer',
'eslint/lib/code-path-analysis/code-path-analyzer',
);

module.exports = {
meta: {
docs: {
description: 'prevent top-level code evaluation',
category: 'Possible Errors',
recommended: true,
url:
'https://github.com/endojs/Jessie/blob/master/packages/eslint-plugin/lib/rules/no-top-level-effects.js',
},
type: 'problem',
fixable: null,
schema: [],
supported: true || CodePathAnalyzer !== null,
},
create(context) {
let depth = 0;
return {
':function': _node => {
depth += 1;
},
':function:exit': _node => {
depth -= 1;
},
CallExpression: node => {
if (depth > 0) {
return;
}
if (
node.callee.type === 'Identifier' &&
node.callee.name === 'harden'
) {
return;
}
context.report({
node,
message: `top-level function calls are ${nono}; only 'harden' is allowed`,
});
},
UpdateExpression: node => {
if (depth > 0) {
return;
}
context.report({
node,
message: `top-level mutations are ${nono}; only non-side-effecting ones are allowed`,
});
},
AssignmentExpression: node => {
if (depth > 0) {
return;
}
context.report({
node,
message: `top-level mutating assignments are ${nono}; only bindings are allowed`,
});
},
};
},
};
9 changes: 9 additions & 0 deletions packages/eslint-plugin/lib/use-jessie-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const nono = `not allowed in Jessie`;

exports.jessieRules = {
'@jessie.js/no-nested-await': ['error'],
'@jessie.js/no-top-level-effects': ['error'],
curly: ['error', 'all'],
eqeqeq: ['error', 'always'],
'no-bitwise': ['error'],
Expand All @@ -18,6 +19,14 @@ exports.jessieRules = {
// to see what AST nodes are produced by different programs.
'no-restricted-syntax': [
'error',
{
selector: `Program > [type!='ExpressionStatement'][type!='ImportDeclaration'][type!='ExportDefaultDeclaration'][type!='ExportNamedDeclaration'][type!='VariableDeclaration']`,
message: `top-level statement of this type is ${nono}`,
},
{
selector: `Program > VariableDeclaration[kind!='const']`,
message: `non-'const' top-level variable declarations are ${nono}`,
},
{
selector: `BinaryExpression[operator='in']`,
message: `'in' is ${nono}`,
Expand Down