import { TSESLint } from '@typescript-eslint/experimental-utils' ; import { ruleTester } from '../rule-tester' ; import { IssueLocation } from '../../src/utils/locations' ; import rule = require ( '../../src/rules/no-identical-functions' ) ; ruleTester . run ( 'no-identical-functions' , rule , { valid : [ { code : `
      function foo() {
        console.log("Hello");
        console.log("World");
        return 42;
      }

      function bar() {
        if (x) {
          console.log("Hello");
        }
        return 42;
      }` , } , { code : `
      function foo() {
        console.log("Hello");
        return 42;
      }

      function bar() {
        console.log("Hello");
        return 42;
      }` , } , { code : `
      let foo = (a, b) => {
        [
          a,
          b
        ]
      }

      let bar = (a, b) => (
        [
          a,
          b
        ]
      )
      ` , } , ] , invalid : [ { code : `
      function foo() {
        console.log("Hello");
        console.log("World");
        return 42;
      }

      function bar() {
        console.log("Hello");
        console.log("World");
        return 42;
      }` , errors : [ message ( 2 , 8 ) ] , } , { code : `
      function foo() {
        console.log("Hello");
        console.log("World");
        return 42;
      }

      let funcExpression = function () { // Noncompliant
        console.log("Hello");
        console.log("World");
        return 42;
      }

      let arrowFunction = () => { // Noncompliant
        console.log("Hello");
        console.log("World");
        return 42;
      }

      class A {
        constructor() { // Noncompliant
          console.log("Hello");
          console.log("World");
          return 42;
        }

        method() { // Noncompliant
          console.log("Hello");
          console.log("World");
          return 42;
        }

        set setter(p) { // Noncompliant
          console.log("Hello");
          console.log("World");
          return 42;
        }

        get getter() { // Noncompliant
          console.log("Hello");
          console.log("World");
          return 42;
        }
      }

      async function asyncFunction() { // Noncompliant
        console.log("Hello");
        console.log("World");
        return 42;
      }

      let asyncExpression = async function () { // Noncompliant
        console.log("Hello");
        console.log("World");
        return 42;
      }` , errors : [ message ( 2 , 8 ) , message ( 2 , 14 ) , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 21 , column : 9 , endColumn : 20 , } , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 27 , column : 9 , endColumn : 15 , } , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 33 , column : 13 , endColumn : 19 , } , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 39 , column : 13 , endColumn : 19 , } , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 46 , column : 22 , endColumn : 35 , } , { messageId : 'identicalFunctions' , data : { line : 2 , } , line : 52 , column : 35 , endColumn : 43 , } , ] , } , { code : `
      let foo = () => (
        [
          1,
        ]
      )

      let bar = () => (
        [
          1,
        ]
      )` , errors : [ message ( 2 , 8 ) ] , } , { code : `
      function foo1() {
        return [
          1,
        ];
      }

      function bar1() { // Noncompliant
        return [
          1,
        ];
      }` , errors : [ message ( 2 , 8 ) ] , } , { code : `
      const x = {
        data: function () {
          return {
            a: 2
          }
        }
      }

      const y = {
        data: function () {
          return {
            a: 2
          }
        }
      }` , errors : [ message ( 3 , 11 ) ] , } , { code : `
      const x = {
        foo() {
          console.log("Hello");
          console.log("World");
          return 42;
        },

        bar() {
          console.log("Hello");
          console.log("World");
          return 42;
        },
      };` , errors : [ { messageId : 'identicalFunctions' , data : { line : 3 , } , line : 9 , column : 9 , endColumn : 12 , } , ] , } , { code : `
      const x = {
        foo() {
      //^^^>
          console.log("Hello");
          console.log("World");
          return 42;
        },

        bar() {
      //^^^
          console.log("Hello");
          console.log("World");
          return 42;
        },
      };` , options : [ 'sonar-runtime' ] , errors : [ encodedMessage ( 3 , 10 , [ { line : 3 , column : 8 , endLine : 3 , endColumn : 11 , message : 'Original implementation' } , ] ) , ] , } , { code : `
      function foo1() {
        //     ^^^^>
        return [
          1,
        ];
      }
      function bar1() {
    //         ^^^^
        return [
          1,
        ];
      }` , options : [ 'sonar-runtime' ] , errors : [ encodedMessage ( 2 , 8 , [ { line : 2 , column : 15 , endLine : 2 , endColumn : 19 , message : 'Original implementation' } , ] ) , ] , } , ] , } ) ; function message ( originalLine , duplicationLine ) { return { messageId : 'identicalFunctions' , data : { line : originalLine , } , line : duplicationLine , endLine : duplicationLine , } ; } function encodedMessage ( originalLine , duplicationLine , secondaries ) { return { messageId : 'sonarRuntime' , data : { line : originalLine , sonarRuntimeData : JSON . stringify ( { secondaryLocations : secondaries , message : `Update this function so that its implementation is not identical to the one on line ${ originalLine }.` , } ) , } , line : duplicationLine , endLine : duplicationLine , } ; }