Skip to content

Commit

Permalink
Single line comments (#74)
Browse files Browse the repository at this point in the history
* feat: added $capabilities() function

* fix: package lock

* feat: support single line comments and comments inside strings

* fix: remove console.log

* fix: exclude double slashes that are part of a url
  • Loading branch information
mechanik-daniel authored May 29, 2024
1 parent 2a50f21 commit e890262
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/helpers/parser/removeComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
* Project name: FUME-COMMUNITY
*/

const isUrlPart = (charIndex: number, expr: string): boolean => {
// the minimum index for a url's // part is after the 'http(s):' part
// so undex lower than 7 means it's a comment and not a url
if (charIndex < 7) return false;
const prevSevenChars: string = expr.substring(charIndex - 7, charIndex);
const prevSixChars: string = prevSevenChars.substring(1);
if (
['https:', '[https:'].includes(prevSevenChars.trimStart()) || ['http:', '[http:'].includes(prevSixChars.trimStart())
) {
return true;
} else {
return false;
}
};

export const removeComments = (expr: string): string => {
const exprLen: number = expr.length;
if (exprLen === 0) return expr;
Expand Down Expand Up @@ -52,7 +67,8 @@ export const removeComments = (expr: string): string => {
} else {
// not a quote sign
const twoChars: string = currentChar + nextChar;
if ((twoChars === '/*' || twoChars === '//') && openedQuote === '') {
const notUrl: boolean = !isUrlPart(i, expr);
if (openedQuote === '' && (twoChars === '/*' || (twoChars === '//' && notUrl))) {
// opening comment, not inside quotes
openedComment = twoChars;
continue;
Expand Down

0 comments on commit e890262

Please sign in to comment.