Files
blockly/scripts/tsick.js
dependabot[bot] 2546b01d70 chore(deps): Bump prettier from 2.8.8 to 3.0.0 (#7322)
* chore(deps): Bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* chore: Reformat using Prettier v3.0 defaults

The main change is to add trailing commas to the last line of
block-formatted function calls.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2023-07-25 14:56:10 +00:00

87 lines
2.3 KiB
JavaScript

/**
* @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');
}