Class: CodeMask

lib/bundler/scanner.CodeMask(source)

A lookup answering "is this offset inside a string, comment or literal?".

Built once per module and consulted many times. A flat Uint8Array costs one byte per source character and turns every query into an array read, which matters because the export scanner asks the question for every identifier in a module.

Constructor

new CodeMask(source)

Parameters:
Name Type Description
source string

The source the mask describes.

Source:

Members

mask :Uint8Array

Type:
  • Uint8Array
Source:

regions :Array:.<Region:>

Type:
  • Array:.<Region:>
Source:

source :string

Type:
  • string
Source:

Methods

isCode(index) → {boolean}

Whether an offset holds executable code rather than literal or comment text.

Parameters:
Name Type Description
index number

The offset to test.

Source:
Returns:

True when the offset is code.

Type
boolean

isLiteral(index) → {boolean}

Whether an offset sits inside a literal whose text is data.

Distinct from CodeMask#isCode: a comment is not code, but its whitespace is still safe to remove, whereas a string or template literal carries its spacing as a value. Anything that rewrites whitespace has to tell those two apart.

Parameters:
Name Type Description
index number

The offset to test.

Source:
Returns:

True inside a string, template or regex literal.

Type
boolean

withoutComments() → {string}

Returns the source with every comment replaced by equivalent whitespace.

Offsets are preserved, so a position found in the blanked text is valid in the original. String and template contents are left alone: they are values, not noise, and blanking them would corrupt the module.

Source:
Returns:

The comment-free source, same length as the original.

Type
string