-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from NullVoxPopuli/copy-pr-185
Fix Glint types by converting helpers to plain functions
- Loading branch information
Showing
20 changed files
with
170 additions
and
694 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
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 |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import Helper from '@ember/component/helper'; | ||
import truthConvert from '../utils/truth-convert.ts'; | ||
import type { MaybeTruth } from '../utils/truth-convert.ts'; | ||
|
||
export interface AndSignature { | ||
interface AndSignature<T extends unknown[]> { | ||
Args: { | ||
Positional: MaybeTruth[]; | ||
Positional: T; | ||
}; | ||
Return: boolean; | ||
Return: T[number]; | ||
} | ||
|
||
export default helper<AndSignature>((params) => { | ||
for (let i = 0, len = params.length; i < len; i++) { | ||
if (truthConvert(params[i]) === false) { | ||
return params[i] as boolean; | ||
// We use class-based helper to ensure arguments are lazy-evaluated | ||
// and helper short-circuits like native JavaScript `&&` (logical AND). | ||
export default class AndHelper<T extends unknown[]> extends Helper< | ||
AndSignature<T> | ||
> { | ||
public compute(params: T): T[number] { | ||
for (let i = 0, len = params.length; i < len; i++) { | ||
if (truthConvert(params[i]) === false) { | ||
return params[i]; | ||
} | ||
} | ||
return params[params.length - 1]; | ||
} | ||
return params[params.length - 1] as boolean; | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,12 +1,3 @@ | ||
import { helper } from '@ember/component/helper'; | ||
|
||
export interface EqSignature { | ||
Args: { | ||
Positional: [unknown, unknown]; | ||
}; | ||
Return: boolean; | ||
export default function eq(left: unknown, right: unknown): boolean { | ||
return left === right; | ||
} | ||
|
||
export default helper<EqSignature>((params) => { | ||
return params[0] === params[1]; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,5 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import { isArray } from '@ember/array'; | ||
import type EmberArray from '@ember/array'; | ||
import { isArray as isEmberArray } from '@ember/array'; | ||
|
||
export interface IsArraySignature { | ||
Args: { | ||
Positional: unknown[] | EmberArray<unknown>; | ||
}; | ||
Return: boolean; | ||
export default function isArray(...params: unknown[]) { | ||
return params.every(isEmberArray); | ||
} | ||
|
||
export default helper<IsArraySignature>((params) => { | ||
return params.every(isArray); | ||
}); |
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 |
---|---|---|
@@ -1,13 +1,3 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import { isEmpty } from '@ember/utils'; | ||
|
||
export interface IsEmptySignature { | ||
Args: { | ||
Positional: [unknown]; | ||
}; | ||
Return: boolean; | ||
} | ||
|
||
export default helper<IsEmptySignature>(([obj]) => { | ||
return isEmpty(obj); | ||
}); | ||
export default isEmpty; |
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 |
---|---|---|
@@ -1,13 +1,3 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import { isEqual } from '@ember/utils'; | ||
|
||
export interface IsEqualSignature { | ||
Args: { | ||
Positional: [unknown, unknown]; | ||
}; | ||
Return: boolean; | ||
} | ||
|
||
export default helper<IsEqualSignature>(([a, b]) => { | ||
return isEqual(a, b); | ||
}); | ||
export default isEqual; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,3 @@ | ||
import { helper } from '@ember/component/helper'; | ||
|
||
export interface NotEqSignature { | ||
Args: { | ||
Positional: [unknown, unknown]; | ||
}; | ||
Return: boolean; | ||
export default function notEq(left: unknown, right: unknown) { | ||
return left !== right; | ||
} | ||
|
||
export default helper<NotEqSignature>((params) => { | ||
return params[0] !== params[1]; | ||
}); |
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 |
---|---|---|
@@ -1,14 +1,6 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import truthConvert from '../utils/truth-convert.ts'; | ||
import type { MaybeTruth } from '../utils/truth-convert.ts'; | ||
|
||
export interface NotSignature { | ||
Args: { | ||
Positional: MaybeTruth[]; | ||
}; | ||
Return: boolean; | ||
} | ||
|
||
export default helper<NotSignature>((params) => { | ||
export default function not(...params: MaybeTruth[]) { | ||
return params.every((param) => !truthConvert(param)); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import truthConvert from '../utils/truth-convert.ts'; | ||
import type { MaybeTruth } from '../utils/truth-convert.ts'; | ||
import Helper from '@ember/component/helper'; | ||
|
||
export interface OrSignature { | ||
interface OrSignature<T extends unknown[]> { | ||
Args: { | ||
Positional: MaybeTruth[]; | ||
Positional: T; | ||
}; | ||
Return: boolean; | ||
Return: T[number]; | ||
} | ||
|
||
export default helper<OrSignature>((params) => { | ||
for (let i = 0, len = params.length; i < len; i++) { | ||
if (truthConvert(params[i]) === true) { | ||
return params[i] as boolean; | ||
// We use class-based helper to ensure arguments are lazy-evaluated | ||
// and helper short-circuits like native JavaScript `||` (logical OR). | ||
export default class OrHelper<T extends unknown[]> extends Helper< | ||
OrSignature<T> | ||
> { | ||
public compute(params: T): T[number] { | ||
for (let i = 0, len = params.length; i < len; i++) { | ||
if (truthConvert(params[i]) === true) { | ||
return params[i]; | ||
} | ||
} | ||
return params[params.length - 1]; | ||
} | ||
return params[params.length - 1] as boolean; | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,5 @@ | ||
import { helper } from '@ember/component/helper'; | ||
import truthConvert from '../utils/truth-convert.ts'; | ||
|
||
export interface XorSignature { | ||
Args: { | ||
Positional: [unknown, unknown]; | ||
}; | ||
Return: boolean; | ||
export default function xor(left: unknown, right: unknown) { | ||
return truthConvert(left) !== truthConvert(right); | ||
} | ||
|
||
export default helper<XorSignature>((params) => { | ||
return truthConvert(params[0]) !== truthConvert(params[1]); | ||
}); |
4 changes: 4 additions & 0 deletions
4
packages/modern-test-app/app/components/and-or-type-checking.hbs
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,4 @@ | ||
<div> | ||
{{log @andArg}} | ||
{{log @orArg}} | ||
</div> |
19 changes: 19 additions & 0 deletions
19
packages/modern-test-app/app/components/and-or-type-checking.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,19 @@ | ||
import templateOnly from '@ember/component/template-only'; | ||
|
||
interface Signature { | ||
Element: HTMLDivElement; | ||
Args: { | ||
andArg: object | boolean; | ||
orArg: object | boolean; | ||
}; | ||
} | ||
|
||
const AndOrTypeChecking = templateOnly<Signature>(); | ||
|
||
export default AndOrTypeChecking; | ||
|
||
declare module '@glint/environment-ember-loose/registry' { | ||
export default interface Registry { | ||
AndOrTypeChecking: typeof AndOrTypeChecking; | ||
} | ||
} |
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
Oops, something went wrong.