-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX lts] Workaround for integer literals
Workaround for the Glimmer VM bug which encodes/decodes integer literals correctly. See #15635. Based on #18484 and #18484 (comment) Fixes #15635. Closes #18484. Co-authored-by: Saravana Kumar V <[email protected]>
- Loading branch information
1 parent
80325ec
commit b331602
Showing
5 changed files
with
223 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { assert } from '@ember/debug'; | ||
import { CapturedArguments, VM, VMArguments } from '@glimmer/interfaces'; | ||
import { HelperRootReference } from '@glimmer/reference'; | ||
|
||
function i({ positional }: CapturedArguments): number { | ||
assert('[BUG] -i takes a single string', typeof positional.at(0).value() === 'string'); | ||
return parseInt(positional.at(0).value() as string, 10); | ||
} | ||
|
||
export default function(args: VMArguments, vm: VM) { | ||
return new HelperRootReference(i, args.capture(), vm.env); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
packages/ember-template-compiler/lib/plugins/safe-integers-bugfix.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { AST, ASTPlugin, ASTPluginEnvironment, Syntax } from '@glimmer/syntax'; | ||
import { MustacheStatement, NumberLiteral } from '@glimmer/syntax/dist/types/lib/types/nodes'; | ||
|
||
/** | ||
@module ember | ||
*/ | ||
|
||
/** | ||
A Glimmer2 AST transformation that replaces all instances of | ||
```handlebars | ||
{{123456789}} | ||
``` | ||
to | ||
```handlebars | ||
{{-i "123456789"}} | ||
``` | ||
as well as other integer number literals in sexp arguments, etc. | ||
The version of Glimmer VM we are using has a bug that encodes | ||
certain integers incorrectly. This forces them into strings and | ||
use `{{-i}}` (which is a wrapper around `parseInt`) to decode | ||
them manually as a workaround. | ||
This should be removed when the Glimmer VM bug is fixed. | ||
@private | ||
@class SafeIntegersBugfix | ||
*/ | ||
|
||
export default function safeIntegersBugfix(env: ASTPluginEnvironment): ASTPlugin { | ||
let { builders: b } = env.syntax; | ||
|
||
return { | ||
name: 'safe-integers-bugfix', | ||
|
||
visitor: { | ||
MustacheStatement(node: AST.MustacheStatement): AST.MustacheStatement | undefined { | ||
if (!requiresWorkaround(node)) { | ||
return; | ||
} | ||
|
||
return b.mustache( | ||
'-i', | ||
[b.string(String(node.path.value))], | ||
undefined, | ||
!node.escaped, | ||
node.loc | ||
); | ||
}, | ||
|
||
NumberLiteral(node: AST.NumberLiteral): AST.SubExpression | undefined { | ||
if (!requiresWorkaround(node)) { | ||
return; | ||
} | ||
|
||
return b.sexpr( | ||
'-i', | ||
[b.string(String(node.value))], | ||
undefined, | ||
node.loc | ||
); | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function requiresWorkaround(node: AST.MustacheStatement): node is MustacheStatement & { path: NumberLiteral }; | ||
function requiresWorkaround(node: AST.NumberLiteral): boolean; | ||
function requiresWorkaround(node: AST.MustacheStatement | AST.NumberLiteral): boolean { | ||
if (node.type === 'MustacheStatement' && node.path.type === 'NumberLiteral') { | ||
return requiresWorkaround(node.path); | ||
} else if (node.type === 'NumberLiteral') { | ||
// TODO: narrow this down? | ||
return isInteger(node); | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
function isInteger(node: AST.NumberLiteral): boolean { | ||
return Number.isInteger(node.value); | ||
} |