@@ -67,59 +67,70 @@ const acorn = require("acorn");
6767const walk = require ( "estree-walker" ) . walk ;
6868
6969function detectHardcodedValues ( code , scriptName ) {
70- let processedCode = code . replace ( / (? ! \w + # ) \b # ( \w + ) / g, "_$1" ) ;
71- const ast = acorn . parse ( processedCode , {
70+ let processedCode = String ( code || '' ) . replace ( / (? ! \w + # ) \b # ( \w + ) / g, "_$1" ) ;
71+
72+ let ast ;
73+ try {
74+ ast = acorn . parse ( processedCode , {
7275 ecmaVersion : "latest" ,
7376 locations : true ,
74- } ) ;
77+ } ) ;
78+ } catch ( e ) {
79+ if ( e instanceof SyntaxError ) {
80+ console . error ( `[ACORN PARSE ERROR] Skipping script "${ scriptName } " due to malformed code` ) ;
81+ // Return an empty array so the main loop can continue
82+ return [ ] ;
83+ }
84+ throw e ; // Re-throw other unexpected errors
85+ }
7586
76- const hardcodedValues = [ ] ;
87+ const hardcodedValues = [ ] ;
7788
78- walk ( ast , {
79- enter ( node ) {
80- // Variable assignments
81- if ( node . type === "VariableDeclaration" ) {
82- node . declarations . forEach ( ( declaration ) => {
83- if (
84- declaration . init &&
85- declaration . init . type === "Literal" &&
86- typeof declaration . init . value === "string" &&
87- ! isCommonException ( declaration . init . value )
88- ) {
89- hardcodedValues . push ( {
90- scriptName : scriptName ,
91- variableName : declaration . id . name ,
92- field : "hard_coded_value_detected" ,
93- status : CONSTANTS . FAIL ,
94- type : typeof declaration . init . value ,
95- line : declaration . loc . start . line ,
96- column : declaration . loc . start . column ,
97- } ) ;
98- }
99- } ) ;
100- }
89+ walk ( ast , {
90+ enter ( node ) {
91+ // Variable assignments
92+ if ( node . type === "VariableDeclaration" ) {
93+ node . declarations . forEach ( ( declaration ) => {
94+ if (
95+ declaration . init &&
96+ declaration . init . type === "Literal" &&
97+ typeof declaration . init . value === "string" &&
98+ ! isCommonException ( declaration . init . value )
99+ ) {
100+ hardcodedValues . push ( {
101+ scriptName : scriptName ,
102+ variableName : declaration . id . name ,
103+ field : "hard_coded_value_detected" ,
104+ status : CONSTANTS . FAIL ,
105+ type : typeof declaration . init . value ,
106+ line : declaration . loc . start . line ,
107+ column : declaration . loc . start . column ,
108+ } ) ;
109+ }
110+ } ) ;
111+ }
101112
102- // Object literals
103- if (
104- node . type === "Property" &&
105- node . value . type === "Literal" &&
106- typeof node . value . value === "string" &&
107- ! isCommonException ( node . value . value )
108- ) {
109- hardcodedValues . push ( {
110- scriptName : scriptName ,
111- variableName : node . key . name || node . key . value ,
112- field : "hard_coded_value_detected" ,
113- status : CONSTANTS . FAIL ,
114- type : typeof node . value . value ,
115- line : node . loc . start . line ,
116- column : node . loc . start . column ,
117- } ) ;
118- }
119- } ,
120- } ) ;
113+ // Object literals
114+ if (
115+ node . type === "Property" &&
116+ node . value . type === "Literal" &&
117+ typeof node . value . value === "string" &&
118+ ! isCommonException ( node . value . value )
119+ ) {
120+ hardcodedValues . push ( {
121+ scriptName : scriptName ,
122+ variableName : node . key . name || node . key . value ,
123+ field : "hard_coded_value_detected" ,
124+ status : CONSTANTS . FAIL ,
125+ type : typeof node . value . value ,
126+ line : node . loc . start . line ,
127+ column : node . loc . start . column ,
128+ } ) ;
129+ }
130+ } ,
131+ } ) ;
121132
122- return hardcodedValues ;
133+ return hardcodedValues ;
123134}
124135
125136// Helper functions
0 commit comments