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

Method scanString

internal/scanner/scanner.go:1586–1636  ·  view source on GitHub ↗
(jsxAttributeString bool)

Source from the content-addressed store, hash-verified

1584}
1585
1586func (s *Scanner) scanString(jsxAttributeString bool) string {
1587 quote := s.char()
1588 if quote == '\'' {
1589 s.tokenFlags |= ast.TokenFlagsSingleQuote
1590 }
1591 s.pos++
1592 // Fast path for simple strings without escape sequences.
1593 strLen := strings.IndexByte(s.text[s.pos:], byte(quote))
1594 if strLen == 0 {
1595 s.pos++
1596 return ""
1597 }
1598 if strLen > 0 {
1599 str := s.text[s.pos : s.pos+strLen]
1600 if jsxAttributeString ||
1601 strings.IndexByte(str, '\\') < 0 && strings.IndexByte(str, '\r') < 0 && strings.IndexByte(str, '\n') < 0 {
1602 s.pos += strLen + 1
1603 return str
1604 }
1605 }
1606 var sb strings.Builder
1607 start := s.pos
1608 for {
1609 ch := s.char()
1610 if ch < 0 {
1611 sb.WriteString(s.text[start:s.pos])
1612 s.tokenFlags |= ast.TokenFlagsUnterminated
1613 s.error(diagnostics.Unterminated_string_literal)
1614 break
1615 }
1616 if ch == quote {
1617 sb.WriteString(s.text[start:s.pos])
1618 s.pos++
1619 break
1620 }
1621 if ch == '\\' && !jsxAttributeString {
1622 sb.WriteString(s.text[start:s.pos])
1623 sb.WriteString(s.scanEscapeSequence(EscapeSequenceScanningFlagsString | EscapeSequenceScanningFlagsReportErrors))
1624 start = s.pos
1625 continue
1626 }
1627 if (ch == '\n' || ch == '\r') && !jsxAttributeString {
1628 sb.WriteString(s.text[start:s.pos])
1629 s.tokenFlags |= ast.TokenFlagsUnterminated
1630 s.error(diagnostics.Unterminated_string_literal)
1631 break
1632 }
1633 s.pos++
1634 }
1635 return sb.String()
1636}
1637
1638func (s *Scanner) scanTemplateAndSetTokenValue(shouldEmitInvalidEscapeError bool) ast.Kind {
1639 startedWithBacktick := s.char() == '`'

Callers 2

ScanMethod · 0.95
ScanJsxAttributeValueMethod · 0.95

Calls 4

charMethod · 0.95
errorMethod · 0.95
scanEscapeSequenceMethod · 0.95
StringMethod · 0.65

Tested by

no test coverage detected