Skip to content

Commit

Permalink
ember meta helpers (#84)
Browse files Browse the repository at this point in the history
* ember-helpers

* allow scope override

* +
  • Loading branch information
lifeart committed Feb 1, 2024
1 parent 22c8c8f commit 9eee462
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
12 changes: 12 additions & 0 deletions plugins/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ export type ResolvedHBS = {
flags: {
hasThisAccess: boolean;
};
bindings: Set<string>;
};

function getScopeBindings(path: any, bindings: Set<string> = new Set()) {
Object.keys(path.scope.bindings).forEach((key) => {
bindings.add(key);
});
if (path.parentPath) {
getScopeBindings(path.parentPath, bindings);
}
return bindings;
}

export function processTemplate(
hbsToProcess: ResolvedHBS[],
mode: 'development' | 'production',
Expand Down Expand Up @@ -155,6 +166,7 @@ export function processTemplate(
flags: {
hasThisAccess: hasThisAccess,
},
bindings: getScopeBindings(path),
});
path.replaceWith(t.identifier('$placeholder'));
}
Expand Down
24 changes: 20 additions & 4 deletions plugins/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,27 @@ describe.each([
describe('convert function builder', () => {
describe('mustache helper usage - optional chaining', () => {
test('it has proper chains', () => {
expect($t<ASTv1.MustacheStatement>(`{{toInitials @name @initialLength @initials}}`)).toEqual(
`$:() => ` + $mh('toInitials', '$:this[$args].name,$:this[$args].initialLength,$:this[$args].initials'),
expect(
$t<ASTv1.MustacheStatement>(
`{{toInitials @name @initialLength @initials}}`,
),
).toEqual(
`$:() => ` +
$mh(
'toInitials',
'$:this[$args].name,$:this[$args].initialLength,$:this[$args].initials',
),
);
expect($t<ASTv1.MustacheStatement>(`{{toInitials @name @initialLength.a @initials}}`)).toEqual(
`$:() => ` + $mh('toInitials', '$:this[$args].name,$:this[$args].initialLength?.a,$:this[$args].initials'),
expect(
$t<ASTv1.MustacheStatement>(
`{{toInitials @name @initialLength.a @initials}}`,
),
).toEqual(
`$:() => ` +
$mh(
'toInitials',
'$:this[$args].name,$:this[$args].initialLength?.a,$:this[$args].initials',
),
);
});
});
Expand Down
8 changes: 7 additions & 1 deletion plugins/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
serializePath,
toObject,
setFlags,
setBindings,
resolvePath,
toOptionalChaining,
toSafeJSPath,
Expand Down Expand Up @@ -75,8 +76,13 @@ function patchNodePath(node: ASTv1.MustacheStatement | ASTv1.SubExpression) {
export type PrimitiveJSType = null | number | string | boolean | undefined;
export type ComplexJSType = PrimitiveJSType | HBSControlExpression | HBSNode;

export function convert(seenNodes: Set<ASTv1.Node>, flags: Flags) {
export function convert(
seenNodes: Set<ASTv1.Node>,
flags: Flags,
bindings: Set<string> = new Set(),
) {
setFlags(flags);
setBindings(bindings);

function serializeParam(p: any) {
if (typeof p !== 'string') {
Expand Down
3 changes: 3 additions & 0 deletions plugins/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export const SYMBOLS = {
$_GET_SLOTS: '$_GET_SLOTS',
$_GET_ARGS: '$_GET_ARGS',
$_GET_FW: '$_GET_FW',
COMPONENT_HELPER: '$_componentHelper',
MODIFIER_HELPER: '$_modifierHelper',
HELPER_HELPER: '$_helperHelper',
$template: '$template',
$_hasBlockParams: '$_hasBlockParams',
$_hasBlock: '$_hasBlock',
Expand Down
4 changes: 4 additions & 0 deletions plugins/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type HBSControlExpression,
type HBSNode,
serializeNode,
setBindings,
} from './utils';
import { processTemplate, type ResolvedHBS } from './babel';
import { convert } from './converter';
Expand Down Expand Up @@ -55,6 +56,7 @@ export function transform(
) {
const programs: {
meta: ResolvedHBS['flags'];
bindings: ResolvedHBS['bindings'];
template: Array<HBSNode | HBSControlExpression>;
}[] = [];
const seenNodes: Set<ASTv1.Node> = new Set();
Expand Down Expand Up @@ -84,9 +86,11 @@ export function transform(

hbsToProcess.forEach((content) => {
const flags = content.flags;
setBindings(content.bindings);
const ast = preprocess(content.template);
const program: (typeof programs)[number] = {
meta: flags,
bindings: content.bindings,
template: [],
};
traverse(ast, {
Expand Down
18 changes: 16 additions & 2 deletions plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import type { Flags } from './flags';
import type { ComplexJSType } from './converter';

let flags!: Flags;
let bindings: Set<string> = new Set();

export function setBindings(b: Set<string>) {
bindings = b;
}

export function setFlags(f: Flags) {
flags = f;
Expand Down Expand Up @@ -59,13 +64,22 @@ export function isPath(str: string) {
}

export function resolvePath(str: string) {
if (str.includes('has-block-params')) {
if (bindings.has(str)) {
return str;
}
if (str === 'has-block-params') {
str = str.replace(
'has-block-params',
`${SYMBOLS.$_hasBlockParams}.bind(this, $slots)`,
);
} else if (str.includes('has-block')) {
} else if (str === 'has-block') {
str = str.replace('has-block', `${SYMBOLS.$_hasBlock}.bind(this, $slots)`);
} else if (str === 'component') {
str = SYMBOLS.COMPONENT_HELPER;
} else if (str === 'helper') {
str = SYMBOLS.HELPER_HELPER;
} else if (str === 'modifier') {
str = SYMBOLS.MODIFIER_HELPER;
}
return toSafeJSPath(
toOptionalChaining(str)
Expand Down
10 changes: 10 additions & 0 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ const $_className = 'className';

let ROOT: Component<any> | null = null;

export function $_componentHelper(arg: any) {
return arg;
}
export function $_modifierHelper(arg: any) {
return arg;
}
export function $_helperHelper(arg: any) {
return arg;
}

export function resetRoot() {
ROOT = null;
}
Expand Down

0 comments on commit 9eee462

Please sign in to comment.