Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit f1c2e12

Browse files
committed
Refine identifying keyword kind (in prep for fix)
* This is preparatory work for #113. * Note that there are **three kinds of keywords**: reserved, built-in, and limited. Only reserved words must be avoided when porting from JS to Dart (see [footnote 1 in Dart Up and Running Chap. 2](https://www.dartlang.org/docs/dart-up-and-running/ch02.html#keywords) ). * `getLibraryName()` has been tweaked to only rename libraries having names that match a *reserved word*. * Added test case for built-in and limited keywords.
1 parent e8bf69e commit f1c2e12

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/main.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,19 +497,24 @@ export class Transpiler {
497497
this.emit(translated);
498498
}
499499

500-
static DART_KEYWORDS =
501-
('abstract as assert async await break case catch class const continue ' +
502-
'default deferred do dynamic else enum export extends external factory false final ' +
503-
'finally for get if implements import in is library new null operator part rethrow return' +
504-
' set static super switch sync this throw true try typedef var void while with yield')
505-
.split(/ /);
500+
// For the Dart keyword list see
501+
// https://www.dartlang.org/docs/dart-up-and-running/ch02.html#keywords
502+
static DART_RESERVED_WORDS =
503+
('assert break case catch class const continue default do else enum extends false final ' +
504+
'finally for if in is new null rethrow return super switch this throw true try var void ' +
505+
'while with').split(/ /);
506+
507+
// These are the built-in and limited keywords.
508+
static DART_OTHER_KEYWORDS =
509+
('abstract as async await deferred dynamic export external factory get implements import ' +
510+
'library operator part set static sync typedef yield').split(/ /);
506511

507512
getLibraryName(nameForTest: string = null) {
508513
var parts = (nameForTest || this.relativeFileName).split('/');
509514
return parts.filter((p) => p.length > 0)
510515
.map((p) => p.replace(/[^\w.]/g, '_'))
511516
.map((p) => p.replace(/\.[jt]s$/g, ''))
512-
.map((p) => Transpiler.DART_KEYWORDS.indexOf(p) != -1 ? '_' + p : p)
517+
.map((p) => Transpiler.DART_RESERVED_WORDS.indexOf(p) != -1 ? '_' + p : p)
513518
.join('.');
514519
}
515520

test/DartTest.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,12 @@ describe('transpile to dart', () => {
572572
var res = transpiler.translateProgram(program, 'a/b/c.ts');
573573
chai.expect(res).to.equal(' library a.b.c ; var x ;');
574574
});
575-
it('handles keywords', () => {
575+
it('handles reserved words', () => {
576576
chai.expect(transpiler.getLibraryName('/a/for/in/do/x')).to.equal('a._for._in._do.x');
577577
});
578+
it('handles built-in and limited keywords', () => {
579+
chai.expect(transpiler.getLibraryName('/as/if/sync/x')).to.equal('as._if.sync.x');
580+
});
578581
it('handles file extensions', () => {
579582
chai.expect(transpiler.getLibraryName('a/x.ts')).to.equal('a.x');
580583
chai.expect(transpiler.getLibraryName('a/x.js')).to.equal('a.x');

0 commit comments

Comments
 (0)