MLH-Fellowship / pyre-check

Performant type-checking for python.
https://pyre-check.org/
MIT License
3 stars 1 forks source link

[Fall 2021] Step 3: VSCode Plugin - right click to generate model #7

Open gbleaney opened 3 years ago

gbleaney commented 3 years ago

When Pysa developers are working on a rule, they often look at a function in code such as:

def foo(arg):
   pass

If they want to write a model for that function, they need to figure out the fully qualified name for that function, so that they can write a model like this:

def module.file.foo(arg) -> TaintSource[SomeSource]: ...

It would be really convenient if they could just right click on the function itself, and auto-generate the model. Ideally, if a user right clicked on foo and chose "Generate Pysa Model", this would be placed on their clipboard:

def module.file.foo(arg): ...

They could then paste that in some .pysa file, and add their TaintSource/TaintSink annotations.

To implement this you'll probably need to:

  1. Register a custom right click menu: https://code.visualstudio.com/api/references/contribution-points#contributes.menus
  2. Have that menu trigger the generation of a model
    1. Get the function name and file
    2. Query Pyre to get information on the file. Eg. pyre query "types('module/file.py')" | jq will give something like this:
      ...
      {
        "location": {
          "start": {
            "line": 11,
            "column": 8
          },
          "stop": {
            "line": 11,
            "column": 16
          }
        },
        "annotation": "typing.Callable(module.Class.__init__)[[Named(self, module.Class), Named(environ, typing.Dict[str, typing.Any])], None]"
      },
      ...
    3. In there you can see module.Class.__init__ is the fully qualified name of the function I was looking for. You'll need to extract that. It might be worth modifying pyre query to have a more specific API you can query so you don't have to do the extraction within the plugin itself.
    4. Create a Pysa model using the fully qualified function name
  3. Put the model on to the user's clipboard - https://code.visualstudio.com/api/references/vscode-api#Clipboard
  4. Show a little message saying that you've put the model on the clipboard - search showInformationMessage here: https://code.visualstudio.com/api/references/vscode-api
r0rshark commented 3 years ago

We can keep this work for the upcoming MLH round

gracewgao commented 3 years ago

I'd be happy to work on this if no one else is working on it yet!