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

Commit 8e26ee3

Browse files
committed
Do not unquote properties that are not valid JS identifiers.
1 parent af6eb5b commit 8e26ee3

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/tsickle.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ class Annotator extends ClosureRewriter {
775775
// If it has a symbol, it's actually a regular declared property.
776776
if (!quotedPropSym) return false;
777777
const propName = (eae.argumentExpression as ts.StringLiteral).text;
778+
// Properties containing non-JS identifier names must not be unquoted.
779+
if (!isValidClosurePropertyName(propName)) return false;
778780
this.writeNode(eae.expression);
779781
this.emit(`.${propName}`);
780782
return true;

test_files/quote_props/quote.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
goog.module('test_files.quote_props.quote');var module = module || {id: 'test_files/quote_props/quote.js'};
1+
goog.module('test_files.quote_props.quote');var module = module || {id: 'test_files/quote_props/quote.js'};/**
2+
* @fileoverview added by tsickle
3+
* @suppress {checkTypes} checked by tsc
4+
*/
5+
26
/**
37
* @record
48
*/
@@ -14,8 +18,10 @@ quoted['hello'] = 1;
1418
function QuotedMixed() { }
1519
/** @type {number} */
1620
QuotedMixed.prototype.foo;
17-
let /** @type {!QuotedMixed} */ quotedMixed = { foo: 1 };
21+
let /** @type {!QuotedMixed} */ quotedMixed = { foo: 1, 'invalid-identifier': 2 };
1822
console.log(quotedMixed.foo);
1923
quotedMixed.foo = 1;
2024
// Should be converted to non-quoted access.
2125
quotedMixed.foo = 1;
26+
// Must not be converted to non-quoted access, as it's not valid JS.
27+
quotedMixed['invalid-identifier'] = 1;

test_files/quote_props/quote.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ interface QuotedMixed extends Quoted {
1515
// It's unclear whether it's the right thing to do, user code might
1616
// access this field in a mixed fashion.
1717
foo: number;
18+
'invalid-identifier': number;
1819
}
19-
let quotedMixed: QuotedMixed = {foo: 1};
20+
let quotedMixed: QuotedMixed = {foo: 1, 'invalid-identifier': 2};
2021
console.log(quotedMixed.foo);
2122

2223
quotedMixed.foo = 1;
2324
// Should be converted to non-quoted access.
2425
quotedMixed['foo'] = 1;
26+
// Must not be converted to non-quoted access, as it's not valid JS.
27+
quotedMixed['invalid-identifier'] = 1;

test_files/quote_props/quote.tsickle.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Warning at test_files/quote_props/quote.ts:9:13: Quoted has a string index type but is accessed using dotted access. Quoting the access.
22
Warning at test_files/quote_props/quote.ts:10:1: Quoted has a string index type but is accessed using dotted access. Quoting the access.
33
====
4+
/**
5+
* @fileoverview added by tsickle
6+
* @suppress {checkTypes} checked by tsc
7+
*/
8+
49
// silence warnings about redeclaring vars.
510
export {};
611
/**
@@ -27,17 +32,23 @@ quoted['hello'] = 1;
2732
function QuotedMixed() {}
2833
/** @type {number} */
2934
QuotedMixed.prototype.foo;
35+
/* TODO: handle strange member:
36+
'invalid-identifier': number;
37+
*/
3038

3139

3240
interface QuotedMixed extends Quoted {
3341
// Assume that foo should be renamed, as it is explicitly declared.
3442
// It's unclear whether it's the right thing to do, user code might
3543
// access this field in a mixed fashion.
3644
foo: number;
45+
'invalid-identifier': number;
3746
}
38-
let /** @type {!QuotedMixed} */ quotedMixed: QuotedMixed = {foo: 1};
47+
let /** @type {!QuotedMixed} */ quotedMixed: QuotedMixed = {foo: 1, 'invalid-identifier': 2};
3948
console.log(quotedMixed.foo);
4049

4150
quotedMixed.foo = 1;
4251
// Should be converted to non-quoted access.
4352
quotedMixed.foo = 1;
53+
// Must not be converted to non-quoted access, as it's not valid JS.
54+
quotedMixed['invalid-identifier'] = 1;

0 commit comments

Comments
 (0)