Skip to content

Commit

Permalink
fix event emitters
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiW committed Sep 26, 2024
1 parent 44518d4 commit 74d0deb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
26 changes: 21 additions & 5 deletions packages/core/src/generators/angular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ const stringifyBinding =
}
};

export const replaceOutputEmits = (code?: string, outputEventEmitters?: string[]): string => {
let newCode = code || '';

if (!outputEventEmitters?.length) {
return newCode;
}

outputEventEmitters?.forEach((_var) => {
const regexp = '(^|\\s|;|\\()(props\\.?)' + _var + '\\(';
const replacer = '$1this.' + removeOnFromAngularOutputEvent(_var) + '.emit(';
newCode = newCode.replace(new RegExp(regexp, 'g'), replacer);
});

return newCode;
};

const handleNgOutletBindings = (node: MitosisNode, options: ToAngularOptions) => {
let allProps = '';
for (const key in node.properties) {
Expand Down Expand Up @@ -558,9 +574,9 @@ const processAngularCode =
DO_NOT_USE_VARS_TRANSFORMS(code, {
contextVars,
domRefs,
outputVars,
stateVars,
}),
(newCode) => replaceOutputEmits(newCode, outputVars),
(newCode) => stripStateAndPropsRefs(newCode, { replaceWith }),
);

Expand Down Expand Up @@ -869,11 +885,11 @@ export const componentToAngular: TranspilerGenerator<ToAngularOptions> =
props.delete(variableName);
});

const outputs = outputVars.map((outputName) => {
// Building the @Output() EventEmitters
const outputEvents = outputVars.map((outputName) => {
if (options?.experimental?.outputs) {
return options?.experimental?.outputs(json, outputName);
}

return `@Output() ${removeOnFromAngularOutputEvent(outputName)} = new EventEmitter()`;
});

Expand Down Expand Up @@ -997,7 +1013,7 @@ export const componentToAngular: TranspilerGenerator<ToAngularOptions> =
Boolean(injectables.length) || dynamicComponents.size || refsForObjSpread.size;

const angularCoreImports = [
...(outputs.length ? ['Output', 'EventEmitter'] : []),
...(outputEvents.length ? ['Output', 'EventEmitter'] : []),
...(options?.experimental?.inject ? ['Inject', 'forwardRef'] : []),
'Component',
...(domRefs.size || dynamicComponents.size || refsForObjSpread.size
Expand Down Expand Up @@ -1046,7 +1062,7 @@ export const componentToAngular: TranspilerGenerator<ToAngularOptions> =
})
.join('\n')}
${outputs.join('\n')}
${outputEvents.join('\n')}
${Array.from(domRefs)
.map(
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/generators/react/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export const blockToReact = (
if (json.bindings[key]?.type === 'spread') {
str += ` {...(${value})} `;
} else if (key.startsWith('on')) {
const { arguments: cusArgs = ['event'] } = json.bindings[key]!;
const { arguments: cusArgs = [] } = json.bindings[key]!;
const eventName = options.type === 'native' ? NATIVE_EVENT_MAPPER[key] || key : key;
str += ` ${eventName}={(${cusArgs.join(',')}) => ${updateStateSettersInCode(
useBindingValue,
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/helpers/strip-state-and-props-refs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { removeOnFromAngularOutputEvent } from '@/generators/angular';
import { replacePropsIdentifier, replaceStateIdentifier } from './replace-identifiers';

export type StripStateAndPropsRefsOptions = {
Expand Down Expand Up @@ -50,7 +49,7 @@ export type DO_NOT_USE_ARGS = {
* Do not use these anywhere. We are migrating to AST transforms and should avoid Regex String.replace() as they are
* very brittle.
*
* If you need to re-use a part of this, re-create it as an AST tranform first.
* If you need to re-use a part of this, re-create it as an AST transform first.
*/
export const DO_NOT_USE_VARS_TRANSFORMS = (
newCode: string,
Expand All @@ -61,7 +60,7 @@ export const DO_NOT_USE_VARS_TRANSFORMS = (
outputVars?.forEach((_var) => {
// determine expression edge cases onMessage( to this.onMessage.emit(
const regexp = '(^|\\s|;|\\()(props\\.?)' + _var + '\\(';
const replacer = '$1' + context + removeOnFromAngularOutputEvent(_var) + '.emit(';
const replacer = '$1' + context + _var + '.emit(';
newCode = newCode.replace(new RegExp(regexp, 'g'), replacer);
});

Expand Down

0 comments on commit 74d0deb

Please sign in to comment.