@@ -1895,12 +1895,9 @@ namespace ts {
1895
1895
1896
1896
for ( const node of file . statements ) {
1897
1897
collectModuleReferences ( node , /*inAmbientModule*/ false ) ;
1898
- if ( ( file . flags & NodeFlags . PossiblyContainsDynamicImport ) || isJavaScriptFile ) {
1899
- collectDynamicImportOrRequireCalls ( node ) ;
1900
- }
1901
1898
}
1902
1899
if ( ( file . flags & NodeFlags . PossiblyContainsDynamicImport ) || isJavaScriptFile ) {
1903
- collectDynamicImportOrRequireCalls ( file . endOfFileToken ) ;
1900
+ collectDynamicImportOrRequireCalls ( file ) ;
1904
1901
}
1905
1902
1906
1903
file . imports = imports || emptyArray ;
@@ -1952,25 +1949,38 @@ namespace ts {
1952
1949
}
1953
1950
}
1954
1951
1955
- function collectDynamicImportOrRequireCalls ( node : Node ) : void {
1956
- if ( isRequireCall ( node , /*checkArgumentIsStringLiteralLike*/ true ) ) {
1957
- imports = append ( imports , node . arguments [ 0 ] ) ;
1958
- }
1959
- // we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
1960
- else if ( isImportCall ( node ) && node . arguments . length === 1 && isStringLiteralLike ( node . arguments [ 0 ] ) ) {
1961
- imports = append ( imports , node . arguments [ 0 ] as StringLiteralLike ) ;
1962
- }
1963
- else if ( isLiteralImportTypeNode ( node ) ) {
1964
- imports = append ( imports , node . argument . literal ) ;
1965
- }
1966
- collectDynamicImportOrRequireCallsForEachChild ( node ) ;
1967
- if ( hasJSDocNodes ( node ) ) {
1968
- forEach ( node . jsDoc , collectDynamicImportOrRequireCallsForEachChild ) ;
1952
+ function collectDynamicImportOrRequireCalls ( file : SourceFile ) {
1953
+ const r = / i m p o r t | r e q u i r e / g ;
1954
+ while ( r . exec ( file . text ) !== null ) {
1955
+ const node = getTokenAtPosition ( file , r . lastIndex ) ;
1956
+ if ( isRequireCall ( node , /*checkArgumentIsStringLiteralLike*/ true ) ) {
1957
+ imports = append ( imports , node . arguments [ 0 ] ) ;
1958
+ }
1959
+ // we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
1960
+ else if ( isImportCall ( node ) && node . arguments . length === 1 && isStringLiteralLike ( node . arguments [ 0 ] ) ) {
1961
+ imports = append ( imports , node . arguments [ 0 ] as StringLiteralLike ) ;
1962
+ }
1963
+ else if ( isLiteralImportTypeNode ( node ) ) {
1964
+ imports = append ( imports , node . argument . literal ) ;
1965
+ }
1969
1966
}
1970
1967
}
1971
1968
1972
- function collectDynamicImportOrRequireCallsForEachChild ( node : Node ) {
1973
- forEachChild ( node , collectDynamicImportOrRequireCalls ) ;
1969
+ /** Returns a token if position is in [start-of-leading-trivia, end) */
1970
+ function getTokenAtPosition ( sourceFile : SourceFile , position : number ) : Node {
1971
+ let current : Node = sourceFile ;
1972
+ const getContainingChild = ( child : Node ) => {
1973
+ if ( child . pos <= position && ( position < child . end || ( position === child . end && ( child . kind === SyntaxKind . EndOfFileToken ) ) ) ) {
1974
+ return child ;
1975
+ }
1976
+ } ;
1977
+ while ( true ) {
1978
+ const child = hasJSDocNodes ( current ) && forEach ( current . jsDoc , getContainingChild ) || forEachChild ( current , getContainingChild ) ;
1979
+ if ( ! child ) {
1980
+ return current ;
1981
+ }
1982
+ current = child ;
1983
+ }
1974
1984
}
1975
1985
}
1976
1986
0 commit comments