Skip to content

Commit

Permalink
change let -> const if not reassigned, phetsims/tasks#973
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Dec 12, 2018
1 parent 750daea commit 979f466
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 66 deletions.
40 changes: 20 additions & 20 deletions js/ServerTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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' )
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 };
}
Expand All @@ -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 );

Expand Down
6 changes: 3 additions & 3 deletions js/localeInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
70 changes: 35 additions & 35 deletions js/routeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );
Expand All @@ -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
Expand All @@ -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 ';
Expand All @@ -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();
Expand All @@ -349,21 +349,21 @@ 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;
}
}

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;
Expand All @@ -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 ) {
Expand All @@ -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 );
Expand All @@ -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++ ) {
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 );
Expand Down
14 changes: 7 additions & 7 deletions js/stringSubmissionQueue.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ 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 ] ) {
stringSets[ repo ] = {};
}

// 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;
Expand All @@ -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,
Expand Down Expand Up @@ -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 ] )
Expand Down
2 changes: 1 addition & 1 deletion js/translate-sim.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( `</${tag}[\\s\\>]` ) );

Expand Down

0 comments on commit 979f466

Please sign in to comment.