Skip to content
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: 🎸 Add support for self closing tag and control flow #180

Merged

Conversation

kekel87
Copy link
Contributor

@kekel87 kekel87 commented Mar 2, 2024

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[x] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[x] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

The library does not work with templates containing self-closing (Angular 15) components and control flows (Angular 17).

Issue Number: #171, #155

What is the new behavior?

Self-closing components work without changing anything other than updating the compiler.
I added processing to pass through Control Flows (BlockNode).

Does this PR introduce a breaking change?

[ ] Yes
[x] No

🤷 Perhaps old versions of Angular that are no longer supported via the latest compiler.

Other information

  • implement function to detect BlockNode and get children of BlockNode
  • add unit test to self-closing tag and control flow
  • since you haven't yet merged the infra/migrate-to-esm branch, you only had to look at the last commit

@shaharkazaz
Copy link
Collaborator

I'm currently on vacation, when I get back I'll review 🤗

@jabiinfante
Copy link

Hi!

Thanks for your work upgrading the library to support new angular features.
As far as i have try, it works like a charm.

I have found a little issue using marker:

import { marker } from '@ngneat/transloco-keys-manager/marker';

It seems the entrypoint is not found in the new package.json esm version:

Error: Module not found: Error: Package path ./marker is not exported from package

Changing the exports key in the package.json like theis, seems to work as expected:

  "exports": {
    ".": {
      "import": "./public-api.js",
      "types": "./public-api.d.ts"
    },
    "./marker": {
      "import": "./marker.js",
      "types": "./marker.d.ts"
    }
  },

Hope it helps.

@shaharkazaz
Copy link
Collaborator

@jabiinfante Thanks!
@kekel87 Can you please add this to the package.json?

@shaharkazaz shaharkazaz changed the base branch from master to infra/migrate-to-esm March 11, 2024 16:13
__tests__/buildTranslationFiles.spec.ts Outdated Show resolved Hide resolved
/**
* Returns children nodes of the given node if it's a block node.
*/
export function getChildrendNodesIfBlock(node: TmplAstNode): TmplAstNode[] {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please separate this into isBlockNode and resolveBlockChildren
Once separated we can make node.children the default return value of resolveBlockChildren and have something like this:

    if (isBlockNode(node)) {
      traverse(resolveBlockChildNodes(node), containers, config);
      continue;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means we'll have to run the if tests twice. Not very optimizing, is it?

What's more, if I return node.children without doing an if, I get a typing error.

export function isBlockNode(node: TmplAstNode): boolean {
  // test here for the first time
  return isTmplAstIfBlock(node) ||
    isTmplAstForLoopBlock(node) ||
    isTmplAstDeferredBlock(node) ||
    isTmplAstSwitchBlock(node) ||
    isBlockWithChildren(node);
}

export function resolveBlockChildNodes(node: TmplAstNode): TmplAstNode[] {
  // test again
  if (isTmplAstIfBlock(node)) {
    return node.branches;
  }

  if (isTmplAstForLoopBlock(node)) {
    return node.empty ? [...node.children, node.empty] : node.children;
  }

  if (isTmplAstDeferredBlock(node)) {
    // Typing errors here
    return [
      ...node.children,
      ...[node.loading, node.error, node.placeholder].filter(Boolean),
    ];
  }

  if (isTmplAstSwitchBlock(node)) {
    return node.cases;
  }

   // Typing errors here
  return node.children;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We don't have any performance considerations here and this isn't some very heavy calculation that I would agree we need to optimize, I'd take readability.
  2. What's the type error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/keys-builder/template/utils.ts Outdated Show resolved Hide resolved
src/keys-builder/template/utils.ts Outdated Show resolved Hide resolved
return node instanceof TmplAstSwitchBlock;
}

function isNotNull<T>(input: T | null): input is T {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed, see the next comments

Copy link
Contributor Author

@kekel87 kekel87 Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a great tool for solving typing problems in strict mode. See #180 (comment) and #180 (comment)

Copy link
Collaborator

@shaharkazaz shaharkazaz Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

K I see.
Let's a positive form of this like isTmplAstNode instead of isNotNull

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@kekel87
Copy link
Contributor Author

kekel87 commented Mar 11, 2024

@jabiinfante Thanks! @kekel87 Can you please add this to the package.json?

Done, I have also include this fix: #132

@shaharkazaz
Copy link
Collaborator

@kekel87 Just making sure you saw my last comments :)

src/marker.ts Outdated
Comment on lines 2 to 4
key: T,
params?: unknown,
lang?: string,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you update the signature? do we support resolving params like lang?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what to say. You saw my comment 😅
image
image

This is a way of specifying the scope.
I don't think this is a definitive solution.
But it does get people unstuck.

It comes from resolving the marker function in the same way as the Transloco service and pipe.

Code
  if (content.includes('@ngneat/transloco')) {
    extractors.push(serviceExtractor, pureFunctionExtractor);
  }

  if (content.includes('@ngneat/transloco-keys-manager')) {
    extractors.push(markerExtractor);
  }

  ...

  extractors
    .map((ex) => ex(ast))
    .flat()
    .forEach(({ key, lang }) => {
      // Here, we solve three functions in the same way.
      const [keyWithoutScope, scopeAlias] = resolveAliasAndKeyFromService(
        key,
        lang,
        scopes
      );
      addKey({
        scopeAlias,
        keyWithoutScope,
        ...baseParams,
      });
    });

Maybe there's a way to improve this. You need to support the scope in marker in any case. #178 #132 #179
But I grant you it has nothing to do with this PR. I can remove it if you like.

I'd like to finish the Angular subject quickly. 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just reverted to this change.
I propose to move forward and I was preparing another PR for that.
But after the release of a version.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kekel87 Missed the ref to the other issue.
I think it should be in another PR as you intended 👍

@shaharkazaz shaharkazaz merged commit 0e3ab54 into jsverse:infra/migrate-to-esm Mar 15, 2024
2 checks passed
@kekel87
Copy link
Contributor Author

kekel87 commented Mar 19, 2024

@shaharkazaz Hi, any plan for a new release ?

@shaharkazaz
Copy link
Collaborator

@kekel87 yes, I need to finish something in the Transloco repo

@shaharkazaz
Copy link
Collaborator

@kekel87 v4 is released. Thank you for all your contribution 👏

@kekel87 kekel87 deleted the feat-support-angular-16-17 branch March 27, 2024 08:55
nyffellare pushed a commit to Nyffels-Open-Source/transloco-keys-manager-with-defaults that referenced this pull request Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants