Skip to content

Commit

Permalink
define global externs for ReadonlyMap.
Browse files Browse the repository at this point in the history
FUTURE_COPYBARA_INTEGRATE_REVIEW=TGP
PiperOrigin-RevId: 541706571
  • Loading branch information
varomodt authored and copybara-github committed Jun 20, 2023
1 parent 7c15472 commit 3c6c7ef
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/closure_externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ var NodeListOf;
*/
var RegExpExecArray;

/** @typedef {!Map} */
var ReadonlyMap;

/** @typedef {!Set} */
var ReadonlySet;

Expand Down
10 changes: 10 additions & 0 deletions src/externs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,16 @@ export function generateExterns(
reportDiagnostic(diagnostics, decl, 'anonymous type in externs');
return;
}

// gbigint, as defined in
// google3/third_party/java_src/clutz/src/resources/closure.lib.d.ts, is
// defined separately in TypeScript and JavaScript.
if (name.escapedText === 'gbigint'
// Just the terminal filename so we can test this.
&& decl.getSourceFile().fileName.endsWith('closure.lib.d.ts')) {
return;
}

const typeName = namespace.concat([name.getText()]).join('.');
if (PREDECLARED_CLOSURE_EXTERNS_LIST.indexOf(typeName) >= 0) return;

Expand Down
6 changes: 6 additions & 0 deletions src/ts_migration_exports_shim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export function createTsMigrationExportsShimTransformerFactory(
src, srcIds, typeChecker, host, manifest, tsickleDiagnostics);
const tsmesFile = srcIds.google3PathWithoutExtension() + '.tsmes.js';
const dtsFile = srcIds.google3PathWithoutExtension() + '.tsmes.d.ts';
if (!host.generateTsMigrationExportsShim) {
// we need to create the Generator to make sure there aren't any shim
// related function calls if generateTsMigrationExportsShim isn't true,
// but we don't want to actually write any files, so return
return src;
}
if (!generator.foundMigrationExportsShim()) {
// If there is no export shims calls, we still need to generate empty
// files, so that we always produce a predictable set of files.
Expand Down
7 changes: 7 additions & 0 deletions src/type_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ export class TypeTranslator {
}
return innerSymbol ?? '?';
}
// gbigint, as defined in
// google3/third_party/java_src/clutz/src/resources/closure.lib.d.ts, is
// defined separately in TypeScript and JavaScript.
// In JS, gbigint is treated as an object so needs !.
if (type.aliasSymbol?.escapedName === 'gbigint') {
return '!gbigint';
}
this.warn(`unhandled type flags: ${ts.TypeFlags[type.flags]}`);
return '?';
case ts.TypeFlags.Index:
Expand Down
1 change: 1 addition & 0 deletions test/e2e_closure_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('golden file tests', () => {
'third_party/tslib/externs.js',
'third_party/tslib/tslib.js',
'test/googbase_fake.js',
'test_files/temp_externs.js',
'test_files/augment/shim.js',
'test_files/clutz_type_value.no_externs/type_value.js',
'test_files/clutz.no_externs/default_export.js',
Expand Down
9 changes: 9 additions & 0 deletions test_files/gbigint/closure.lib.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Opaque value type that represents big integer values regardless of native
* platform support for `bigint`. See go/gbigint for more info.
*/
interface gbigint {
readonly __doNotManuallySet: unique symbol;

valueOf(): gbigint;
}
28 changes: 28 additions & 0 deletions test_files/gbigint/exports_gbigint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @fileoverview added by tsickle
* Generated from: test_files/gbigint/exports_gbigint.ts
*/
goog.module('test_files.gbigint.exports_gbigint');
var module = module || { id: 'test_files/gbigint/exports_gbigint.ts' };
goog.require('tslib');
/**
* @param {string} val
* @return {!gbigint}
*/
function toGbigint(val) {
return (/** @type {!gbigint} */ ((/** @type {*} */ (val))));
}
/**
* A `gbigint` value on an exported variable.
* @type {!gbigint}
*/
exports.myGbigint = toGbigint('0');
/**
* A `gbigint` value on a param type.
* @param {!gbigint} val
* @return {void}
*/
function takeGbigint(val) {
console.log(val);
}
exports.takeGbigint = takeGbigint;
11 changes: 11 additions & 0 deletions test_files/gbigint/exports_gbigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function toGbigint(val: string): gbigint {
return val as unknown as gbigint;
}

/** A `gbigint` value on an exported variable. */
export const myGbigint: gbigint = toGbigint('0');

/** A `gbigint` value on a param type. */
export function takeGbigint(val: gbigint): void {
console.log(val);
}
21 changes: 21 additions & 0 deletions test_files/temp_externs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* When gbigint is made globally available, this file will be
* deleted as the typings here will move to a global location. At this moment,
* the proposed location is:
* google3/javascript/externs/google_legacy.js
* @externs
*/

/**
* See go/gbigint.
*
* Opaque value type that represents `bigint` values regardless of native
* platform support for `bigint`. As value types, they have a guaranteed
* consistent runtime representation compatible with `===`, ES6 `Map` keys and
* `Set` values.
* @interface
*/
function gbigint() {}

/** @const {symbol} */
gbigint.prototype.__doNotManuallySet;

0 comments on commit 3c6c7ef

Please sign in to comment.