-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
no-promise-fibers-usage
.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* @fileoverview This rule checks the usage of Meteor Promise Fibers methods. | ||
* @author Matheus Castro | ||
* @copyright 2023 Renan Castro. All rights reserved. | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
|
||
const promiseFiberPrototypeAwait = "await"; | ||
Check failure on line 8 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
const promiseFiberMethods = [ | ||
Check failure on line 9 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
"await", | ||
"awaitAll", | ||
"async", | ||
"asyncApply", | ||
]; | ||
|
||
const isGlobalPromiseFiber = (node) => { | ||
const nodeObjectName = node && | ||
Check failure on line 17 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
node.object && | ||
node.object.type === "Identifier" && | ||
Check failure on line 19 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
node.object.name; | ||
|
||
return nodeObjectName === "Promise"; | ||
Check failure on line 22 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
}; | ||
|
||
const isUsingNotAllowedPromiseFiberMethod = (node) => { | ||
if (!isGlobalPromiseFiber(node)) { | ||
return; | ||
} | ||
|
||
const nodePropertyName = node && | ||
Check failure on line 30 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
node.property && | ||
node.property.type === "Identifier" && | ||
Check failure on line 32 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
node.property.name; | ||
|
||
return promiseFiberMethods.includes(nodePropertyName); | ||
} | ||
Check failure on line 36 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
Check failure on line 36 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
Check failure on line 36 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
|
||
const isPromisePrototypeAwaitCall = (node) => { | ||
const isMemberExpression = node && node.type === "MemberExpression"; | ||
Check failure on line 39 in npm-packages/eslint-plugin-meteor/lib/rules/no-promise-fibers-usage.js GitHub Actions / test (14.x)
|
||
if (!isMemberExpression) { | ||
return; | ||
} | ||
|
||
const isAccessingGlobalPromise = node && | ||
node.object && | ||
node.object.type === "Identifier" && | ||
node.object.name === "Promise"; | ||
if (isAccessingGlobalPromise) { | ||
return; | ||
} | ||
|
||
const nodePropertyName = node && | ||
node.property && | ||
node.property.type === "Identifier" && | ||
node.property.name; | ||
|
||
return nodePropertyName === promiseFiberPrototypeAwait; | ||
}; | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Detect `Promise` Fibers abstraction calls', | ||
recommended: true, | ||
}, | ||
}, | ||
create: (context) => ({ | ||
MemberExpression: (node) => { | ||
if (isUsingNotAllowedPromiseFiberMethod(node)) { | ||
return context.report(node, "Fibers abstractions for Promises should be replaced by native methods"); | ||
} | ||
|
||
if (isPromisePrototypeAwaitCall(node)) { | ||
return context.report(node, "Promise Fibers prototype await call is not allowed"); | ||
} | ||
}, | ||
}), | ||
}; |