Skip to content

Commit

Permalink
Clean up rules and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Sep 5, 2023
1 parent 363fd39 commit 01d1214
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/src/extend/code-path-analysis.md
Expand Up @@ -37,7 +37,7 @@ This has references of both the initial segment and the final segments of a code
* `finalSegments` (`CodePathSegment[]`) - The final segments which includes both returned and thrown.
* `returnedSegments` (`CodePathSegment[]`) - The final segments which includes only returned.
* `thrownSegments` (`CodePathSegment[]`) - The final segments which includes only thrown.
* `currentSegments` (`CodePathSegment[]`) - Segments of the current position.
* `currentSegments` (`CodePathSegment[]`) - **Deprecated.** Segments of the current position.
* `upper` (`CodePath|null`) - The code path of the upper function/global scope.
* `childCodePaths` (`CodePath[]`) - Code paths of functions this code path contains.

Expand Down Expand Up @@ -148,7 +148,7 @@ module.exports = function(context) {
* @param {ASTNode} node - The current node.
* @returns {void}
*/
"onCodePathSegmentLoop(fromSegment, toSegment, node) {
onCodePathSegmentLoop(fromSegment, toSegment, node) {
// do something with segment
}
};
Expand Down
11 changes: 5 additions & 6 deletions lib/rules/require-atomic-updates.js
Expand Up @@ -242,7 +242,7 @@ module.exports = {

// Handle references to prepare verification.
Identifier(node) {
const { codePath, referenceMap } = stack;
const { referenceMap } = stack;
const reference = referenceMap && referenceMap.get(node);

// Ignore if this is not a valid variable reference.
Expand All @@ -255,7 +255,7 @@ module.exports = {

// Add a fresh read variable.
if (reference.isRead() && !(writeExpr && writeExpr.parent.operator === "=")) {
segmentInfo.markAsRead(codePath.currentSegments, variable);
segmentInfo.markAsRead(stack.currentSegments, variable);
}

/*
Expand All @@ -282,16 +282,15 @@ module.exports = {
* If the reference exists in `outdatedReadVariables` list, report it.
*/
":expression:exit"(node) {
const { codePath, referenceMap } = stack;

// referenceMap exists if this is in a resumable function scope.
if (!referenceMap) {
if (!stack.referenceMap) {
return;
}

// Mark the read variables on this code path as outdated.
if (node.type === "AwaitExpression" || node.type === "YieldExpression") {
segmentInfo.makeOutdated(codePath.currentSegments);
segmentInfo.makeOutdated(stack.currentSegments);
}

// Verify.
Expand All @@ -303,7 +302,7 @@ module.exports = {
for (const reference of references) {
const variable = reference.resolved;

if (segmentInfo.isOutdated(codePath.currentSegments, variable)) {
if (segmentInfo.isOutdated(stack.currentSegments, variable)) {
if (node.parent.left === reference.identifier) {
context.report({
node: node.parent,
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/linter/code-path-analysis/code-path-analyzer.js
Expand Up @@ -531,6 +531,28 @@ describe("CodePathAnalyzer", () => {

});

it("should be fired after a return inside of function and if statement", () => {
let lastCodePathNodeType = null;

linter.defineRule("test", {
create: () => ({
onUnreachableCodePathSegmentEnd(segment, node) {
lastCodePathNodeType = node.type;
assert(segment instanceof CodePathSegment);
assert.strictEqual(node.type, "BlockStatement");
},
"Program:exit"() {
assert.strictEqual(lastCodePathNodeType, "BlockStatement");
}
})
});
linter.verify(
"function foo() { if (bar) { return; foo(); } else {} }",
{ rules: { test: 2 } }
);

});

it("should be fired at the end of programs/functions for the final segment", () => {
let count = 0;
let lastNodeType = null;
Expand Down

0 comments on commit 01d1214

Please sign in to comment.