@@ -1895,21 +1895,31 @@ class WithStatic { static { const inStaticBlock = 20; } }
18951895 /**
18961896 * @param {string } source what to parse
18971897 * @param {number } index which statement to ask about
1898+ * @param {boolean } sourceAvailable whether the original source is available
18981899 * @returns {string } the source the comment run covers
18991900 */
1900- const attachedTo = ( source , index ) => {
1901+ const attachedTo = ( source , index , sourceAvailable = true ) => {
19011902 const parser = new JavascriptParser ( "module" ) ;
19021903 const { ast, comments } = JavascriptParser . _parse ( source , {
19031904 sourceType : "module" ,
19041905 ranges : true ,
19051906 comments : true
19061907 } ) ;
1907- parser . comments = /** @type {EXPECTED_ANY } */ ( comments ) ;
1908- parser . _source = source ;
1909- const statement = /** @type {EXPECTED_ANY } */ ( ast ) . body [ index ] ;
1908+ parser . comments = comments ;
1909+ parser . _source = sourceAvailable ? source : undefined ;
1910+ const statement = ast . body [ index ] ;
1911+ const target =
1912+ ( statement . type === "ExportNamedDeclaration" ||
1913+ statement . type === "ExportDefaultDeclaration" ) &&
1914+ statement . declaration
1915+ ? statement . declaration
1916+ : statement ;
1917+ if ( target !== statement ) {
1918+ parser . statementPath = [ statement , target ] ;
1919+ }
19101920 return source . slice (
1911- parser . getAttachedCommentsStart ( statement ) ,
1912- statement . range [ 0 ]
1921+ parser . getAttachedCommentsStart ( target ) ,
1922+ /** @type { [number, number] } */ ( statement . range ) [ 0 ]
19131923 ) ;
19141924 } ;
19151925
@@ -1933,6 +1943,58 @@ class WithStatic { static { const inStaticBlock = 20; } }
19331943 it ( "reads the statement itself when nothing precedes it" , ( ) => {
19341944 expect ( attachedTo ( "function f() {}" , 0 ) ) . toBe ( "" ) ;
19351945 } ) ;
1946+
1947+ it ( "ignores comments inside and after the statement" , ( ) => {
1948+ expect (
1949+ attachedTo ( "/* before */function f() {/* inside */}\n/* after */" , 0 )
1950+ ) . toBe ( "/* before */" ) ;
1951+ expect ( attachedTo ( "function f() {/* inside */}\n/* after */" , 0 ) ) . toBe (
1952+ ""
1953+ ) ;
1954+ } ) ;
1955+
1956+ it ( "keeps the conservative comment run without source text" , ( ) => {
1957+ expect (
1958+ attachedTo (
1959+ "/* first */first();\n/* second */function f() {}/* after */" ,
1960+ 1 ,
1961+ false
1962+ )
1963+ ) . toBe ( "/* first */first();\n/* second */" ) ;
1964+ } ) ;
1965+
1966+ it . each ( [ "export function f() {}" , "export default function() {}" ] ) (
1967+ "finds comments before %s" ,
1968+ ( declaration ) => {
1969+ expect ( attachedTo ( `/* before */${ declaration } /* after */` , 0 ) ) . toBe (
1970+ "/* before */"
1971+ ) ;
1972+ }
1973+ ) ;
1974+
1975+ it ( "does not read every later comment to find an attached run" , ( ) => {
1976+ const source = "/* before */function f() {}" + "/* after */" . repeat ( 1024 ) ;
1977+ const parser = new JavascriptParser ( "module" ) ;
1978+ const { ast, comments } = JavascriptParser . _parse ( source , {
1979+ sourceType : "module" ,
1980+ ranges : true ,
1981+ comments : true
1982+ } ) ;
1983+ let reads = 0 ;
1984+ for ( const comment of comments ) {
1985+ const range = comment . range ;
1986+ Object . defineProperty ( comment , "range" , {
1987+ get ( ) {
1988+ reads ++ ;
1989+ return range ;
1990+ }
1991+ } ) ;
1992+ }
1993+ parser . comments = comments ;
1994+ parser . _source = source ;
1995+ expect ( parser . getAttachedCommentsStart ( ast . body [ 0 ] ) ) . toBe ( 0 ) ;
1996+ expect ( reads ) . toBeLessThan ( 32 ) ;
1997+ } ) ;
19361998 } ) ;
19371999
19382000 describe ( "WebpackParser fast paths" , ( ) => {
0 commit comments