Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
BeksOmega committed Nov 27, 2023
1 parent 13eadb7 commit 76da697
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 62 deletions.
4 changes: 2 additions & 2 deletions core/field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,8 +810,8 @@ export abstract class Field<T = any>
margin !== undefined
? margin
: !this.isFullBlockField()
? this.getConstants()!.FIELD_BORDER_RECT_X_PADDING
: 0;
? this.getConstants()!.FIELD_BORDER_RECT_X_PADDING
: 0;
let totalWidth = xOffset * 2;
let totalHeight = constants!.FIELD_TEXT_HEIGHT;

Expand Down
130 changes: 70 additions & 60 deletions scripts/migration/js2ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs');
const path = require('path');

const filenames = process.argv.slice(2); // Trim off node and script name.
const filenames = process.argv.slice(2); // Trim off node and script name.

//////////////////////////////////////////////////////////////////////
// Load deps files via require (since they're executalbe .js files).
Expand Down Expand Up @@ -40,7 +40,7 @@ globalThis.goog = {};
* to {'module': 'goog'} for backwards-compatibility. Valid properties
* and values include {'module': 'goog'} and {'lang': 'es6'}.
*/
goog.addDependency = function(relPath, provides, _requires, opt_loadFlags) {
goog.addDependency = function (relPath, provides, _requires, opt_loadFlags) {
// Ignore any non-ESM files, as they can't be imported.
if (opt_loadFlags?.module !== 'es6') return;

Expand All @@ -61,7 +61,7 @@ require(path.resolve(__dirname, '../../build/deps.js'));

/** RegExp matching goog.require statements. */
const requireRE =
/(?:const\s+(?:([$\w]+)|(\{[^}]*\}))\s+=\s+)?goog.require(Type)?\('([^']+)'\);/mg;
/(?:const\s+(?:([$\w]+)|(\{[^}]*\}))\s+=\s+)?goog.require(Type)?\('([^']+)'\);/gm;

/** RegExp matching key: value pairs in destructuring assignments. */
const keyValueRE = /([$\w]+)\s*:\s*([$\w]+)\s*(?=,|})/g;
Expand All @@ -80,72 +80,82 @@ for (const filename of filenames) {
contents = contents.replace(/^\s*["']use strict["']\s*; *\n/m, '');

// Migrate from goog.module to goog.declareModuleId.
const closurePathRelative =
path.relative(path.dirname(path.resolve(filename)), closurePath);
const closurePathRelative = path.relative(
path.dirname(path.resolve(filename)),
closurePath,
);
contents = contents.replace(
/^goog.module\('([$\w.]+)'\);$/m,
`import * as goog from '${closurePathRelative}/goog.js';\n` +
`goog.declareModuleId('$1');`);
/^goog.module\('([$\w.]+)'\);$/m,
`import * as goog from '${closurePathRelative}/goog.js';\n` +
`goog.declareModuleId('$1');`,
);

// Migrate from goog.require to import.
contents = contents.replace(
requireRE,
function(
orig, // Whole statement to be replaced.
name, // Name of named import of whole module (if applicable).
names, // {}-enclosed list of destructured imports.
type, // If truthy, it is a requireType not require.
moduleId, // goog.module ID that was goog.require()d.
) {
const importPath = modulePaths[moduleId];
type = type ? ' type' : '';
if (!importPath) {
console.warn(`Unable to migrate goog.require('${
moduleId}') as no ES module path known.`);
return orig;
}
let relativePath =
path.relative(path.dirname(path.resolve(filename)), importPath);
if (relativePath[0] !== '.') relativePath = './' + relativePath;
if (name) {
return `import${type} * as ${name} from '${relativePath}';`;
} else if (names) {
names = names.replace(keyValueRE, '$1 as $2');
return `import${type} ${names} from '${relativePath}';`;
} else { // Side-effect only require.
return `import${type} '${relativePath}';`;
}
});
requireRE,
function (
orig, // Whole statement to be replaced.
name, // Name of named import of whole module (if applicable).
names, // {}-enclosed list of destructured imports.
type, // If truthy, it is a requireType not require.
moduleId, // goog.module ID that was goog.require()d.
) {
const importPath = modulePaths[moduleId];
type = type ? ' type' : '';
if (!importPath) {
console.warn(
`Unable to migrate goog.require('${moduleId}') as no ES module path known.`,
);
return orig;
}
let relativePath = path.relative(
path.dirname(path.resolve(filename)),
importPath,
);
if (relativePath[0] !== '.') relativePath = './' + relativePath;
if (name) {
return `import${type} * as ${name} from '${relativePath}';`;
} else if (names) {
names = names.replace(keyValueRE, '$1 as $2');
return `import${type} ${names} from '${relativePath}';`;
} else {
// Side-effect only require.
return `import${type} '${relativePath}';`;
}
},
);

// Find and update or remove old-style export assignemnts.
/** @type {!Array<{name: string, re: RegExp>}>} */
const easyExports = [];
contents = contents.replace(
/^\s*exports\.([$\w]+)\s*=\s*([$\w]+)\s*;\n/gm,
function(
orig, // Whole statement to be replaced.
exportName, // Name to export item as.
declName, // Already-declared name for item being exported.
) {
// Renamed exports have to be transalted as-is.
if (exportName !== declName) {
return `export {${declName} as ${exportName}};\n`;
}
// OK, we're doing "export.foo = foo;". Can we update the
// declaration? We can't actualy modify it yet as we're in
// the middle of a search-and-replace on contents already, but
// we can delete the old export and later update the
// declaration into an export.
const declRE = new RegExp(
`^(\\s*)((?:const|let|var|function|class)\\s+${declName})\\b`,
'gm');
if (contents.match(declRE)) {
easyExports.push({exportName, declRE});
return ''; // Delete existing export assignment.
} else {
return `export ${exportName};\n`; // Safe fallback.
}
});
/^\s*exports\.([$\w]+)\s*=\s*([$\w]+)\s*;\n/gm,
function (
orig, // Whole statement to be replaced.
exportName, // Name to export item as.
declName, // Already-declared name for item being exported.
) {
// Renamed exports have to be transalted as-is.
if (exportName !== declName) {
return `export {${declName} as ${exportName}};\n`;
}
// OK, we're doing "export.foo = foo;". Can we update the
// declaration? We can't actualy modify it yet as we're in
// the middle of a search-and-replace on contents already, but
// we can delete the old export and later update the
// declaration into an export.
const declRE = new RegExp(
`^(\\s*)((?:const|let|var|function|class)\\s+${declName})\\b`,
'gm',
);
if (contents.match(declRE)) {
easyExports.push({exportName, declRE});
return ''; // Delete existing export assignment.
} else {
return `export ${exportName};\n`; // Safe fallback.
}
},
);
// Add 'export' to existing declarations where appropriate.
for (const {exportName, declRE} of easyExports) {
contents = contents.replace(declRE, '$1export $2');
Expand Down

0 comments on commit 76da697

Please sign in to comment.