Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/js/shared/str.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function removeNewLinesAndExtraSpaces(str: any): any;
export function randomAlphanumeric(length: number): string;
export function toFixedLocale(number: any, precision: any): any;
/**
* @summary Removes lines started with # character. Shebang (#!) is excluded.
* @summary Removes comments from a given source code text based on the specified programming language.
* @param text {String} text to remove comments from.
* @param language {String} programming language of the text.
* @return {String}
Expand Down
9 changes: 6 additions & 3 deletions dist/js/shared/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ function toFixedLocale(number, precision) {
}
exports.toFixedLocale = toFixedLocale;
/**
* @summary Removes lines started with # character. Shebang (#!) is excluded.
* @summary Removes comments from a given source code text based on the specified programming language.
* @param text {String} text to remove comments from.
* @param language {String} programming language of the text.
* @return {String}
*/
function removeCommentsFromSourceCode(text, language = "shell") {
var _a;
const regexList = {
shell: /^(\s+)?#(?!!).*$/gm,
shell: /#(?!!).*$/gm,
fortran: /!.*$/gm,
python: /#.*$/gm,
};
return text.replace(regexList[language], "");
return text.replace((_a = regexList[language]) !== null && _a !== void 0 ? _a : regexList.shell, "");
}
exports.removeCommentsFromSourceCode = removeCommentsFromSourceCode;
/**
Expand Down
8 changes: 5 additions & 3 deletions src/js/shared/str.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ export function toFixedLocale(number, precision) {
}

/**
* @summary Removes lines started with # character. Shebang (#!) is excluded.
* @summary Removes comments from a given source code text based on the specified programming language.
* @param text {String} text to remove comments from.
* @param language {String} programming language of the text.
* @return {String}
*/
export function removeCommentsFromSourceCode(text, language = "shell") {
const regexList = {
shell: /^(\s+)?#(?!!).*$/gm,
shell: /#(?!!).*$/gm,
fortran: /!.*$/gm,
python: /#.*$/gm,
};
return text.replace(regexList[language], "");
return text.replace(regexList[language] ?? regexList.shell, "");
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/py/mat3ra/utils/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ def snake_to_camel(snake_case_str: str) -> str:


def remove_comments_from_source_code(text: str, language: str = "shell") -> str:
"""Removes lines starting with # (except shebang)."""
return re.sub(r"^(\s+)?#(?!!).*$", "", text, flags=re.MULTILINE)
"""Removes comments from source code based on the language.
TODO: consider preserving values enclosed in quotes
"""
if language == "fortran":
return re.sub(r"!.*$", "", text, flags=re.MULTILINE)
if language == "python":
return re.sub(r"#.*$", "", text, flags=re.MULTILINE)
return re.sub(r"#(?!!).*$", "", text, flags=re.MULTILINE)


def remove_empty_lines_from_string(text: str) -> str:
Expand Down
7 changes: 2 additions & 5 deletions tests/py/unit/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ class Model(BaseModel):


def test_remove_timestampable_keys():
assert remove_timestampable_keys(
{"a": 1, "createdAt": "x", "updatedAt": "y", "removedAt": "z"}
) == {"a": 1}
assert remove_timestampable_keys({"a": 1, "createdAt": "x", "updatedAt": "y", "removedAt": "z"}) == {"a": 1}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the linter on auto-save that wasn't applied before, while should have



def test_comment_and_empty_line_stripping_matches_js():
text = "# comment\n\nx=1\n # indented\n#!/bin/bash\n echo hi # inline\n"
without_comments = remove_comments_from_source_code(text)
assert "#!/" in without_comments # shebang preserved
assert "echo hi # inline" in without_comments # inline comment preserved
assert "comment" not in without_comments

assert remove_empty_lines_from_string(without_comments) == "x=1\n#!/bin/bash\n echo hi # inline"
assert remove_empty_lines_from_string(without_comments) == "x=1\n#!/bin/bash\n echo hi"
Loading