mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
More exhaustive needsDotDotForPropertyAccess
This commit is contained in:
parent
ceb5fac343
commit
20249e5c4a
@ -1054,9 +1054,11 @@ namespace ts {
|
||||
// Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal
|
||||
function needsDotDotForPropertyAccess(expression: Expression) {
|
||||
if (expression.kind === SyntaxKind.NumericLiteral) {
|
||||
// check if numeric literal was originally written with a dot
|
||||
// check if numeric literal is a decimal literal that was originally written with a dot
|
||||
const text = getLiteralTextOfNode(<LiteralExpression>expression);
|
||||
return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
|
||||
return getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.All) === NumericLiteralFlags.None
|
||||
&& !(<LiteralExpression>expression).isOctalLiteral
|
||||
&& text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
|
||||
}
|
||||
else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
|
||||
// check if constant enum value is integer
|
||||
|
||||
@ -341,16 +341,52 @@ namespace ts {
|
||||
}
|
||||
|
||||
export function isBinaryOrOctalIntegerLiteral(node: LiteralLikeNode, text: string) {
|
||||
if (node.kind === SyntaxKind.NumericLiteral && text.length > 1) {
|
||||
return node.kind === SyntaxKind.NumericLiteral
|
||||
&& (getNumericLiteralFlags(text, /*hint*/ NumericLiteralFlags.BinaryOrOctal) & NumericLiteralFlags.BinaryOrOctal) !== 0;
|
||||
}
|
||||
|
||||
export const enum NumericLiteralFlags {
|
||||
None = 0,
|
||||
Hexadecimal = 1 << 0,
|
||||
Binary = 1 << 1,
|
||||
Octal = 1 << 2,
|
||||
Scientific = 1 << 3,
|
||||
|
||||
BinaryOrOctal = Binary | Octal,
|
||||
BinaryOrOctalOrHexadecimal = BinaryOrOctal | Hexadecimal,
|
||||
All = Hexadecimal | Binary | Octal | Scientific,
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans a numeric literal string to determine the form of the number.
|
||||
* @param text Numeric literal text
|
||||
* @param hint If `Scientific` or `All` is specified, performs a more expensive check to scan for scientific notation.
|
||||
*/
|
||||
export function getNumericLiteralFlags(text: string, hint?: NumericLiteralFlags) {
|
||||
if (text.length > 1) {
|
||||
switch (text.charCodeAt(1)) {
|
||||
case CharacterCodes.b:
|
||||
case CharacterCodes.B:
|
||||
return NumericLiteralFlags.Binary;
|
||||
case CharacterCodes.o:
|
||||
case CharacterCodes.O:
|
||||
return true;
|
||||
return NumericLiteralFlags.Octal;
|
||||
case CharacterCodes.x:
|
||||
case CharacterCodes.X:
|
||||
return NumericLiteralFlags.Hexadecimal;
|
||||
}
|
||||
|
||||
if (hint & NumericLiteralFlags.Scientific) {
|
||||
for (let i = text.length - 1; i >= 0; i--) {
|
||||
switch (text.charCodeAt(i)) {
|
||||
case CharacterCodes.e:
|
||||
case CharacterCodes.E:
|
||||
return NumericLiteralFlags.Scientific;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return NumericLiteralFlags.None;
|
||||
}
|
||||
|
||||
function getQuotedEscapedLiteralText(leftQuote: string, text: string, rightQuote: string) {
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
//// [propertyAccessNumericLiterals.es6.ts]
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
|
||||
|
||||
//// [propertyAccessNumericLiterals.es6.js]
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
@ -0,0 +1,21 @@
|
||||
=== tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts ===
|
||||
0xffffffff.toString();
|
||||
>0xffffffff.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
0o01234.toString();
|
||||
>0o01234.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
0b01101101.toString();
|
||||
>0b01101101.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
1234..toString();
|
||||
>1234..toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
1e0.toString();
|
||||
>1e0.toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.es5.d.ts, --, --))
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
=== tests/cases/conformance/es6/propertyAccess/propertyAccessNumericLiterals.es6.ts ===
|
||||
0xffffffff.toString();
|
||||
>0xffffffff.toString() : string
|
||||
>0xffffffff.toString : (radix?: number) => string
|
||||
>0xffffffff : 4294967295
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
0o01234.toString();
|
||||
>0o01234.toString() : string
|
||||
>0o01234.toString : (radix?: number) => string
|
||||
>0o01234 : 668
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
0b01101101.toString();
|
||||
>0b01101101.toString() : string
|
||||
>0b01101101.toString : (radix?: number) => string
|
||||
>0b01101101 : 109
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
1234..toString();
|
||||
>1234..toString() : string
|
||||
>1234..toString : (radix?: number) => string
|
||||
>1234. : 1234
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
1e0.toString();
|
||||
>1e0.toString() : string
|
||||
>1e0.toString : (radix?: number) => string
|
||||
>1e0 : 1
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
15
tests/baselines/reference/propertyAccessNumericLiterals.js
Normal file
15
tests/baselines/reference/propertyAccessNumericLiterals.js
Normal file
@ -0,0 +1,15 @@
|
||||
//// [propertyAccessNumericLiterals.ts]
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
000.toString();
|
||||
|
||||
//// [propertyAccessNumericLiterals.js]
|
||||
0xffffffff.toString();
|
||||
668..toString();
|
||||
109..toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
000.toString();
|
||||
@ -0,0 +1,25 @@
|
||||
=== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts ===
|
||||
0xffffffff.toString();
|
||||
>0xffffffff.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
0o01234.toString();
|
||||
>0o01234.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
0b01101101.toString();
|
||||
>0b01101101.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
1234..toString();
|
||||
>1234..toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
1e0.toString();
|
||||
>1e0.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
000.toString();
|
||||
>000.toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
>toString : Symbol(Number.toString, Decl(lib.d.ts, --, --))
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
=== tests/cases/conformance/expressions/propertyAccess/propertyAccessNumericLiterals.ts ===
|
||||
0xffffffff.toString();
|
||||
>0xffffffff.toString() : string
|
||||
>0xffffffff.toString : (radix?: number) => string
|
||||
>0xffffffff : 4294967295
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
0o01234.toString();
|
||||
>0o01234.toString() : string
|
||||
>0o01234.toString : (radix?: number) => string
|
||||
>0o01234 : 668
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
0b01101101.toString();
|
||||
>0b01101101.toString() : string
|
||||
>0b01101101.toString : (radix?: number) => string
|
||||
>0b01101101 : 109
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
1234..toString();
|
||||
>1234..toString() : string
|
||||
>1234..toString : (radix?: number) => string
|
||||
>1234. : 1234
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
1e0.toString();
|
||||
>1e0.toString() : string
|
||||
>1e0.toString : (radix?: number) => string
|
||||
>1e0 : 1
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
000.toString();
|
||||
>000.toString() : string
|
||||
>000.toString : (radix?: number) => string
|
||||
>000 : 0
|
||||
>toString : (radix?: number) => string
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
// @target: es6
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
@ -0,0 +1,7 @@
|
||||
// @target: es3
|
||||
0xffffffff.toString();
|
||||
0o01234.toString();
|
||||
0b01101101.toString();
|
||||
1234..toString();
|
||||
1e0.toString();
|
||||
000.toString();
|
||||
Loading…
x
Reference in New Issue
Block a user