Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Add rule for print concat
Browse files Browse the repository at this point in the history
  • Loading branch information
nishtahir committed Jun 13, 2018
1 parent 14a7996 commit 4e64930
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/_data/rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ categories:
- name: no-void-func
description: Disallow functions with `void` return type
since: 1.0.0
- name: print-concat-semicolon
description: Print concatenation must be done with a semicolon.
since: 2.0.0
14 changes: 14 additions & 0 deletions docs/user-guide/rules/print-concat-semicolon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: print-concat-semicolon
layout: doc
edit_link: https://github.com/willowtreeapps/wist/edit/master/docs/user-guide/rules/print-concat-semicolon
sidebar: "user-guide"
grouping: "rules"
---

# print-concat-semicolon

Print concatenation must be done with a semicolon.

## Version
This rule was introduced in Wist 2.0.0.
42 changes: 42 additions & 0 deletions src/js/rules/print-concat-semicolon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* When constructing console strings from non-string objects, avoid '+' operator
* which requires objects to be converted to strings first.
* This approach tends to be susceptible to crashes.
*
* @description Print concatenation must be done with a semicolon.
* @since 2.0.0
*/
'use strict';

module.exports = {
create(context) {
return {
'printStatement:enter': function (ast) {
ast.children.forEach(element => {
let node = element.node;
if (node.context == "expression") {
let concatSymbol = element.children.findFirst(child => child.node.text == "+")
if (concatSymbol != null) {
context.report({
message: `'+' should not be used in print statements. Consider using a ';' instead.`,
loc: {
start: ast.node.start,
end: ast.node.stop
}
});
}
console.log(element);
}
});
// let node = astNode.node;
// context.report({
// message: `'print' statements should only be used for debugging purposes.`,
// loc: {
// start: node.start,
// end: node.stop
// }
// });
}
};
}
};

0 comments on commit 4e64930

Please sign in to comment.