Skip to content

Commit

Permalink
feat: remove comments from python code (#103)
Browse files Browse the repository at this point in the history
* feat: remove comments from python code

* add tests

---------

Co-authored-by: Naomi Carrigan <[email protected]>
  • Loading branch information
ShaunSHamilton and Naomi Carrigan authored Dec 11, 2023
1 parent c67c0cc commit f3996cd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/__tests__/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ def c():
}
});

it("removeComments", () => {
const code = `
a = 1
# comment
def b(d, e):
a = 2
# comment
return a #comment
`;
const result = python.removeComments(code);
expect(result).toEqual(`
a = 1
def b(d, e):
a = 2
return a
`);
});

it("getBlock", () => {
const code = `
a = 1
Expand Down
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export module python {
return null;
}

export function removeComments(code: string) {
return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(#.*$)/gm, "");
}

/**
* Gets a Python block of code matching the `blockPattern`
* @param code - Code string to search
Expand Down

0 comments on commit f3996cd

Please sign in to comment.