@@ -55,7 +55,12 @@ const EXECUTABLE_SCRIPT_TYPES = new Set([
5555 'text/partytown' ,
5656] )
5757
58- const NEXT_SCRIPT_IMPORT = / i m p o r t \s + ( \w + ) \s * (?: , \s * \{ [ ^ } ] * \} \s * ) ? f r o m \s * [ ' " ] n e x t \/ s c r i p t [ ' " ] /
58+ /** `import Script from 'next/script'` (optionally with named imports) or `import { default as Script }`. */
59+ const NEXT_SCRIPT_IMPORT =
60+ / i m p o r t \s + (?: ( \w + ) \s * (?: , \s * \{ [ ^ } ] * \} \s * ) ? | \{ [ ^ } ] * \b d e f a u l t \s + a s \s + ( \w + ) [ ^ } ] * \} \s * ) f r o m \s * [ ' " ] n e x t \/ s c r i p t [ ' " ] /
61+
62+ /** MDX fenced and inline code, which a docs page displays rather than renders. */
63+ const MDX_CODE = / ` ` ` [ \s \S ] * ?` ` ` | ` [ ^ ` \n ] * ` / g
5964/** The start of the element's own `type` prop — not a suffix like `data-type`. */
6065const TYPE_PROP = / (?: ^ | \s ) t y p e \s * = \s * /
6166/** A statically known `type` value right after {@link TYPE_PROP}. */
@@ -78,7 +83,8 @@ function openingTagAttributes(source: string, start: number): string {
7883 * from an expression fails closed: the audit cannot prove it executable.
7984 */
8085function findDataNextScripts ( source : string ) : number [ ] {
81- const localName = NEXT_SCRIPT_IMPORT . exec ( source ) ?. [ 1 ]
86+ const importMatch = NEXT_SCRIPT_IMPORT . exec ( source )
87+ const localName = importMatch ?. [ 1 ] ?? importMatch ?. [ 2 ]
8288 if ( ! localName ) return [ ]
8389 const lines : number [ ] = [ ]
8490 for ( const match of source . matchAll ( new RegExp ( `<${ localName } \\b` , 'g' ) ) ) {
@@ -117,7 +123,10 @@ for (const file of files) {
117123 const bytes = await source . bytes ( )
118124 if ( bytes . includes ( 0 ) ) nulOffenders . push ( file )
119125 if ( file . startsWith ( 'apps/' ) && / \. (?: [ j t ] s x | m d x ) $ / . test ( file ) ) {
120- const text = new TextDecoder ( ) . decode ( bytes )
126+ const decoded = new TextDecoder ( ) . decode ( bytes )
127+ const text = file . endsWith ( '.mdx' )
128+ ? decoded . replace ( MDX_CODE , ( code ) => code . replace ( / [ ^ \n ] / g, ' ' ) )
129+ : decoded
121130 if ( ! text . includes ( 'next/script' ) ) continue
122131 for ( const line of findDataNextScripts ( text ) ) dataScriptOffenders . push ( `${ file } :${ line } ` )
123132 }
0 commit comments