semgrep / pfff

pfff is mainly an OCaml API to write static analysis, dynamic analysis, code visualizations, code navigations, or style-preserving source-to-source transformations such as refactorings on source code.
https://semgrep.dev
Other
186 stars 29 forks source link

allowed decorators in method patterns #542

Closed brandonspark closed 2 years ago

brandonspark commented 2 years ago

I added parsing for patterns which look like methods (with body and without body) that have decorators.

Test plan: @$DECORATOR('...') $NAME(): string parses as

  PartialDef(
    ({
      name=EN(
             Id(("$NAME", ()),
               {id_info_id=1; id_hidden=false; id_resolved=Ref(None);
                id_type=Ref(None); id_svalue=Ref(None); }));
      attrs=[NamedAttr((),
               Id(("$DECORATOR", ()),
                 {id_info_id=2; id_hidden=false; id_resolved=Ref(None);
                  id_type=Ref(None); id_svalue=Ref(None); }),
               [Arg(L(String(("...", ()))))])];
      tparams=[]; },
     FuncDef(
       {fkind=(Method, ()); fparams=[];
        frettype=Some({t_attrs=[];
                       t=TyN(
                           Id(("string", ()),
                             {id_info_id=3; id_hidden=false;
                              id_resolved=Ref(None); id_type=Ref(None);
                              id_svalue=Ref(None); }));
                       });
        fbody=FBStmt(Block([])); }))))

and

@$DECORATOR $NAME(): string { } parses as

Fld(
  F(
    DefStmt(
      ({
        name=EN(
               Id(("$NAME", ()),
                 {id_info_id=1; id_hidden=false; id_resolved=Ref(None);
                  id_type=Ref(None); id_svalue=Ref(None); }));
        attrs=[NamedAttr((),
                 Id(("$DECORATOR", ()),
                   {id_info_id=2; id_hidden=false; id_resolved=Ref(None);
                    id_type=Ref(None); id_svalue=Ref(None); }), [])];
        tparams=[]; },
       FuncDef(
         {fkind=(Method, ()); fparams=[];
          frettype=Some({t_attrs=[];
                         t=TyN(
                             Id(("string", ()),
                               {id_info_id=3; id_hidden=false;
                                id_resolved=Ref(None); id_type=Ref(None);
                                id_svalue=Ref(None); }));
                         });
          fbody=FBStmt(Block([])); })))))

Security