From 979f466f77d9d438e90e9b3bc146aed5257994ab Mon Sep 17 00:00:00 2001 From: zepumph Date: Wed, 12 Dec 2018 14:22:57 -0900 Subject: [PATCH] change let -> const if not reassigned, https://github.com/phetsims/tasks/issues/973 --- js/ServerTests.js | 40 ++++++++++----------- js/localeInfo.js | 6 ++-- js/routeHandlers.js | 70 ++++++++++++++++++------------------- js/stringSubmissionQueue.js | 14 ++++---- js/translate-sim.js | 2 +- 5 files changed, 66 insertions(+), 66 deletions(-) diff --git a/js/ServerTests.js b/js/ServerTests.js index 0f0cd658..8ae5797b 100644 --- a/js/ServerTests.js +++ b/js/ServerTests.js @@ -21,7 +21,7 @@ const FAIL = false; * test handler object - each test is a method on this object. Use "executeTest" in this module to test these handlers. * To write a handler, create a function that will return truthy if the test passes, or falsey/throw error is fails. */ -let testHandlers = { +const testHandlers = { /** * test rapidly requesting several string files @@ -30,7 +30,7 @@ let testHandlers = { testRetrievingMultipleFilesFromGitHub: function() { // create a list of the files to retrieve - let stringFilesToRetrieve = [ + const stringFilesToRetrieve = [ // NOTE - use files that are on both master and the 'tests' branch { repoName: 'arithmetic', locale: 'es', expectedToExist: true }, { repoName: 'chains', locale: 'ab', expectedToExist: true }, @@ -39,7 +39,7 @@ let testHandlers = { { repoName: 'blah', locale: 'es', expectedToExist: false } ]; - let stringRetrievalPromises = []; + const stringRetrievalPromises = []; // create the promises for retrieving the strings stringFilesToRetrieve.forEach( function( stringFileSpec ) { @@ -109,7 +109,7 @@ let testHandlers = { */ testStringMatch: function() { - let stringComparePromises = []; + const stringComparePromises = []; // compare the strings with a completely different set, and make sure that they don't match stringComparePromises.push( LongTermStringStorage.getStrings( 'arithmetic', 'es' ) @@ -149,7 +149,7 @@ let testHandlers = { stringComparePromises.push( LongTermStringStorage.getStrings( 'arithmetic', 'es' ) .then( strings => { - let firstKey = _.keys( strings )[ 0 ]; + const firstKey = _.keys( strings )[ 0 ]; strings[ firstKey ].value = strings[ firstKey ].value + 'X'; return LongTermStringStorage.stringsMatch( 'arithmetic', 'es', strings ) @@ -183,20 +183,20 @@ let testHandlers = { } // make a list of the strings to change - be careful here not to ever mess up real translations - let simName = 'chains'; - let locales = [ 'ab', 'cs' ]; - let stringChangePromises = []; + const simName = 'chains'; + const locales = [ 'ab', 'cs' ]; + const stringChangePromises = []; // get the strings, then modify them locales.forEach( function( locale ) { winston.log( 'info', 'getting strings for sim ' + simName + ', locale ' + locale ); - let modifyStringsPromise = LongTermStringStorage.getStrings( simName, locale ) + const modifyStringsPromise = LongTermStringStorage.getStrings( simName, locale ) .then( strings => { // modify the first string to have a snapshot of the epoch number at the end, e.g. "My Sim---5409483029" - let firstKey = _.keys( strings )[ 0 ]; + const firstKey = _.keys( strings )[ 0 ]; let firstStringValue = strings[ firstKey ].value; - let dividerIndex = firstStringValue.indexOf( '---' ); + const dividerIndex = firstStringValue.indexOf( '---' ); if ( dividerIndex !== -1 ) { firstStringValue = firstStringValue.substring( 0, dividerIndex ); } @@ -224,8 +224,8 @@ let testHandlers = { * @return {Object} */ testTemp: async function() { - let res = await nodeFetch( 'https://phet.colorado.edu/services/metadata/1.2/simulations?format=json&type=html&locale=en&simulation=neuron&summary' ); - let json = await res.json(); + const res = await nodeFetch( 'https://phet.colorado.edu/services/metadata/1.2/simulations?format=json&type=html&locale=en&simulation=neuron&summary' ); + const json = await res.json(); console.log( json ); return json; } @@ -255,7 +255,7 @@ module.exports.executeTest = async function executeTest( testID ) { } } else { - let errorMessage = 'requested test not defined, testID = ' + testID; + const errorMessage = 'requested test not defined, testID = ' + testID; winston.log( 'error', errorMessage ); return { err: errorMessage }; } @@ -267,29 +267,29 @@ module.exports.executeTest = async function executeTest( testID ) { */ module.exports.runTests = async function() { - let testSummary = { + const testSummary = { passed: 0, failed: 0 }; - for ( let testName of Object.keys( testHandlers ) ) { // eslint-disable-line no-restricted-syntax + for ( const testName of Object.keys( testHandlers ) ) { // eslint-disable-line no-restricted-syntax winston.info( `\n\n\nRunning test ${testName}` ); - let { testPass, err } = await this.executeTest( testName ); + const { testPass, err } = await this.executeTest( testName ); if ( testPass ) { winston.log( 'info', 'test ' + testName + ' result: PASS' ); testSummary.passed += 1; } else { - let errMessage = err ? `, err = ${err}` : ''; + const errMessage = err ? `, err = ${err}` : ''; winston.log( 'info', 'test ' + testName + ' result: PASS' + errMessage ); testSummary.failed += 1; } } - let numberOfTests = Object.keys( testHandlers ).length; - let summary = `Tests completed, ran ${numberOfTests} tests, ${testSummary.passed} passed, ${testSummary.failed} failed.`; + const numberOfTests = Object.keys( testHandlers ).length; + const summary = `Tests completed, ran ${numberOfTests} tests, ${testSummary.passed} passed, ${testSummary.failed} failed.`; winston.log( 'info', summary ); diff --git a/js/localeInfo.js b/js/localeInfo.js index 23e6fb16..2e95f162 100644 --- a/js/localeInfo.js +++ b/js/localeInfo.js @@ -46,7 +46,7 @@ async function updateLocaleInfo() { // update the sorted locale info array sortedLocaleInfoArray = []; - for ( let locale in localeInfoObject ) { + for ( const locale in localeInfoObject ) { if ( locale !== 'en' && localeInfoObject.hasOwnProperty( locale ) ) { sortedLocaleInfoArray.push( { code: locale, @@ -88,8 +88,8 @@ async function updateLocaleInfo() { * @returns {Module} - the module */ function requireFromString( src, filename ) { - let Module = module.constructor; - let m = new Module(); + const Module = module.constructor; + const m = new Module(); m._compile( src, filename ); return m.exports; } diff --git a/js/routeHandlers.js b/js/routeHandlers.js index 322d2b1a..ebcca239 100644 --- a/js/routeHandlers.js +++ b/js/routeHandlers.js @@ -206,7 +206,7 @@ module.exports.logout = function( req, res ) { */ module.exports.chooseSimulationAndLanguage = async function( req, res ) { - let simInfoArray = await simData.getSimTranslationPageInfo( req.session.teamMember ); + const simInfoArray = await simData.getSimTranslationPageInfo( req.session.teamMember ); // sort the list of sims to be in alphabetical order by sim title simInfoArray.sort( function( a, b ) { @@ -238,10 +238,10 @@ module.exports.renderTranslationPage = async function( req, res ) { const client = new Client(); - let simName = req.params.simName; - let targetLocale = req.params.targetLocale; - let activeSimsPath = '/phetsims/perennial/master/data/active-sims'; - let userId = ( req.session.userId ) ? req.session.userId : 0; // use an id of 0 for localhost testing + const simName = req.params.simName; + const targetLocale = req.params.targetLocale; + const activeSimsPath = '/phetsims/perennial/master/data/active-sims'; + const userId = ( req.session.userId ) ? req.session.userId : 0; // use an id of 0 for localhost testing winston.log( 'info', 'creating translation page for ' + simName + ' ' + targetLocale ); @@ -266,19 +266,19 @@ module.exports.renderTranslationPage = async function( req, res ) { // extract strings from the sim's html file and store them in the extractedStrings array // extractedStrings in an array of objects of the form { projectName: 'color-vision', stringKeys: [ 'key1', 'key2', ... ] } - let result = TranslationUtils.extractStrings( body, simName ); + const result = TranslationUtils.extractStrings( body, simName ); if ( !result ) { renderError( res, 'Tried to extract strings from an invalid URL', 'url: ' + simUrl ); return; } - let extractedStrings = result.extractedStrings; - let simSha = result.sha; // sha of the sim at the time of publication, or 'master' if no sha is found + const extractedStrings = result.extractedStrings; + const simSha = result.sha; // sha of the sim at the time of publication, or 'master' if no sha is found winston.log( 'info', 'sim sha: ' + simSha ); - let englishStrings = {}; // object to hold the English strings - let stringPromises = []; + const englishStrings = {}; // object to hold the English strings + const stringPromises = []; // initialize the sims array from the active-sims file in chipper winston.log( 'info', 'sending request to ' + GITHUB_RAW_FILE_URL_BASE + activeSimsPath ); @@ -290,11 +290,11 @@ module.exports.renderTranslationPage = async function( req, res ) { ); extractedStrings.forEach( async function( extractedStringObject ) { - let projectName = extractedStringObject.projectName; - let repoSha = ( projectName === simName ) ? simSha : 'master'; - let stringsFilePath = GITHUB_RAW_FILE_URL_BASE + '/phetsims/' + projectName + '/' + repoSha + '/' + projectName + + const projectName = extractedStringObject.projectName; + const repoSha = ( projectName === simName ) ? simSha : 'master'; + const stringsFilePath = GITHUB_RAW_FILE_URL_BASE + '/phetsims/' + projectName + '/' + repoSha + '/' + projectName + '-strings_en.json'; - let translatedStringsPath = GITHUB_RAW_FILE_URL_BASE + '/phetsims/babel/' + global.preferences.babelBranch + '/' + + const translatedStringsPath = GITHUB_RAW_FILE_URL_BASE + '/phetsims/babel/' + global.preferences.babelBranch + '/' + projectName + '/' + projectName + '-strings_' + targetLocale + '.json'; // request the english strings @@ -314,14 +314,14 @@ module.exports.renderTranslationPage = async function( req, res ) { return Promise.all( stringPromises ).then( async results => { winston.log( 'info', 'finished called in renderTranslationPage' ); - let currentSimStringsArray = []; - let simStringsArray = []; - let commonStringsArray = []; - let unusedTranslatedStringsArray = []; + const currentSimStringsArray = []; + const simStringsArray = []; + const commonStringsArray = []; + const unusedTranslatedStringsArray = []; // create a query for determining if the user has any saved strings let repositories = ''; - let savedStrings = {}; + const savedStrings = {}; for ( i = 0; i < extractedStrings.length; i++ ) { if ( i > 0 ) { repositories += ' OR '; @@ -332,7 +332,7 @@ module.exports.renderTranslationPage = async function( req, res ) { // These objects will store string key/value pairs for each repo. savedStrings[ extractedStrings[ i ].projectName ] = {}; } - let savedStringsQuery = 'SELECT * from saved_translations where user_id = $1 AND locale = $2 AND (' + repositories + ')'; + const savedStringsQuery = 'SELECT * from saved_translations where user_id = $1 AND locale = $2 AND (' + repositories + ')'; winston.log( 'info', 'running query: ' + savedStringsQuery ); await client.connect(); @@ -349,7 +349,7 @@ module.exports.renderTranslationPage = async function( req, res ) { if ( rows && rows.length > 0 ) { winston.log( 'info', 'using ' + rows.length + ' saved strings' ); for ( i = 0; i < rows.length; i++ ) { - let row = rows[ i ]; + const row = rows[ i ]; savedStrings[ row.repository ][ row.stringkey ] = row.stringvalue; } } @@ -357,13 +357,13 @@ module.exports.renderTranslationPage = async function( req, res ) { client.end(); let simTitle; // sim title gets filled in here (e.g. Area Builder instead of area-builder) - let otherSims = []; // other sim dependencies get filled in here (e.g. beers-law-lab when translating concentration) + const otherSims = []; // other sim dependencies get filled in here (e.g. beers-law-lab when translating concentration) // iterate over all projects from which this sim draws strings for ( i = 0; i < extractedStrings.length; i++ ) { - let project = extractedStrings[ i ]; - let strings = englishStrings[ project.projectName ]; - let previouslyTranslatedStrings = req.session.translatedStrings[ targetLocale ][ project.projectName ]; + const project = extractedStrings[ i ]; + const strings = englishStrings[ project.projectName ]; + const previouslyTranslatedStrings = req.session.translatedStrings[ targetLocale ][ project.projectName ]; // put the strings under common strings, current sim strings, or sim strings depending on which project they are from let array; @@ -380,19 +380,19 @@ module.exports.renderTranslationPage = async function( req, res ) { } for ( let j = 0; j < project.stringKeys.length; j++ ) { - let key = project.stringKeys[ j ]; + const key = project.stringKeys[ j ]; - let stringVisible = strings.hasOwnProperty( key ) && ( ( strings[ key ].visible === undefined ) ? true : strings[ key ].visible ); + const stringVisible = strings.hasOwnProperty( key ) && ( ( strings[ key ].visible === undefined ) ? true : strings[ key ].visible ); if ( stringVisible ) { // data needed to render to the string on the page - the key, the current value, the English value, and the repo - let stringRenderInfo = { + const stringRenderInfo = { key: key, englishValue: escapeHTML( strings[ key ].value ), repo: project.projectName }; - let savedStringValue = savedStrings[ project.projectName ][ key ]; + const savedStringValue = savedStrings[ project.projectName ][ key ]; // use saved string if it exists if ( savedStringValue ) { @@ -406,7 +406,7 @@ module.exports.renderTranslationPage = async function( req, res ) { else if ( previouslyTranslatedStrings[ key ] ) { // use previous translation value obtained from GitHub, if it exists - let translatedString = previouslyTranslatedStrings[ key ]; + const translatedString = previouslyTranslatedStrings[ key ]; winston.log( 'info', 'using previously translated string ' + key + ': ' + getPrintableString( translatedString.value ) ); stringRenderInfo.value = escapeHTML( translatedString.value ); @@ -427,7 +427,7 @@ module.exports.renderTranslationPage = async function( req, res ) { // Identify strings that are translated but not used so that they don't get removed from the translation. // This is only relevant for shared/common strings. - for ( let stringKey in previouslyTranslatedStrings ) { + for ( const stringKey in previouslyTranslatedStrings ) { if ( previouslyTranslatedStrings.hasOwnProperty( stringKey ) ) { let containsObjectWithKey = false; for ( let index = 0; index < array.length; index++ ) { @@ -450,7 +450,7 @@ module.exports.renderTranslationPage = async function( req, res ) { } // sort the arrays by the English values - let compare = function( a, b ) { + const compare = function( a, b ) { if ( a.englishValue.toLowerCase() < b.englishValue.toLowerCase() ) { return -1; } @@ -469,7 +469,7 @@ module.exports.renderTranslationPage = async function( req, res ) { const languageDirection = targetLocaleInfo ? targetLocaleInfo.direction : 'ltr'; // assemble the data that will be supplied to the template - let templateData = { + const templateData = { title: TITLE, subtitle: 'Please enter a translation for each English string:', destinationLanguage: languageName, @@ -580,7 +580,7 @@ module.exports.saveStrings = function( req, res ) { } ); const repos = {}; - for ( let string in req.body ) { + for ( const string in req.body ) { if ( Object.hasOwnProperty.call( req.body, string ) ) { // data submitted is in the form "[repository] [key]", for example "area-builder area-builder.title" @@ -678,7 +678,7 @@ module.exports.runTest = async function( req, res ) { if ( req.session.teamMember ) { const testID = req.params.testID; - let result = await ServerTests.executeTest( testID ); + const result = await ServerTests.executeTest( testID ); // send back an empty response res.send( result ); diff --git a/js/stringSubmissionQueue.js b/js/stringSubmissionQueue.js index 0451a0d6..62941d19 100644 --- a/js/stringSubmissionQueue.js +++ b/js/stringSubmissionQueue.js @@ -50,9 +50,9 @@ module.exports.stringSubmissionQueue = async ( req, res ) => { _.keys( await req.body ).forEach( submittedStringKey => { // data submitted is in the form "[repository] [key]", for example "area-builder area-builder.title" - let repoAndStringKey = submittedStringKey.split( ' ' ); - let repo = repoAndStringKey[ 0 ]; - let stringKey = repoAndStringKey[ 1 ]; + const repoAndStringKey = submittedStringKey.split( ' ' ); + const repo = repoAndStringKey[ 0 ]; + const stringKey = repoAndStringKey[ 1 ]; // if this sim or lib isn't represent yet, add it if ( !stringSets[ repo ] ) { @@ -60,10 +60,10 @@ module.exports.stringSubmissionQueue = async ( req, res ) => { } // get the value of the string - let stringValue = req.body[ submittedStringKey ]; + const stringValue = req.body[ submittedStringKey ]; // check if the string is already in translatedStrings to get the history if it exists - let translatedString = req.session.translatedStrings[ targetLocale ] && + const translatedString = req.session.translatedStrings[ targetLocale ] && req.session.translatedStrings[ targetLocale ][ repo ] && req.session.translatedStrings[ targetLocale ][ repo ][ stringKey ]; let history = translatedString && translatedString.history; @@ -85,7 +85,7 @@ module.exports.stringSubmissionQueue = async ( req, res ) => { // only add the update if the value has changed if ( oldValue !== stringValue ) { - let newHistoryEntry = { + const newHistoryEntry = { userId: ( req.session.userId ) ? req.session.userId : 'phet-test', timestamp: Date.now(), oldValue: oldValue, @@ -118,7 +118,7 @@ module.exports.stringSubmissionQueue = async ( req, res ) => { const changedStringSets = _.pick( stringSets, _.keys( stringsChangedFlags ) ); // save the modified strings to long-term storage - let stringSavePromises = []; + const stringSavePromises = []; _.keys( await changedStringSets ).forEach( simOrLibName => { stringSavePromises.push( LongTermStringStorage.saveStrings( simOrLibName, targetLocale, changedStringSets[ simOrLibName ] ) diff --git a/js/translate-sim.js b/js/translate-sim.js index abf1e627..78d6a0a0 100644 --- a/js/translate-sim.js +++ b/js/translate-sim.js @@ -265,7 +265,7 @@ $( document ).ready( function() { const closedTags = ( nonTranslatedString.match( /\<\/[a-zA-Z0-9]+/g ) || [] ).map( s => s.slice( 2 ) ); const tags = openTags.filter( tag => closedTags.includes( tag ) ); - for ( let tag of tags ) { // eslint-disable-line no-restricted-syntax + for ( const tag of tags ) { // eslint-disable-line no-restricted-syntax const openMatch = translatedString.match( new RegExp( `<${tag}[\\s\\>]` ) ); const closedMatch = translatedString.match( new RegExp( `]` ) );