Skip to content

Commit

Permalink
refactor abortion process in namespaceValidator, namespace, no-deprec…
Browse files Browse the repository at this point in the history
…ated rule

Helper function namespaceValidator needs to stop executing depending on callback functions. Because callback functions coming from namespace, no-deprecated rules cannot stop execution due to scope, I added a statement which is passed from namespaceValidator to the callback functions received from namespace and no-deprecated rules, and if callback functions want to stop execution, then they return the same statement and namespaceValidator stops execution
  • Loading branch information
azyzz228 committed Nov 2, 2022
1 parent f761be2 commit 5985656
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/core/namespaceValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ export default function namespaceValidator(
// go deep
let namespace = namespaces.get(node.object.name);
const namepath = [node.object.name];
const returnChecker = 510; // callbacks return this value if they want THIS function to stop executing
// while property is namespace and parent is member expression, keep validating
while (namespace instanceof Exports && node.type === 'MemberExpression') {
if (node.computed) {
if (onComputed(node) === 'return') {
if (onComputed(node, returnChecker) === returnChecker) {
return;
}
}

if (onNamespaceNotFound) {
if (onNamespaceNotFound(node, namespace, namepath) === 'return') {
if (onNamespaceNotFound(node, namespace, namepath, returnChecker) === returnChecker) {
return;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/rules/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,25 +118,25 @@ module.exports = {
}

function onComputed(context, allowComputed){
return function innerOnComputed(node) {
return function innerOnComputed(node, returnChecker) {
if (!allowComputed) {
context.report(
node.property,
`Unable to validate computed reference to imported namespace '${dereference.object.name}'.`,
);
}
return 'return';
return returnChecker;
};
}

function onNamespaceNotFound(context) {
return function inner(node, namespace, namepath) {
return function inner(node, namespace, namepath, returnChecker) {
if (!namespace.has(node.property.name)) {
context.report(
node.property,
makeMessage(node.property, namepath),
);
return 'return';
return returnChecker;
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module.exports = {

function onComputed() {
// ignore computed parts for now
return 'return';
return arguments[2];
}

namespaceValidator(
Expand Down

0 comments on commit 5985656

Please sign in to comment.