```ts
export function stripComments(source: string): string {
  let inString = false;
  let inBlockComment = false;
  let result = '';
  let currentLine = '';

  for (let i = 0; i < source.length; i++) {
    const char = source[i];

    if (inString) {
      if (char === '"' && source[i - 1] !== '\\') {
        inString = false;
      }
      currentLine += char;
    } else if (inBlockComment) {
      if (char === '*' && i + 1 < source.length && source[i + 1] === '/') {
        inBlockComment = false;
        i++; // Skip the '/' character
      }
    } else {
      if (char === '"') {
        inString = true;
        currentLine += char;
      } else if (char === '/' && i + 1 < source.length) {
        if (source[i + 1] === '/') {
          break; // End of line, skip the rest
        } else if (source[i + 1] === '*') {
          inBlockComment = true;
          i++; // Skip the '*' character
        } else {
          currentLine += char;
        }
      } else {
        currentLine += char;
      }
    }

    if (char === '\n' && !inString) {
      result += currentLine.trimEnd() + '\n';
      currentLine = '';
    }
  }

  if (inBlockComment) {
    throw new Error('Unclosed block comment');
  }
  if (inString) {
    throw new Error('Unclosed string literal');
  }

  return result + currentLine.trimEnd();
}
```