-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feat]: equality operators #1277
Open
snewcomer
wants to merge
8
commits into
glimmerjs:main
Choose a base branch
from
snewcomer:sn/equality-operators
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fa8bfcb
[Feat]: equality operators
snewcomer cba1602
Strict only helpers
snewcomer 61d5d74
some progress with strictMode in tests
snewcomer 86aaf17
proper re-render tests
snewcomer 518b33e
pop and push instead of popJs and pushJs
snewcomer c4948ec
add opcodes to toml file
snewcomer dec2734
fail on anything but two arguments
snewcomer 15dbe96
[WIP] add loose mode compatible keywords
wycats File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
89 changes: 89 additions & 0 deletions
89
packages/@glimmer/compiler/lib/passes/1-normalization/keywords/utils/equality.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,89 @@ | ||||||
import { ASTv2, generateSyntaxError } from '@glimmer/syntax'; | ||||||
|
||||||
import { Err, Ok, Result } from '../../../../shared/result'; | ||||||
import * as mir from '../../../2-encoding/mir'; | ||||||
import { NormalizationState } from '../../context'; | ||||||
import { VISIT_EXPRS } from '../../visitors/expressions'; | ||||||
import { GenericKeywordNode, KeywordDelegate } from '../impl'; | ||||||
|
||||||
function assertEqualKeyword(node: GenericKeywordNode): Result<ASTv2.PositionalArguments> { | ||||||
let { | ||||||
args: { named, positional }, | ||||||
} = node; | ||||||
|
||||||
if (named && !named.isEmpty()) { | ||||||
return Err(generateSyntaxError(`(eq) does not take any named arguments`, node.loc)); | ||||||
} | ||||||
|
||||||
if (positional.size !== 2) { | ||||||
return Err( | ||||||
generateSyntaxError( | ||||||
`(eq) must receive two positional parameters. Received ${ | ||||||
positional?.size ?? 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since we already do |
||||||
} parameters.`, | ||||||
node.loc | ||||||
) | ||||||
); | ||||||
} | ||||||
|
||||||
return Ok(positional); | ||||||
} | ||||||
|
||||||
function translateEqualKeyword( | ||||||
{ node, state }: { node: ASTv2.CallExpression; state: NormalizationState }, | ||||||
positional: ASTv2.PositionalArguments | ||||||
): Result<mir.Equal> { | ||||||
return VISIT_EXPRS.Positional(positional, state).mapOk( | ||||||
(positional) => new mir.Equal({ positional, loc: node.loc }) | ||||||
); | ||||||
} | ||||||
|
||||||
export const equalKeyword: KeywordDelegate< | ||||||
ASTv2.CallExpression | ASTv2.AppendContent, | ||||||
ASTv2.PositionalArguments, | ||||||
mir.Equal | ||||||
> = { | ||||||
assert: assertEqualKeyword, | ||||||
translate: translateEqualKeyword, | ||||||
}; | ||||||
|
||||||
function assertNotEqualKeyword(node: GenericKeywordNode): Result<ASTv2.PositionalArguments> { | ||||||
let { | ||||||
args: { named, positional }, | ||||||
} = node; | ||||||
|
||||||
if (named && !named.isEmpty()) { | ||||||
return Err(generateSyntaxError(`(neq) does not take any named arguments`, node.loc)); | ||||||
} | ||||||
|
||||||
if (positional.size !== 2) { | ||||||
return Err( | ||||||
generateSyntaxError( | ||||||
`(neq) must receive two positional parameters. Received ${ | ||||||
positional?.size ?? 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since we already do |
||||||
} parameters.`, | ||||||
node.loc | ||||||
) | ||||||
); | ||||||
} | ||||||
|
||||||
return Ok(positional); | ||||||
} | ||||||
|
||||||
function translateNotEqualKeyword( | ||||||
{ node, state }: { node: ASTv2.CallExpression; state: NormalizationState }, | ||||||
positional: ASTv2.PositionalArguments | ||||||
): Result<mir.NotEqual> { | ||||||
return VISIT_EXPRS.Positional(positional, state).mapOk( | ||||||
(positional) => new mir.NotEqual({ positional, loc: node.loc }) | ||||||
); | ||||||
} | ||||||
|
||||||
export const notEqualKeyword: KeywordDelegate< | ||||||
ASTv2.CallExpression | ASTv2.AppendContent, | ||||||
ASTv2.PositionalArguments, | ||||||
mir.NotEqual | ||||||
> = { | ||||||
assert: assertNotEqualKeyword, | ||||||
translate: translateNotEqualKeyword, | ||||||
}; |
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
110 changes: 110 additions & 0 deletions
110
packages/@glimmer/integration-tests/test/keywords/equality-test.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,110 @@ | ||
import { | ||
RenderTest, | ||
test, | ||
jitSuite, | ||
defineComponent, | ||
preprocess, | ||
syntaxErrorFor, | ||
trackedObj, | ||
} from '../..'; | ||
|
||
class EqualTest extends RenderTest { | ||
static suiteName = '{{eq}} keyword'; | ||
|
||
@test | ||
['it works eq']() { | ||
const AComponent = defineComponent({}, '{{eq 1 1}}'); | ||
this.renderComponent(AComponent); | ||
|
||
this.assertHTML('true'); | ||
} | ||
|
||
@test | ||
['it errors if 1 argument eq']() { | ||
this.assert.throws(() => { | ||
preprocess(`{{eq 1}}`, { strictMode: true, meta: { moduleName: 'test-module' } }); | ||
}, syntaxErrorFor('(eq) must receive two positional parameters. Received 1 parameters.', `{{eq 1}}`, 'test-module', 1, 0)); | ||
} | ||
|
||
@test | ||
['it errors if more than 2 arguments eq']() { | ||
this.assert.throws(() => { | ||
preprocess(`{{eq 1 1 1}}`, { strictMode: true, meta: { moduleName: 'test-module' } }); | ||
}, syntaxErrorFor('(eq) must receive two positional parameters. Received 3 parameters.', `{{eq 1 1 1}}`, 'test-module', 1, 0)); | ||
} | ||
|
||
@test | ||
['it works falsey eq']() { | ||
const AComponent = defineComponent({}, '{{eq 1 2}}'); | ||
this.renderComponent(AComponent); | ||
|
||
this.assertHTML('false'); | ||
} | ||
|
||
@test | ||
['correctly renders when values update eq']() { | ||
let args = trackedObj({ foo: 123, bar: 456 }); | ||
|
||
const AComponent = defineComponent({}, '{{eq @foo @bar}}'); | ||
this.renderComponent(AComponent, args); | ||
|
||
this.assertHTML('false'); | ||
|
||
args.foo = 456; | ||
this.rerender(); | ||
|
||
this.assertHTML('true'); | ||
} | ||
} | ||
|
||
class NotEqualTest extends RenderTest { | ||
static suiteName = '{{neq}} keyword'; | ||
|
||
@test | ||
['it works neq']() { | ||
const AComponent = defineComponent({}, '{{neq 1 2}}'); | ||
this.renderComponent(AComponent); | ||
|
||
this.assertHTML('true'); | ||
} | ||
|
||
@test | ||
['it errors if 1 argument neq']() { | ||
this.assert.throws(() => { | ||
preprocess(`{{neq 1}}`, { strictMode: true, meta: { moduleName: 'test-module' } }); | ||
}, syntaxErrorFor('(neq) must receive two positional parameters. Received 1 parameters.', `{{neq 1}}`, 'test-module', 1, 0)); | ||
} | ||
|
||
@test | ||
['it errors if more than 2 arguments neq']() { | ||
this.assert.throws(() => { | ||
preprocess(`{{neq 1 1 1}}`, { strictMode: true, meta: { moduleName: 'test-module' } }); | ||
}, syntaxErrorFor('(neq) must receive two positional parameters. Received 3 parameters.', `{{neq 1 1 1}}`, 'test-module', 1, 0)); | ||
} | ||
|
||
@test | ||
['it works falsey neq']() { | ||
const AComponent = defineComponent({}, '{{neq 1 1}}'); | ||
this.renderComponent(AComponent); | ||
|
||
this.assertHTML('false'); | ||
} | ||
|
||
@test | ||
['correctly renders when values update neq']() { | ||
let args = trackedObj({ foo: 123, bar: 456 }); | ||
|
||
const AComponent = defineComponent({}, '{{neq @foo @bar}}'); | ||
this.renderComponent(AComponent, args); | ||
|
||
this.assertHTML('true'); | ||
|
||
args.foo = 456; | ||
this.rerender({ foo: 456 }); | ||
|
||
this.assertHTML('false'); | ||
} | ||
} | ||
|
||
jitSuite(EqualTest); | ||
jitSuite(NotEqualTest); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,4 +108,6 @@ export const enum Op { | |
Not = 110, | ||
GetDynamicVar = 111, | ||
Log = 112, | ||
Equal = 113, | ||
NotEqual = 114, | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wasn't sure if this anonymous type was ok with you. (we could share an actual interface in multiple spots)