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

Method scanStartOfDeclaration

internal/parser/parser.go:6100–6172  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

6098}
6099
6100func (p *Parser) scanStartOfDeclaration() bool {
6101 for {
6102 switch p.token {
6103 case ast.KindVarKeyword, ast.KindLetKeyword, ast.KindConstKeyword, ast.KindFunctionKeyword, ast.KindClassKeyword,
6104 ast.KindEnumKeyword:
6105 return true
6106 case ast.KindUsingKeyword:
6107 return p.isUsingDeclaration()
6108 case ast.KindAwaitKeyword:
6109 return p.isAwaitUsingDeclaration()
6110 // 'declare', 'module', 'namespace', 'interface'* and 'type' are all legal JavaScript identifiers;
6111 // however, an identifier cannot be followed by another identifier on the same line. This is what we
6112 // count on to parse out the respective declarations. For instance, we exploit this to say that
6113 //
6114 // namespace n
6115 //
6116 // can be none other than the beginning of a namespace declaration, but need to respect that JavaScript sees
6117 //
6118 // namespace
6119 // n
6120 //
6121 // as the identifier 'namespace' on one line followed by the identifier 'n' on another.
6122 // We need to look one token ahead to see if it permissible to try parsing a declaration.
6123 //
6124 // *Note*: 'interface' is actually a strict mode reserved word. So while
6125 //
6126 // "use strict"
6127 // interface
6128 // I {}
6129 //
6130 // could be legal, it would add complexity for very little gain.
6131 case ast.KindInterfaceKeyword, ast.KindTypeKeyword, ast.KindDeferKeyword:
6132 return p.nextTokenIsIdentifierOnSameLine()
6133 case ast.KindModuleKeyword, ast.KindNamespaceKeyword:
6134 return p.nextTokenIsIdentifierOrStringLiteralOnSameLine()
6135 case ast.KindAbstractKeyword, ast.KindAccessorKeyword, ast.KindAsyncKeyword, ast.KindDeclareKeyword, ast.KindPrivateKeyword,
6136 ast.KindProtectedKeyword, ast.KindPublicKeyword, ast.KindReadonlyKeyword:
6137 previousToken := p.token
6138 p.nextToken()
6139 // ASI takes effect for this modifier.
6140 if p.hasPrecedingLineBreak() {
6141 return false
6142 }
6143 if previousToken == ast.KindDeclareKeyword && p.token == ast.KindTypeKeyword {
6144 // If we see 'declare type', then commit to parsing a type alias. parseTypeAliasDeclaration will
6145 // report Line_break_not_permitted_here if needed.
6146 return true
6147 }
6148 continue
6149 case ast.KindGlobalKeyword:
6150 p.nextToken()
6151 return p.token == ast.KindOpenBraceToken || p.token == ast.KindIdentifier || p.token == ast.KindExportKeyword
6152 case ast.KindImportKeyword:
6153 p.nextToken()
6154 return p.token == ast.KindDeferKeyword || p.token == ast.KindStringLiteral || p.token == ast.KindAsteriskToken || p.token == ast.KindOpenBraceToken || tokenIsIdentifierOrKeyword(p.token)
6155 case ast.KindExportKeyword:
6156 p.nextToken()
6157 if p.token == ast.KindEqualsToken || p.token == ast.KindAsteriskToken || p.token == ast.KindOpenBraceToken ||

Callers

nothing calls this directly

Tested by

no test coverage detected