From 08ef8d661f60e86a7b5c86778514095d5f726756 Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Fri, 8 Dec 2023 10:14:53 +0200 Subject: [PATCH 1/2] feat: remove comments from python code --- lib/index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/index.ts b/lib/index.ts index 266682a..33b1174 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -123,6 +123,10 @@ export module python { return null; } + + export function removeComments(code: string) { + return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(\#.*$)/gm, ""); + } } export class CSSHelp { From d82504fd8b2f4f43f7cebae8176cda49e18ecd3d Mon Sep 17 00:00:00 2001 From: Shaun Hamilton Date: Fri, 8 Dec 2023 10:21:57 +0200 Subject: [PATCH 2/2] add tests --- lib/__tests__/python.test.ts | 20 ++++++++++++++++++++ lib/index.ts | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/__tests__/python.test.ts b/lib/__tests__/python.test.ts index d3f5aef..e56292d 100644 --- a/lib/__tests__/python.test.ts +++ b/lib/__tests__/python.test.ts @@ -24,4 +24,24 @@ def c(): expect(function_parameters).toEqual("d, e"); } }); + + 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 +`); + }); }); diff --git a/lib/index.ts b/lib/index.ts index 33b1174..cc7fda6 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -125,7 +125,7 @@ export module python { } export function removeComments(code: string) { - return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(\#.*$)/gm, ""); + return code.replace(/\/\/.*|\/\*[\s\S]*?\*\/|(#.*$)/gm, ""); } }