@@ -31,8 +31,7 @@ export function removeAsserts(
3131
3232/**
3333 * Transformer that removes all "debugAssert" statements from the SDK and
34- * replaces the custom message for fail() and hardAssert() with shorter
35- * error codes
34+ * removes the custom message for fail() and hardAssert().
3635 */
3736class RemoveAsserts {
3837 constructor ( private readonly typeChecker : ts . TypeChecker ) { }
@@ -64,43 +63,22 @@ class RemoveAsserts {
6463 declaration . getSourceFile ( ) . fileName . indexOf ( ASSERT_LOCATION ) >= 0
6564 ) {
6665 const method = declaration . name ! . text ;
67-
6866 if ( method === 'debugAssert' ) {
6967 updatedNode = ts . factory . createOmittedExpression ( ) ;
70- } else if ( ( method === 'hardAssert' ) || ( method === 'fail' ) ) {
71- const messageIndex = ( method === 'hardAssert' ) ? 1 : 0 ;
72- if ( ( node . arguments . length > messageIndex ) && ( node . arguments [ messageIndex ] . kind === ts . SyntaxKind . StringLiteral ) ) {
73- const stringLiteral : ts . StringLiteral = node . arguments [ messageIndex ] as ts . StringLiteral ;
74- const errorMessage = RemoveAsserts . trimErrorMessage ( stringLiteral . getFullText ( ) ) ;
75- const errorCode = RemoveAsserts . errorCode ( errorMessage ) ;
76-
77- let errorId : number = - 1 ;
78- try {
79- errorId = RemoveAsserts . saveErrorCode ( errorCode , errorMessage ) ;
80- }
81- catch ( e ) {
82- console . log ( 'Failed to save error code ' + JSON . stringify ( e ) ) ;
83- }
84- const newArguments = [ ...node . arguments ] ;
85- newArguments [ messageIndex ] = ts . factory . createNumericLiteral ( errorId ) ;
86-
87- // Replace the call with the full error message to a
88- // build with an error code
89- updatedNode = ts . factory . createCallExpression (
90- declaration . name ! ,
91- /*typeArgs*/ undefined ,
92- newArguments
93- ) ;
94- } else {
95- const newArguments = [ ...node . arguments ] ;
96- newArguments [ messageIndex ] = ts . factory . createNumericLiteral ( - 1 ) ;
97- // Remove the log message but keep the assertion
98- updatedNode = ts . factory . createCallExpression (
99- declaration . name ! ,
100- /*typeArgs*/ undefined ,
101- newArguments
102- ) ;
103- }
68+ } else if ( method === 'hardAssert' ) {
69+ // Remove the log message but keep the assertion
70+ updatedNode = ts . factory . createCallExpression (
71+ declaration . name ! ,
72+ /*typeArgs*/ undefined ,
73+ node . arguments . filter ( value => ! ts . isStringLiteral ( value ) )
74+ ) ;
75+ } else if ( method === 'fail' ) {
76+ // Remove the log message
77+ updatedNode = ts . factory . createCallExpression (
78+ declaration . name ! ,
79+ /*typeArgs*/ undefined ,
80+ node . arguments . filter ( value => ! ts . isStringLiteral ( value ) )
81+ ) ;
10482 }
10583 }
10684 }
@@ -113,48 +91,4 @@ class RemoveAsserts {
11391 return node ;
11492 }
11593 }
116-
117- static trimErrorMessage ( errorMessage : string ) : string {
118- return errorMessage . substring (
119- errorMessage . indexOf ( "'" ) + 1 ,
120- errorMessage . lastIndexOf ( "'" ) ) ;
121- }
122-
123- static errorCode ( errorMessage : string ) : string {
124- // Create a sha256 hash from the parameter names and types.
125- const hash = createHash ( 'sha256' ) ;
126- hash . update ( errorMessage ) ;
127-
128- // Use the first 7 characters of the hash for a more compact code.
129- const paramHash = hash . digest ( 'hex' ) . substring ( 0 , 7 ) ;
130-
131- return paramHash ;
132- }
133-
134-
135-
136- static saveErrorCode ( errorCode : string , errorMessage : string ) : number {
137- const errorCodes = RemoveAsserts . getErrorCodes ( ) ;
138-
139- const existingErrorCode : Error | undefined = errorCodes [ errorCode ] ;
140- if ( existingErrorCode )
141- { return existingErrorCode . id ; }
142-
143- const id = Object . keys ( errorCodes ) . length ;
144- errorCodes [ errorCode ] = {
145- message : errorMessage ,
146- id
147- } ;
148-
149- RemoveAsserts . saveErrorCodes ( errorCodes ) ;
150-
151- return id ;
152- }
153-
154- static getErrorCodes ( ) : Record < string , Error > {
155- const path = join ( module . path , ERROR_CODE_LOCATION ) ;
156- if ( ! existsSync ( path ) ) {
157- return { } ;
158- }
159- return JSON . parse ( readFileSync ( path , 'utf-8' ) ) ;
160- }
94+ }
0 commit comments