@@ -11,39 +11,42 @@ import { expect } from 'chai';
1111describe ( 'Thrift Field ID Validation' , ( ) => {
1212 const MAX_ALLOWED_FIELD_ID = 3329 ;
1313 const THRIFT_DIR = path . join ( __dirname , '../../thrift' ) ;
14-
14+
1515 it ( 'should ensure all Thrift field IDs are within allowed range' , ( ) => {
1616 const violations : string [ ] = [ ] ;
17-
17+
1818 // Get all JavaScript files in the thrift directory
19- const thriftFiles = fs . readdirSync ( THRIFT_DIR )
20- . filter ( file => file . endsWith ( '.js' ) )
21- . map ( file => path . join ( THRIFT_DIR , file ) ) ;
22-
19+ const thriftFiles = fs
20+ . readdirSync ( THRIFT_DIR )
21+ . filter ( ( file ) => file . endsWith ( '.js' ) )
22+ . map ( ( file ) => path . join ( THRIFT_DIR , file ) ) ;
23+
2324 expect ( thriftFiles . length ) . to . be . greaterThan ( 0 , 'No Thrift JavaScript files found' ) ;
24-
25+
2526 for ( const filePath of thriftFiles ) {
2627 const fileName = path . basename ( filePath ) ;
2728 const fileContent = fs . readFileSync ( filePath , 'utf8' ) ;
28-
29+
2930 // Extract field IDs from both read and write functions
3031 const fieldIds = extractFieldIds ( fileContent ) ;
31-
32+
3233 for ( const fieldId of fieldIds ) {
3334 if ( fieldId >= MAX_ALLOWED_FIELD_ID ) {
34- violations . push ( `${ fileName } : Field ID ${ fieldId } exceeds maximum allowed value of ${ MAX_ALLOWED_FIELD_ID - 1 } ` ) ;
35+ violations . push (
36+ `${ fileName } : Field ID ${ fieldId } exceeds maximum allowed value of ${ MAX_ALLOWED_FIELD_ID - 1 } ` ,
37+ ) ;
3538 }
3639 }
3740 }
38-
41+
3942 if ( violations . length > 0 ) {
4043 const errorMessage = [
4144 `Found Thrift field IDs that exceed the maximum allowed value of ${ MAX_ALLOWED_FIELD_ID - 1 } .` ,
4245 'This can cause compatibility issues and conflicts with reserved ID ranges.' ,
4346 'Violations found:' ,
44- ...violations . map ( v => ` - ${ v } ` )
47+ ...violations . map ( ( v ) => ` - ${ v } ` ) ,
4548 ] . join ( '\n' ) ;
46-
49+
4750 throw new Error ( errorMessage ) ;
4851 }
4952 } ) ;
@@ -55,29 +58,29 @@ describe('Thrift Field ID Validation', () => {
5558 */
5659function extractFieldIds ( fileContent : string ) : number [ ] {
5760 const fieldIds = new Set < number > ( ) ;
58-
61+
5962 // Pattern 1: Extract field IDs from case statements in read functions
6063 // Example: case 1281:
6164 const casePattern = / c a s e \s + ( \d + ) : / g;
6265 let match ;
63-
66+
6467 while ( ( match = casePattern . exec ( fileContent ) ) !== null ) {
6568 const fieldId = parseInt ( match [ 1 ] , 10 ) ;
6669 if ( ! isNaN ( fieldId ) ) {
6770 fieldIds . add ( fieldId ) ;
6871 }
6972 }
70-
73+
7174 // Pattern 2: Extract field IDs from writeFieldBegin calls in write functions
7275 // Example: output.writeFieldBegin('errorDetailsJson', Thrift.Type.STRING, 1281);
7376 const writeFieldPattern = / w r i t e F i e l d B e g i n \( [ ^ , ] + , \s * [ ^ , ] + , \s * ( \d + ) \) / g;
74-
77+
7578 while ( ( match = writeFieldPattern . exec ( fileContent ) ) !== null ) {
7679 const fieldId = parseInt ( match [ 1 ] , 10 ) ;
7780 if ( ! isNaN ( fieldId ) ) {
7881 fieldIds . add ( fieldId ) ;
7982 }
8083 }
81-
84+
8285 return Array . from ( fieldIds ) . sort ( ( a , b ) => a - b ) ;
83- }
86+ }
0 commit comments