Giter Site home page Giter Site logo

Comments (2)

fredemmott avatar fredemmott commented on July 23, 2024

Here's the basics of just assertEquals - doesn't handle whitespace, comments, generating nice code etc, but gets the semantics right:

<?hh // strict
/*
 *  Copyright (c) 2017-present, Facebook, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the MIT license found in the
 *  LICENSE file in the root directory of this source tree.
 *
 */

namespace Facebook\HHAST\Linters;

use type Facebook\HHAST\{
  EditableNode,
  EditableList,
  FunctionCallExpression,
  BinaryExpression,
  MemberSelectionExpression,
  LeftParenToken,
  RightParenToken,
  NameToken,
  EqualToken,
  DelimitedComment,
  ListItem,
  CommaToken,
  WhiteSpace,
};
use function Facebook\HHAST\Missing;
use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Str, Vec};

final class NoAssertEqualsLinter
  extends AutoFixingASTLinter<FunctionCallExpression> {

  <<__Override>>
  protected static function getTargetType(): classname<FunctionCallExpression> {
    return FunctionCallExpression::class;
  }

  <<__Override>>
  public function getLintErrorForNode(
    FunctionCallExpression $node,
    vec<EditableNode> $parents,
  ): ?FixableASTLintError<FunctionCallExpression> {
    $rec = $node->getReceiver();
    if (!$rec instanceof MemberSelectionExpression) {
      return null;
    }
    $method = $rec->getName()->getCode();
    if ($method !== 'assertEquals') {
      return null;
    }

    return new FixableASTLintError($this, "use fbexpect", $node);
  }

  <<__Override>>
  public function getFixedNode(
    FunctionCallExpression $node,
  ): FunctionCallExpression {
    $params = vec($node->getArgumentListx()->getChildren());
    $expected = $params[0];
    $actual = $params[1];
    $message = Vec\drop($params, 2);

    $rec = $node->getReceiver();
    invariant(
      $rec instanceof MemberSelectionExpression,
      'receiver should be a member selection expression',
    );

    return $node
      ->withReceiver(
        $rec->withObject(
          new FunctionCallExpression(
            new NameToken(
              $rec->getObject()->getFirstTokenx()->getLeading(),
              Missing(),
              'expect',
            ),
            new LeftParenToken(Missing(), Missing()),
            new EditableList(vec[$actual]),
            new RightParenToken(Missing(), Missing()),
          ),
        )
          ->withName(new NameToken(Missing(), Missing(), 'toBeSame')),
      )
      ->withArgumentList(
        new EditableList(Vec\concat(vec[$expected], $message)),
      );
  }
}

from hhast.

fredemmott avatar fredemmott commented on July 23, 2024

Done in that PR

from hhast.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.