Skip to content

Commit 7c680ea

Browse files
committed
Added --explicit-static flag
This flag allows the user to disable the default behavior of assuming that all properties declared on the anonymous types of top level variable declarations are static
1 parent d82a3d4 commit 7c680ea

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ const main = require('./build/lib/main.js');
44

55
var args = require('minimist')(process.argv.slice(2), {
66
base: 'string',
7-
boolean: ['semantic-diagnostics', 'generate-html'],
8-
alias: {'semantic-diagnostics': 'semanticDiagnostics', 'generate-html': 'generateHTML'}
7+
boolean: ['semantic-diagnostics', 'generate-html', 'explicit-static'],
8+
alias: {
9+
'semantic-diagnostics': 'semanticDiagnostics',
10+
'generate-html': 'generateHTML',
11+
'explicit-static': 'explicitStatic'
12+
}
913
});
1014
try {
1115
var transpiler = new main.Transpiler(args);

lib/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ export interface TranspilerOptions {
4545
* Generate browser API facades instead of importing them from dart:html.
4646
*/
4747
generateHTML?: boolean;
48+
/**
49+
* Do not assume that all properties declared on the anonymous types of top level variable
50+
* declarations are static.
51+
*/
52+
explicitStatic?: boolean;
4853

4954
/**
5055
* Experimental JS Interop specific option to promote properties with function
@@ -255,7 +260,7 @@ export class Transpiler {
255260
}
256261

257262
this.lastCommentIdx = -1;
258-
merge.normalizeSourceFile(sourceFile, this.fc);
263+
merge.normalizeSourceFile(sourceFile, this.fc, this.options.explicitStatic);
259264
this.pushContext(OutputContext.Default);
260265
this.visit(sourceFile);
261266
this.popContext();

lib/merge.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export class MergedTypeParameters {
227227
/**
228228
* Normalize a SourceFile
229229
*/
230-
export function normalizeSourceFile(f: ts.SourceFile, fc: FacadeConverter) {
230+
export function normalizeSourceFile(f: ts.SourceFile, fc: FacadeConverter, explicitStatic = false) {
231231
let modules: Map<string, ts.ModuleDeclaration> = new Map();
232232

233233
// Merge top level modules.
@@ -375,24 +375,26 @@ export function normalizeSourceFile(f: ts.SourceFile, fc: FacadeConverter) {
375375
break;
376376
}
377377

378-
// Finds all existing declarations of this property in the inheritance
379-
// hierarchy of this class
380-
const existingDeclarations =
381-
findPropertyInHierarchy(base.ident(member.name), existing, classes);
382-
383-
if (existingDeclarations.size) {
384-
// TODO(derekx): For dom.d.ts it makes sense to make all properties that are
385-
// declared on the anonymous types of top level variable declarations
386-
// static, but this may not always be correct
387-
for (const existingDecl of existingDeclarations) {
388-
addModifier(existingDecl, ts.createModifier(ts.SyntaxKind.StaticKeyword));
378+
if (!explicitStatic) {
379+
// Finds all existing declarations of this property in the inheritance
380+
// hierarchy of this class
381+
const existingDeclarations =
382+
findPropertyInHierarchy(base.ident(member.name), existing, classes);
383+
384+
if (existingDeclarations.size) {
385+
for (const existingDecl of existingDeclarations) {
386+
addModifier(
387+
existingDecl, ts.createModifier(ts.SyntaxKind.StaticKeyword));
388+
}
389389
}
390390
}
391391

392392
// If needed, add declaration of property to the interface that we are
393393
// currently handling
394394
if (!findPropertyInClass(base.ident(member.name), existing)) {
395-
addModifier(member, ts.createModifier(ts.SyntaxKind.StaticKeyword));
395+
if (!explicitStatic) {
396+
addModifier(member, ts.createModifier(ts.SyntaxKind.StaticKeyword));
397+
}
396398
member.parent = existing;
397399
Array.prototype.push.call(members, member);
398400
}

test/declaration_test.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,9 @@ abstract class AbstractRange {
522522
}`);
523523
});
524524

525-
it('makes properties of top level variables with anonymous types static', () => {
526-
expectTranslate(`
525+
describe('emitting properties of top level variables with anonymous types as static', () => {
526+
it('performs this by default', () => {
527+
expectTranslate(`
527528
declare interface CacheBase {
528529
readonly CHECKING: number;
529530
readonly DOWNLOADING: number;
@@ -538,9 +539,7 @@ abstract class AbstractRange {
538539
readonly CHECKING: number;
539540
readonly DOWNLOADING: number;
540541
readonly IDLE: number;
541-
};
542-
543-
`).to.equal(`@anonymous
542+
};`).to.equal(`@anonymous
544543
@JS()
545544
abstract class CacheBase {
546545
external static num get CHECKING;
@@ -555,6 +554,45 @@ abstract class MyCache implements CacheBase {
555554
external static num get DOWNLOADING;
556555
external static num get IDLE;
557556
}`);
557+
});
558+
559+
it('but not when the --explicit-static flag is set', () => {
560+
const explicitStaticOpts = {failFast: true, explicitStatic: true};
561+
expectTranslate(
562+
`
563+
declare interface CacheBase {
564+
readonly CHECKING: number;
565+
readonly DOWNLOADING: number;
566+
readonly IDLE: number;
567+
}
568+
569+
declare interface MyCache extends CacheBase {}
570+
571+
declare var MyCache: {
572+
prototype: MyCache;
573+
new (): MyCache;
574+
readonly CHECKING: number;
575+
readonly DOWNLOADING: number;
576+
readonly IDLE: number;
577+
};`,
578+
explicitStaticOpts)
579+
.to.equal(`@anonymous
580+
@JS()
581+
abstract class CacheBase {
582+
external num get CHECKING;
583+
external num get DOWNLOADING;
584+
external num get IDLE;
585+
external factory CacheBase({num CHECKING, num DOWNLOADING, num IDLE});
586+
}
587+
588+
@JS("MyCache")
589+
abstract class MyCache implements CacheBase {
590+
external factory MyCache();
591+
external num get CHECKING;
592+
external num get DOWNLOADING;
593+
external num get IDLE;
594+
}`);
595+
});
558596
});
559597
});
560598

0 commit comments

Comments
 (0)