fix: Fix the compiler test, and check if it worked. (#6638)

* Add tsick.js to rewrite enums.

tsc generates JavaScript which is incompatible with the Closure Compiler's advanced optimizations.

* Remove unused 'outputCode' variable.

* Rename 'run_X_in_browser.js' to 'webdriver.js'

The Mocha and generator tests can both be run either manually or via our webdriver.  In all cases they run in a browser.  These two 'run_X_in_browser.js' files only apply to webdriver, thus they are confusingly named.

Also delete completely unused (and broken) `run_all_tests.sh`

* Linting improvements to mocha/webdriver.js

Still not at 100%.  Complains about require/module/process/__dirname not being defined in multiple places.

* runTestBlock -> runTestFunction

'Block' means something very different in Blockly.

* Removal of `var` from scripts.

* Add webdriver test to verify compile test worked.

* Resolve conficts with 'develop'.

* Address PR comments.
This commit is contained in:
Neil Fraser
2022-11-25 20:45:00 +01:00
committed by GitHub
parent be4b8323c5
commit 5a64a9a7f7
14 changed files with 349 additions and 154 deletions

83
scripts/tsick.js Normal file
View File

@@ -0,0 +1,83 @@
/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Lightweight conversion from tsc ouptut to Closure Compiler
* compatible input.
*/
'use strict';
// eslint-disable-next-line no-undef
const fs = require('fs');
// eslint-disable-next-line no-undef
const DIR = process.argv[2];
// Number of files rewritten.
let fileCount = 0;
/**
* Recursively spider the given directory and rewrite any JS files found.
* @param {string} dir Directory path to start from.
*/
function spider(dir) {
if (!dir.endsWith('/')) {
dir += '/';
}
const entries = fs.readdirSync(dir, {withFileTypes: true});
for (const entry of entries) {
if (entry.isDirectory()) {
spider(dir + entry.name + '/');
} else if (entry.name.endsWith('.js')) {
rewriteFile(dir + entry.name);
}
}
}
/**
* Given a file, rewrite it if it contains problematic patterns.
* @param {string} path Path of file to potentially rewrite.
*/
function rewriteFile(path) {
const oldCode = fs.readFileSync(path, 'utf8');
const newCode = rewriteEnum(oldCode);
if (newCode !== oldCode) {
fileCount++;
fs.writeFileSync(path, newCode);
}
}
/**
* Unquote enum definitions in the given code string.
* @param {string} code Original code generated by tsc.
* @return {string} Rewritten code for Closure Compiler.
*/
function rewriteEnum(code) {
// Find all enum definitions. They look like this:
// (function (names) {
// ...
// })(names || (names = {}));
const enumDefs = code.match(/\s+\(function \((\w+)\) \{\n[^}]*\}\)\(\1 [^)]+\1 = \{\}\)\);/g) || [];
for (const oldEnumDef of enumDefs) {
// enumDef looks like a bunch of lines in one of these two formats:
// ScopeType["BLOCK"] = "block";
// KeyCodes[KeyCodes["TAB"] = 9] = "TAB";
// We need to unquote them to look like one of these two formats:
// ScopeType.BLOCK = "block";
// KeyCodes[KeyCodes.TAB = 9] = "TAB";
let newEnumDef = oldEnumDef.replace(/\["(\w+)"\]/g, '.$1');
newEnumDef = newEnumDef.replace(') {', ') { // Converted by tsick.');
code = code.replace(oldEnumDef, newEnumDef);
}
return code;
}
if (DIR) {
spider(DIR);
console.log(`Unquoted enums in ${fileCount} files.`);
} else {
throw Error('No source directory specified');
}