MCPcopy Create free account
hub / github.com/microsoft/typescript-go / scanTemplateAndSetTokenValue

Method scanTemplateAndSetTokenValue

internal/scanner/scanner.go:1638–1688  ·  view source on GitHub ↗
(shouldEmitInvalidEscapeError bool)

Source from the content-addressed store, hash-verified

1636}
1637
1638func (s *Scanner) scanTemplateAndSetTokenValue(shouldEmitInvalidEscapeError bool) ast.Kind {
1639 startedWithBacktick := s.char() == '`'
1640 s.pos++
1641 start := s.pos
1642 parts := make([]string, 0, 4)
1643 var token ast.Kind
1644 for {
1645 s.scanASCIIWhile(func(b byte) bool {
1646 return b != '`' && b != '$' && b != '\\' && b != '\r'
1647 })
1648 ch := s.char()
1649 if ch < 0 || ch == '`' {
1650 parts = append(parts, s.text[start:s.pos])
1651 if ch == '`' {
1652 s.pos++
1653 } else {
1654 s.tokenFlags |= ast.TokenFlagsUnterminated
1655 s.error(diagnostics.Unterminated_template_literal)
1656 }
1657 token = core.IfElse(startedWithBacktick, ast.KindNoSubstitutionTemplateLiteral, ast.KindTemplateTail)
1658 break
1659 }
1660 if ch == '$' && s.charAt(1) == '{' {
1661 parts = append(parts, s.text[start:s.pos])
1662 s.pos += 2
1663 token = core.IfElse(startedWithBacktick, ast.KindTemplateHead, ast.KindTemplateMiddle)
1664 break
1665 }
1666 if ch == '\\' {
1667 parts = append(parts, s.text[start:s.pos])
1668 parts = append(parts, s.scanEscapeSequence(EscapeSequenceScanningFlagsString|core.IfElse(shouldEmitInvalidEscapeError, EscapeSequenceScanningFlagsReportErrors, 0)))
1669 start = s.pos
1670 continue
1671 }
1672 // Speculated ECMAScript 6 Spec 11.8.6.1:
1673 // <CR><LF> and <CR> LineTerminatorSequences are normalized to <LF> for Template Values
1674 if ch == '\r' {
1675 parts = append(parts, s.text[start:s.pos])
1676 s.pos++
1677 if s.char() == '\n' {
1678 s.pos++
1679 }
1680 parts = append(parts, "\n")
1681 start = s.pos
1682 continue
1683 }
1684 s.pos++
1685 }
1686 s.tokenValue = strings.Join(parts, "")
1687 return token
1688}
1689
1690func (s *Scanner) scanEscapeSequence(flags EscapeSequenceScanningFlags) string {
1691 start := s.pos

Callers 2

ScanMethod · 0.95
ReScanTemplateTokenMethod · 0.95

Calls 8

charMethod · 0.95
scanASCIIWhileMethod · 0.95
errorMethod · 0.95
charAtMethod · 0.95
scanEscapeSequenceMethod · 0.95
IfElseFunction · 0.92
makeFunction · 0.50
appendFunction · 0.50

Tested by

no test coverage detected