11import fs from "fs"
2- import { JSDOM , DOMWindow } from "jsdom"
2+ import type { DOMWindow } from "jsdom"
3+ import { JSDOM } from "jsdom"
34import { CLIEngine } from "eslint"
45
56const DataSources = [
@@ -43,7 +44,7 @@ type Datum = {
4344
4445// Main
4546; ( async ( ) => {
46- const data : Record < number , Datum > = Object . create ( null )
47+ const data : Record < number , Datum > = { }
4748 const existing = {
4849 binProperties : new Set < string > ( ) ,
4950 gcValues : new Set < string > ( ) ,
@@ -70,12 +71,13 @@ type Datum = {
7071 try {
7172 logger . log ( "Fetching data from %o" , url )
7273 ; ( { window } = await JSDOM . fromURL ( url ) )
73- } catch ( error ) {
74+ } catch ( err ) {
75+ const error = err as Error
7476 if ( ! error || error . message !== "Error: socket hang up" ) {
7577 throw error
7678 }
7779 logger . log ( error . message , "then retry." )
78- await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
80+ await new Promise ( ( resolve ) => setTimeout ( resolve , 2000 ) )
7981 }
8082 } while ( window == null )
8183
@@ -99,13 +101,13 @@ ${makeClassDeclarationCode(Object.keys(data))}
99101const gcNameSet = new Set(["General_Category", "gc"])
100102const scNameSet = new Set(["Script", "Script_Extensions", "sc", "scx"])
101103const gcValueSets = new DataSet(${ Object . values ( data )
102- . map ( d => makeDataCode ( d . gcValues ) )
104+ . map ( ( d ) => makeDataCode ( d . gcValues ) )
103105 . join ( "," ) } )
104106const scValueSets = new DataSet(${ Object . values ( data )
105- . map ( d => makeDataCode ( d . scValues ) )
107+ . map ( ( d ) => makeDataCode ( d . scValues ) )
106108 . join ( "," ) } )
107109const binPropertySets = new DataSet(${ Object . values ( data )
108- . map ( d => makeDataCode ( d . binProperties ) )
110+ . map ( ( d ) => makeDataCode ( d . binProperties ) )
109111 . join ( "," ) } )
110112
111113export function isValidUnicodeProperty(version: number, name: string, value: string): boolean {
@@ -141,26 +143,27 @@ export function isValidLoneUnicodeProperty(version: number, value: string): bool
141143 logger . log ( "Formatting code..." )
142144 const engine = new CLIEngine ( { fix : true } )
143145 const result = engine . executeOnText ( code , "properties.ts" ) . results [ 0 ]
144- code = result . output || code
146+ code = result . output ?? code
145147
146148 logger . log ( "Writing '%s'..." , FILE_PATH )
147149 await save ( code )
148150
149151 logger . log ( "Completed!" )
150- } ) ( ) . catch ( error => {
152+ } ) ( ) . catch ( ( err ) => {
153+ const error = err as Error
151154 logger . error ( error . stack )
152155 process . exitCode = 1
153156} )
154157
155158function collectValues (
156- window : Window ,
159+ window : DOMWindow ,
157160 id : string ,
158161 existingSet : Set < string > ,
159162) : string [ ] {
160163 const selector = `${ id } td:nth-child(1) code`
161164 const nodes = window . document . querySelectorAll ( selector )
162- const values = Array . from ( nodes , node => node . textContent || "" )
163- . filter ( value => {
165+ const values = Array . from ( nodes , ( node ) => node . textContent ?? "" )
166+ . filter ( ( value ) => {
164167 if ( existingSet . has ( value ) ) {
165168 return false
166169 }
@@ -183,15 +186,15 @@ function collectValues(
183186function makeClassDeclarationCode ( versions : string [ ] ) : string {
184187 const fields = versions
185188 . map (
186- v =>
189+ ( v ) =>
187190 `private _raw${ v } : string\nprivate _set${ v } : Set<string> | undefined` ,
188191 )
189192 . join ( "\n" )
190- const parameters = versions . map ( v => `raw${ v } : string` ) . join ( ", " )
191- const init = versions . map ( v => `this._raw${ v } = raw${ v } ` ) . join ( "\n" )
193+ const parameters = versions . map ( ( v ) => `raw${ v } : string` ) . join ( ", " )
194+ const init = versions . map ( ( v ) => `this._raw${ v } = raw${ v } ` ) . join ( "\n" )
192195 const getters = versions
193196 . map (
194- v =>
197+ ( v ) =>
195198 `public get es${ v } (): Set<string> { return this._set${ v } || (this._set${ v } = new Set(this._raw${ v } .split(" "))) }` ,
196199 )
197200 . join ( "\n" )
@@ -209,7 +212,7 @@ function makeClassDeclarationCode(versions: string[]): string {
209212
210213function makeDataCode ( values : string [ ] ) : string {
211214 return `"${ values
212- . map ( value => JSON . stringify ( value ) . slice ( 1 , - 1 ) )
215+ . map ( ( value ) => JSON . stringify ( value ) . slice ( 1 , - 1 ) )
213216 . join ( " " ) } "`
214217}
215218
@@ -227,8 +230,12 @@ function makeVerificationCode(
227230
228231function save ( content : string ) : Promise < void > {
229232 return new Promise ( ( resolve , reject ) => {
230- fs . writeFile ( FILE_PATH , content , error =>
231- error ? reject ( error ) : resolve ( ) ,
232- )
233+ fs . writeFile ( FILE_PATH , content , ( error ) => {
234+ if ( error ) {
235+ reject ( error )
236+ } else {
237+ resolve ( )
238+ }
239+ } )
233240 } )
234241}
0 commit comments