From c961b4bc2adf6a0284d5ad47cff2324f6093a3d7 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Fri, 19 Apr 2019 11:33:32 +0900 Subject: [PATCH 01/13] Create test0419.js --- test0419.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 test0419.js diff --git a/test0419.js b/test0419.js new file mode 100644 index 0000000..2f91ea7 --- /dev/null +++ b/test0419.js @@ -0,0 +1,12 @@ +function CWE_398() { // IDENTICAL_BRANCHES + if (x >= 0) { + y = x; + } else { + y = x; + } +} +function CWE_476() { // NULL_POINTER + var obj; + var y = obj.x; // UNINITIALIZED_LOCAL_VAR + console.log(y); +} From 6ce7745ba857eb08cb953dddf9f1365b2283f3f2 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Mon, 15 Jul 2019 11:39:05 +0900 Subject: [PATCH 02/13] Create react-rule.js --- react-rule.js | 216 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 react-rule.js diff --git a/react-rule.js b/react-rule.js new file mode 100644 index 0000000..c25ecb9 --- /dev/null +++ b/react-rule.js @@ -0,0 +1,216 @@ +const instance = ReactDOM.render(, rootElement); +foo(instance); // ASYNC_RENDER_RETURN_VALUE alarm + +import ReactDOM from 'react-dom'; + +ReactDOM.render( +
+ {/* BAD_DANGER_WITH_CHILDREN alarm */} +
, document.getElementById("root") +); + +import React from 'react'; + +class Hello extends React.Component { + render() { + return ( +
{/* BAD_EVENT_HANDLER alarm */} + Hello +
+ ); + } +} + +import React from 'react'; + +class Hello extends React.Component { + render() { + return ( + false}> {/* BAD_EVENT_HANDLER_RETURN_FALSE alarm */} + foo.com + + ); + } +} + +import React from 'react'; + +class Hello extends React.Component { + render() { + return ( +
+ /* This is a comment */ {/* BAD_JSX_COMMENT alarm because this text is recognized as a JSX child instead of a comment. */} +
// is a double slash.
{/* BAD_JSX_COMMENT alarm because this text in div element is recognized as a JSX child instead of a comment. */} +
+ ); + } +} + +import React from 'react'; + +class Foo extends React.Component { + render() { + return ( +
+ {this.props.items.length && `(${this.props.items.join(', ')})`} {/* BAD_LENGTH_CHECK alarm */} +
+ ); + } +} + +// Example 1 +import React from 'react'; + +class Hello extends React.Component { + render() { +
Hello
; // BAD_RENDER_RETURN_VALUE alarm because 'render()' does not return this React element. + } +} + +// Example 2 +class Hello2 extends React.Component { + render() { + if (!this.props.myProp) { + return true; // BAD_RENDER_RETURN_VALUE alarm because 'render()' should return only a React element, null, or false. + } + return
Hello {this.props.myProp}
; + } +} + +import React from 'react'; + +class Hello extends React.Component { + getTextStyle() { + return { color: 'blue' }; + } + + render() { + return ( +
+
Text in red
{/* BAD_STYLE_PROP alarm because it is a string value. */} +
Text in blue
{/* BAD_STYLE_PROP alarm because it is a function value. */} +
+ ); + } +} + +// Example 1 +React.createClass({ + render() { + return
Hello
; // BAD_UNKNOWN_PROP alarm + } +}); + +// Example 2 +function handleClick() {} + +React.createClass({ + render() { + return
Hello
; // BAD_UNKNOWN_PROP alarm + } +}); + +// Example 3 +React.createClass({ + render() { + return
Hello
; // BAD_UNKNOWN_PROP alarm + } +}); + +import React from 'react'; + +class SayHello extends React.Component { + constructor(props) { + super(props); + this.state = { name: "DeepScan" }; + } + render() { + this.setState({ name: this.state.name + " Hello"}); // BAD_UPDATE_STATE alarm because `render()` should be a pure function of props and state. + return
{this.state.name}
; + } +} + +import React from 'react'; + +class SayHello extends React.Component { + constructor(props) { + super(props); + this.handleChanged = this.handleChanged.bind(this); + } + handleChanged() { + this.state = { message: "Hello" }; // DIRECT_ASSIGN_TO_STATE alarm + alert(this.state.message); + } + render() { + return ( + + ); + } +} + +import React from 'react'; + +class Hello extends React.Component { + constructor(props) { + super(props); + this.state = { name: "John" }; + } + + handleClick() { + this.setState({ name: "Mary" }); // 'this' has undefined value. + } + + render() { + return ( +
{/* EVENT_HANDLER_INVALID_THIS alarm because 'this.handleClick' function is not bound with 'this'. */} + {this.state.name} +
+ ); + } +} + +import React from 'react'; + +class Hello extends React.Component { + render() { + var childs = this.props.greetings.map((greeting) =>
  • {greeting.name}
  • ); // MISSING_KEY_PROP alarm + + return ( + + ); + } +} + +import React from 'react'; +import PropTypes from 'prop-types'; + +class Hello extends React.Component { + componentWillmount() { // REACT_API_TYPO alarm because `componentWillMount` is a correct name of the lifecycle method. + this.state = { + greetName: this.props.greetName + }; + } + render() { + return (
    {this.state.greetName}
    ); + } +} + +Hello.PropTypes = { // REACT_API_TYPO alarm because `propTypes` is a correct name of the component's class. + greetName: PropTypes.string +}; + +import React from 'react'; +import PropTypes from 'prop-types'; + +class Hello extends React.Component { + render() { + return
    Hello, {this.props.name}
    ; + } +} + +Hello.propTypes = { + name: PropTypes.string, + age: PropTypes.number // USELESS_PROP_TYPES alarm because this property 'age' is not used. +}; From 61f299c834347d54346e43347b53bedededa362c Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Mon, 15 Jul 2019 11:41:52 +0900 Subject: [PATCH 03/13] Update react-rule.js --- react-rule.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/react-rule.js b/react-rule.js index c25ecb9..c9b488f 100644 --- a/react-rule.js +++ b/react-rule.js @@ -21,7 +21,6 @@ class Hello extends React.Component { } } -import React from 'react'; class Hello extends React.Component { render() { @@ -33,7 +32,6 @@ class Hello extends React.Component { } } -import React from 'react'; class Hello extends React.Component { render() { @@ -46,8 +44,6 @@ class Hello extends React.Component { } } -import React from 'react'; - class Foo extends React.Component { render() { return ( @@ -58,9 +54,6 @@ class Foo extends React.Component { } } -// Example 1 -import React from 'react'; - class Hello extends React.Component { render() {
    Hello
    ; // BAD_RENDER_RETURN_VALUE alarm because 'render()' does not return this React element. @@ -77,7 +70,6 @@ class Hello2 extends React.Component { } } -import React from 'react'; class Hello extends React.Component { getTextStyle() { @@ -117,7 +109,6 @@ React.createClass({ } }); -import React from 'react'; class SayHello extends React.Component { constructor(props) { @@ -130,8 +121,6 @@ class SayHello extends React.Component { } } -import React from 'react'; - class SayHello extends React.Component { constructor(props) { super(props); @@ -148,7 +137,6 @@ class SayHello extends React.Component { } } -import React from 'react'; class Hello extends React.Component { constructor(props) { @@ -169,7 +157,6 @@ class Hello extends React.Component { } } -import React from 'react'; class Hello extends React.Component { render() { @@ -183,7 +170,6 @@ class Hello extends React.Component { } } -import React from 'react'; import PropTypes from 'prop-types'; class Hello extends React.Component { @@ -201,9 +187,6 @@ Hello.PropTypes = { // REACT_API_TYPO alarm because `propTypes` is a correct nam greetName: PropTypes.string }; -import React from 'react'; -import PropTypes from 'prop-types'; - class Hello extends React.Component { render() { return
    Hello, {this.props.name}
    ; From 70570a58fa65d3bf254ca45e0aa999e191cd7a7b Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Mon, 15 Jul 2019 11:42:21 +0900 Subject: [PATCH 04/13] Update react-rule.js --- react-rule.js | 177 -------------------------------------------------- 1 file changed, 177 deletions(-) diff --git a/react-rule.js b/react-rule.js index c9b488f..8848038 100644 --- a/react-rule.js +++ b/react-rule.js @@ -20,180 +20,3 @@ class Hello extends React.Component { ); } } - - -class Hello extends React.Component { - render() { - return ( - false}> {/* BAD_EVENT_HANDLER_RETURN_FALSE alarm */} - foo.com - - ); - } -} - - -class Hello extends React.Component { - render() { - return ( -
    - /* This is a comment */ {/* BAD_JSX_COMMENT alarm because this text is recognized as a JSX child instead of a comment. */} -
    // is a double slash.
    {/* BAD_JSX_COMMENT alarm because this text in div element is recognized as a JSX child instead of a comment. */} -
    - ); - } -} - -class Foo extends React.Component { - render() { - return ( -
    - {this.props.items.length && `(${this.props.items.join(', ')})`} {/* BAD_LENGTH_CHECK alarm */} -
    - ); - } -} - -class Hello extends React.Component { - render() { -
    Hello
    ; // BAD_RENDER_RETURN_VALUE alarm because 'render()' does not return this React element. - } -} - -// Example 2 -class Hello2 extends React.Component { - render() { - if (!this.props.myProp) { - return true; // BAD_RENDER_RETURN_VALUE alarm because 'render()' should return only a React element, null, or false. - } - return
    Hello {this.props.myProp}
    ; - } -} - - -class Hello extends React.Component { - getTextStyle() { - return { color: 'blue' }; - } - - render() { - return ( -
    -
    Text in red
    {/* BAD_STYLE_PROP alarm because it is a string value. */} -
    Text in blue
    {/* BAD_STYLE_PROP alarm because it is a function value. */} -
    - ); - } -} - -// Example 1 -React.createClass({ - render() { - return
    Hello
    ; // BAD_UNKNOWN_PROP alarm - } -}); - -// Example 2 -function handleClick() {} - -React.createClass({ - render() { - return
    Hello
    ; // BAD_UNKNOWN_PROP alarm - } -}); - -// Example 3 -React.createClass({ - render() { - return
    Hello
    ; // BAD_UNKNOWN_PROP alarm - } -}); - - -class SayHello extends React.Component { - constructor(props) { - super(props); - this.state = { name: "DeepScan" }; - } - render() { - this.setState({ name: this.state.name + " Hello"}); // BAD_UPDATE_STATE alarm because `render()` should be a pure function of props and state. - return
    {this.state.name}
    ; - } -} - -class SayHello extends React.Component { - constructor(props) { - super(props); - this.handleChanged = this.handleChanged.bind(this); - } - handleChanged() { - this.state = { message: "Hello" }; // DIRECT_ASSIGN_TO_STATE alarm - alert(this.state.message); - } - render() { - return ( - - ); - } -} - - -class Hello extends React.Component { - constructor(props) { - super(props); - this.state = { name: "John" }; - } - - handleClick() { - this.setState({ name: "Mary" }); // 'this' has undefined value. - } - - render() { - return ( -
    {/* EVENT_HANDLER_INVALID_THIS alarm because 'this.handleClick' function is not bound with 'this'. */} - {this.state.name} -
    - ); - } -} - - -class Hello extends React.Component { - render() { - var childs = this.props.greetings.map((greeting) =>
  • {greeting.name}
  • ); // MISSING_KEY_PROP alarm - - return ( - - ); - } -} - -import PropTypes from 'prop-types'; - -class Hello extends React.Component { - componentWillmount() { // REACT_API_TYPO alarm because `componentWillMount` is a correct name of the lifecycle method. - this.state = { - greetName: this.props.greetName - }; - } - render() { - return (
    {this.state.greetName}
    ); - } -} - -Hello.PropTypes = { // REACT_API_TYPO alarm because `propTypes` is a correct name of the component's class. - greetName: PropTypes.string -}; - -class Hello extends React.Component { - render() { - return
    Hello, {this.props.name}
    ; - } -} - -Hello.propTypes = { - name: PropTypes.string, - age: PropTypes.number // USELESS_PROP_TYPES alarm because this property 'age' is not used. -}; From b610c38a31016f808982cf0b4adb72986d796198 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Mon, 19 Aug 2019 18:31:37 +0900 Subject: [PATCH 05/13] Update cwe.js --- cwe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwe.js b/cwe.js index bb09a12..808f620 100644 --- a/cwe.js +++ b/cwe.js @@ -60,7 +60,7 @@ } function CWE_670(x) { // STRAY_SEMICOLON - while (++x <= 10) ; + while (++x <= 100) ; { sum += x; } From d2615dfc26d811de892b8eea5152d09879f678bc Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Wed, 21 Aug 2019 12:03:11 +0900 Subject: [PATCH 06/13] Update cwe.js --- cwe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwe.js b/cwe.js index 808f620..bb09a12 100644 --- a/cwe.js +++ b/cwe.js @@ -60,7 +60,7 @@ } function CWE_670(x) { // STRAY_SEMICOLON - while (++x <= 100) ; + while (++x <= 10) ; { sum += x; } From 83b405237a9a345fe81640175599192dfbe7871c Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Wed, 21 Aug 2019 14:04:39 +0900 Subject: [PATCH 07/13] Update cwe.js --- cwe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwe.js b/cwe.js index bb09a12..808f620 100644 --- a/cwe.js +++ b/cwe.js @@ -60,7 +60,7 @@ } function CWE_670(x) { // STRAY_SEMICOLON - while (++x <= 10) ; + while (++x <= 100) ; { sum += x; } From 0a7211628537a1dcfd18cc4a0ce15a7505235b5d Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Mon, 23 Sep 2019 16:46:31 +0900 Subject: [PATCH 08/13] Create cwe.js --- cwe/cwe.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 cwe/cwe.js diff --git a/cwe/cwe.js b/cwe/cwe.js new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/cwe/cwe.js @@ -0,0 +1 @@ +test From 182ac12e39f001fb27d5ec2a1f5dd05fa1818453 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Thu, 10 Oct 2019 11:56:10 +0900 Subject: [PATCH 09/13] Update cwe.js --- cwe/cwe.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cwe/cwe.js b/cwe/cwe.js index 9daeafb..2f91ea7 100644 --- a/cwe/cwe.js +++ b/cwe/cwe.js @@ -1 +1,12 @@ -test +function CWE_398() { // IDENTICAL_BRANCHES + if (x >= 0) { + y = x; + } else { + y = x; + } +} +function CWE_476() { // NULL_POINTER + var obj; + var y = obj.x; // UNINITIALIZED_LOCAL_VAR + console.log(y); +} From 71056374bb695d0c6b7a3aba3fec420bcd5a4635 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Thu, 19 Dec 2019 11:12:39 +0900 Subject: [PATCH 10/13] Update cwe.js --- cwe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cwe.js b/cwe.js index 808f620..911ba95 100644 --- a/cwe.js +++ b/cwe.js @@ -60,7 +60,7 @@ } function CWE_670(x) { // STRAY_SEMICOLON - while (++x <= 100) ; + while (++x <= 100) { sum += x; } From d845e39372e645a97fdbae17abac83c6b8cdb820 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Fri, 30 Aug 2024 14:00:28 +0900 Subject: [PATCH 11/13] Update cwe.js --- cwe.js | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/cwe.js b/cwe.js index 911ba95..b97bc21 100644 --- a/cwe.js +++ b/cwe.js @@ -1,19 +1,5 @@ // This will show examples related with the Common Weakness Enumeration (CWE). - function CWE_398() { // IDENTICAL_BRANCHES - if (x >= 0) { - y = x; - } else { - y = x; - } - } - - function CWE_476() { // NULL_POINTER - var obj; - var y = obj.x; - console.log(y); - } - function CWE_480() { // BAD_BITWISE_OPERATOR var obj = null; if (obj & obj.prop) { From 2972f5bce3df4a912bde724783ee7eef140f6cdd Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Fri, 30 Aug 2024 16:56:01 +0900 Subject: [PATCH 12/13] Delete test0419.js --- test0419.js | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 test0419.js diff --git a/test0419.js b/test0419.js deleted file mode 100644 index 2f91ea7..0000000 --- a/test0419.js +++ /dev/null @@ -1,12 +0,0 @@ -function CWE_398() { // IDENTICAL_BRANCHES - if (x >= 0) { - y = x; - } else { - y = x; - } -} -function CWE_476() { // NULL_POINTER - var obj; - var y = obj.x; // UNINITIALIZED_LOCAL_VAR - console.log(y); -} From 52a6ae8398c54067306b3e6eea26d139825e2537 Mon Sep 17 00:00:00 2001 From: jihye1 <36979920+jihye1@users.noreply.github.com> Date: Fri, 30 Aug 2024 17:00:51 +0900 Subject: [PATCH 13/13] Create 20270830.js --- 20270830.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 20270830.js diff --git a/20270830.js b/20270830.js new file mode 100644 index 0000000..42f38ba --- /dev/null +++ b/20270830.js @@ -0,0 +1,21 @@ +function say(msg) { + return (() => { + console.log(msg); + return 'Done'; + }); +} +function doSomethingAfter1Sec(something) { + setTimeout(() => { + something(); + }, 1000); +} +async function asyncCall() { + console.log('Start'); + var result = await doSomethingAfter1Sec(say('Hi')); // AWAIT_NON_PROMISE alarm because 'doSomethingAfter1Sec(...)' is not Promise. + console.log('Result: ' + result); + console.log('End'); + + console.log('Start'); + await [1, 2, 3].map(x => doSomethingAfter1Sec(say(x))); // AWAIT_NON_PROMISE alarm because awaiting an array has no effect. + console.log('End'); +}