Repository: eslint/eslint
File: tests/lib/languages/js/source-code/source-code.js (JavaScript)

Code before:
			const sourceCode = new SourceCode(code, ast);
			const steps = sourceCode.traverse();

			const visitSteps = steps.filter(step => step.kind === 1);

			visitSteps.forEach(step => {
				assert.strictEqual(
					step.args.length,
					1,
					`Visit step for ${step.target.type} should have exactly 1 argument, got ${step.args.length}`,
				);

				const [node] = step.args;

				// First argument should be the node itself
				assert.strictEqual(
					node,
					step.target,
					`First argument should be the node (${step.target.type})`,
				);
			});
		});

		it("should set `parent` property on nodes as the actual parent node in the AST", () => {
			const code = "if (x) { var y = 1; }";
			const ast = espree.parse(code, DEFAULT_CONFIG);
			const sourceCode = new SourceCode(code, ast);
			sourceCode.traverse();

			const programNode = sourceCode.ast;
			assert.strictEqual(programNode.parent, null);

			const ifNode = programNode.body[0];
			assert.strictEqual(ifNode.parent, programNode);

			const xNode = ifNode.test;
			assert.strictEqual(xNode.parent, ifNode);

			const blockNode = ifNode.consequent;
			assert.strictEqual(blockNode.parent, ifNode);

			const varNode = blockNode.body[0];
			assert.strictEqual(varNode.parent, blockNode);

			const declaratorNode = varNode.declarations[0];
			assert.strictEqual(declaratorNode.parent, varNode);

			const yNode = declaratorNode.id;
			assert.strictEqual(yNode.parent, declaratorNode);

			const number1Node = declaratorNode.init;

Review comment (suggestion):
Since traverse steps don't provide parent argument, I've rewritten this test to check if `parent` property is correctly set in the AST (which is indeed done in `SourceCode#traverse()`).

Code after:
			const sourceCode = new SourceCode(code, ast);
			const steps = sourceCode.traverse();

			const visitSteps = steps.filter(step => step.kind === 1);

			assert.strictEqual(visitSteps.length, 10);

			visitSteps.forEach(step => {
				assert.strictEqual(
					step.args.length,
					1,
					`Visit step for ${step.target.type} should have exactly 1 argument, got ${step.args.length}`,
				);

				const [node] = step.args;

				// First argument should be the node itself
				assert.strictEqual(
					node,
					step.target,
					`First argument should be the node (${step.target.type})`,
				);
			});
		});

		it("should set `parent` property on nodes as the actual parent node in the AST", () => {
			const code = "if (x) { var y = 1; }";
			const ast = espree.parse(code, DEFAULT_CONFIG);
			const sourceCode = new SourceCode(code, ast);
			sourceCode.traverse();

			const programNode = sourceCode.ast;
			assert.strictEqual(programNode.parent, null);

			const ifNode = programNode.body[0];
			assert.strictEqual(ifNode.parent, programNode);

			const xNode = ifNode.test;
			assert.strictEqual(xNode.parent, ifNode);

			const blockNode = ifNode.consequent;
			assert.strictEqual(blockNode.parent, ifNode);

			const varNode = blockNode.body[0];
			assert.strictEqual(varNode.parent, blockNode);

			const declaratorNode = varNode.declarations[0];
			assert.strictEqual(declaratorNode.parent, varNode);

			const yNode = declaratorNode.id;
			assert.strictEqual(yNode.parent, declaratorNode);