assignment-example

Try in Playground
typescript-templatesUnknownInformational

0

No tags

No CWE or CVE

Ast Rule: assignment


assignment-example

How to write a rule
function visit(node, filename, code) {
	if(!node.left || !node.right) {
		return;
	}
	
	if(node.left.astType === "string" && node.left.value === "foo") {
			const error = buildError(node.left.start.line, node.left.start.col,
															 node.left.end.line, node.left.end.col,
															 "foo is a bad variable name", "CRITICAL", "SAFETY");
			const edit = buildEditUpdate(
				node.left.start.line, node.left.start.col,
				node.left.end.line, node.left.end.col,
				"bar");
			const fix = buildFix("use bar instead", [edit]);

			addError(error.addFix(fix));
	}
	
	if(node.right.astType === "string" && node.right.value === "42") {
			const error = buildError(node.right.start.line, node.right.start.col,
															 node.right.end.line, node.right.end.col,
															 "42 is a bad value", "CRITICAL", "SAFETY");
			const edit = buildEditUpdate(
				node.left.start.line, node.left.start.col,
				node.left.end.line, node.left.end.col,
				"51");
			const fix = buildFix("use 51", [edit]);

			addError(error.addFix(fix));
	}
}

assignment.ts

Expected test result: has error

const foo = 42
Add comment

Log in to add a comment


    Be the first one to leave a comment!

Codiga Logo
Codiga Hub
  • Rulesets
  • Playground
  • Snippets
  • Cookbooks
soc-2 icon

We are SOC-2 Compliance Certified

G2 high performer medal

Codiga – All rights reserved 2022.