private void visitScript ( NodeTraversal t , Node n ) { if ( ! n . hasOneChild ( ) || ! n . getFirstChild ( ) . isExprResult ( ) ) { compiler . report ( JSError . make ( n , JSON_UNEXPECTED_TOKEN ) ) ; return ; } Node jsonObject = n . getFirstFirstChild ( ) . detach ( ) ; n . removeFirstChild ( ) ; String moduleName = t . getInput ( ) . getPath ( ) . toModuleName ( ) ; n . addChildToFront ( IR . var ( IR . name ( moduleName ) . useSourceInfoFrom ( jsonObject ) , jsonObject ) . useSourceInfoFrom ( jsonObject ) ) ; n . addChildToFront ( IR . exprResult ( IR . call ( IR . getprop ( IR . name ( "goog" ) , IR . string ( "provide" ) ) , IR . string ( moduleName ) ) ) . useSourceInfoIfMissingFromForTree ( n ) ) ; String inputPath = t . getInput ( ) . getSourceFile ( ) . getOriginalPath ( ) ; if ( inputPath . endsWith ( "/package.json" ) && jsonObject . isObjectLit ( ) ) { List < String > possibleMainEntries = compiler . getOptions ( ) . getPackageJsonEntryNames ( ) ; for ( String entryName : possibleMainEntries ) { Node entry = NodeUtil . getFirstPropMatchingKey ( jsonObject , entryName ) ; if ( entry != null && ( entry . isString ( ) || entry . isObjectLit ( ) ) ) { String dirName = inputPath . substring ( 0 , inputPath . length ( ) - "package.json" . length ( ) ) ; if ( entry . isString ( ) ) { packageJsonMainEntries . put ( inputPath , dirName + entry . getString ( ) ) ; break ; } else if ( entry . isObjectLit ( ) ) { checkState ( entryName . equals ( "browser" ) , entryName ) ; // don't break if we're processing a browser field that is an object literal // because one of its entries may override the package.json main, which // we will get in the next iteration. processBrowserFieldAdvancedUsage ( dirName , entry ) ; } } } } t . reportCodeChange ( ) ; }
For script nodes of JSON objects add a module variable assignment so the result is exported .