@@ -1932,7 +1932,7 @@ function minifyGlobals(ast) {
1932
1932
1933
1933
// Utilities
1934
1934
1935
- function reattachComments ( ast , comments ) {
1935
+ function reattachComments ( ast , commentsMap ) {
1936
1936
const symbols = [ ] ;
1937
1937
1938
1938
// Collect all code symbols
@@ -1949,33 +1949,37 @@ function reattachComments(ast, comments) {
1949
1949
1950
1950
// Walk through all comments in ascending line number, and match each
1951
1951
// comment to the appropriate code block.
1952
- for ( let i = 0 , j = 0 ; i < comments . length ; ++ i ) {
1953
- while ( j < symbols . length && symbols [ j ] . start . pos < comments [ i ] . end ) {
1952
+ let j = 0 ;
1953
+ for ( const [ pos , comments ] of Object . entries ( commentsMap ) ) {
1954
+ while ( j < symbols . length && symbols [ j ] . start . pos < pos ) {
1954
1955
++ j ;
1955
1956
}
1956
1957
if ( j >= symbols . length ) {
1957
- trace ( ` dropping comment : no symbol comes after it ( ${ comments [ i ] . value . slice ( 0 , 30 ) } )` ) ;
1958
+ trace ( ' dropping comments : no symbol comes after them' ) ;
1958
1959
break ;
1959
1960
}
1960
- if ( symbols [ j ] . start . pos - comments [ i ] . end > 20 ) {
1961
+ if ( symbols [ j ] . start . pos - pos > 20 ) {
1961
1962
// This comment is too far away to refer to the given symbol. Drop
1962
1963
// the comment altogether.
1963
- trace ( ` dropping comment : too far from any symbol ( ${ comments [ i ] . value . slice ( 0 , 30 ) } )` ) ;
1964
+ trace ( ' dropping comments : too far from any symbol' ) ;
1964
1965
continue ;
1965
1966
}
1966
1967
symbols [ j ] . start . comments_before ??= [ ] ;
1967
- symbols [ j ] . start . comments_before . push (
1968
- new terser . AST_Token (
1969
- comments [ i ] . type == 'Line' ? 'comment1' : 'comment2' ,
1970
- comments [ i ] . value ,
1971
- undefined ,
1972
- undefined ,
1973
- false ,
1974
- undefined ,
1975
- undefined ,
1976
- '0' ,
1977
- ) ,
1978
- ) ;
1968
+ for ( const comment of comments ) {
1969
+ trace ( 'reattaching comment' ) ;
1970
+ symbols [ j ] . start . comments_before . push (
1971
+ new terser . AST_Token (
1972
+ comment . type == 'Line' ? 'comment1' : 'comment2' ,
1973
+ comment . value ,
1974
+ undefined ,
1975
+ undefined ,
1976
+ false ,
1977
+ undefined ,
1978
+ undefined ,
1979
+ '0' ,
1980
+ ) ,
1981
+ ) ;
1982
+ }
1979
1983
}
1980
1984
}
1981
1985
@@ -2029,19 +2033,30 @@ let extraInfo = null;
2029
2033
if ( extraInfoStart > 0 ) {
2030
2034
extraInfo = JSON . parse ( input . substr ( extraInfoStart + 14 ) ) ;
2031
2035
}
2032
- // Collect all JS code comments to this array so that we can retain them in the outputted code
2033
- // if --closureFriendly was requested.
2034
- const sourceComments = [ ] ;
2036
+ // Collect all JS code comments to this map so that we can retain them in the
2037
+ // outputted code if --closureFriendly was requested.
2038
+ const sourceComments = { } ;
2039
+ const params = {
2040
+ // Keep in sync with --language_in that we pass to closure in building.py
2041
+ ecmaVersion : 2021 ,
2042
+ sourceType : exportES6 ? 'module' : 'script' ,
2043
+ allowAwaitOutsideFunction : true ,
2044
+ } ;
2045
+ if ( closureFriendly ) {
2046
+ const currentComments = [ ] ;
2047
+ Object . assign ( params , {
2048
+ preserveParens : true ,
2049
+ onToken : ( token ) => {
2050
+ // Associate comments with the start position of the next token.
2051
+ sourceComments [ token . start ] = currentComments . slice ( ) ;
2052
+ currentComments . length = 0 ;
2053
+ } ,
2054
+ onComment : currentComments ,
2055
+ } ) ;
2056
+ }
2035
2057
let ast ;
2036
2058
try {
2037
- ast = acorn . parse ( input , {
2038
- // Keep in sync with --language_in that we pass to closure in building.py
2039
- ecmaVersion : 2021 ,
2040
- preserveParens : closureFriendly ,
2041
- onComment : closureFriendly ? sourceComments : undefined ,
2042
- sourceType : exportES6 ? 'module' : 'script' ,
2043
- allowAwaitOutsideFunction : true ,
2044
- } ) ;
2059
+ ast = acorn . parse ( input , params ) ;
2045
2060
} catch ( err ) {
2046
2061
err . message += ( ( ) => {
2047
2062
let errorMessage = '\n' + input . split ( acorn . lineBreak ) [ err . loc . line - 1 ] + '\n' ;
0 commit comments