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

Codegen 643 #646

Closed
wants to merge 5 commits into from
Closed

Codegen 643 #646

wants to merge 5 commits into from

Conversation

coder-van
Copy link

@coder-van coder-van commented Mar 5, 2024

Description

generate TypeScript code recursively from mol type source file. Add config option output, when output = 0 (default), print TypeScript code only, and when output = 1, write to files.

Fixes #643

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

yarn run test

  • Test A
  • Test B

Checklist:

  • I have performed a self-review of my own code

Copy link

vercel bot commented Mar 5, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
lumos-website ✅ Ready (Inspect) Visit Preview Mar 6, 2024 8:09pm

@homura
Copy link
Collaborator

homura commented Mar 6, 2024

Thx for your pull request, it is inspiring for the feature design of multiple output targets.

It would be better if

  1. including some test cases
  2. the cli.ts be cleaner to make it testable, it could be like this
// these declarations are implemented separately from cli.ts

// the `AbsolutePath` is based on the schema file path instead of the file system's  
declare function resolveDependencies(entrySchema: code): Map<AbsolutePath, AbsolutePath[]>;

// similar to the `codegenReturnMore` in this PR, with some enhancement
declare function codegen(code: string): CodegenResult;

type CodegenResult = {
  code: string;
  elements: string[];
}

// helpers
declare function extractAndEraseImportClauses(code: string): string;
type AbsolutePath = string;

// so it can be more readable in the cli.ts
const dependencies = resolveDependencies(entrySource)
Object.entries(dependencies).forEach(([path, dependentPaths]) => {
  // ...
})
  1. several typos, such as bolck, recard, contine
  2. several uncommon abbreviations in comments, such as ex: ...

@coder-van
Copy link
Author

OK, based on your suggestions, I will make optimizations

@coder-van
Copy link
Author

  1. AbsolutePath may be considered a system file path, so i thank RelativePath maybe more better;
  2. function codegen is associated with the test script, I will modify it in subsequent work;
  3. function loopCodegen need a sorted array from function resolveDependencies, Map structure does not provide correct ordering,so it return FileWithDependence[] now.

If you have more suggestions, please continue to reply.

Copy link

codecov bot commented Mar 7, 2024

Codecov Report

Attention: Patch coverage is 49.74874% with 100 lines in your changes are missing coverage. Please review.

Project coverage is 83.89%. Comparing base (b6a3229) to head (4972e2d).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #646      +/-   ##
===========================================
- Coverage    84.17%   83.89%   -0.29%     
===========================================
  Files          120      121       +1     
  Lines        24357    24553     +196     
  Branches      2341     2355      +14     
===========================================
+ Hits         20503    20599      +96     
- Misses        3813     3913     +100     
  Partials        41       41              
Files Coverage Δ
packages/molecule/src/codegen.ts 96.18% <100.00%> (+0.23%) ⬆️
packages/molecule/src/type.ts 100.00% <100.00%> (ø)
packages/molecule/src/resolve.ts 43.18% <43.18%> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6a3229...4972e2d. Read the comment docs.

return [cur];
}

const matched = schema.match(/.*import\s+"(.*)".*;/g);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found that the import statement definition should exclude the "", even though, there is something unexpected when a clause that includes a pattern of import "";, e.g.

vector string_import <byte>; // e.g. import "string";

maybe a capturing group could be helpful here

Copy link
Author

Choose a reason for hiding this comment

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

This is a detail I overlooked, it really has a big impact

Comment on lines +68 to +73
const _dependencies = resolveDependencies(
mRelativePath,
baseDir,
resolved
);
dependencies.push(..._dependencies);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
const _dependencies = resolveDependencies(
mRelativePath,
baseDir,
resolved
);
dependencies.push(..._dependencies);
dependencies.push(...resolveDependencies(
mRelativePath,
baseDir,
resolved
));

It's better to avoid using variables that start with an underline as they often imply that the variable is private and mutable in a scope.

Copy link
Author

Choose a reason for hiding this comment

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

I will avoid this way of writing in the future

return delImportLines.join("\n");
}

function printOrWrite(resultMap: Map<string, ParseResult>, config: Config) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is suggested to divide the function based on distinct output targets to make it more testable.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's better,agree with you.

export function loopCodegen(config: Config): Map<string, ParseResult> {
const result: Map<string, ParseResult> = new Map();
const baseDir = path.dirname(config.schemaFile);
const relativePath = path.basename(config.schemaFile);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found that it's a basename rather than a relativePath, which could be misleading in this context

Copy link
Author

Choose a reason for hiding this comment

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

Yes, indeed.

Comment on lines +18 to +22
export function resolveDependencies(
importPath: RelativePath,
baseDir: string,
resolved: Set<RelativePath>
): FileWithDependence[] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

When defining a function with similar parameters, like importPath, baseDir, and resolvedRelativePath, it is recommended to comment them

Copy link
Author

Choose a reason for hiding this comment

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

Good suggestion!

@homura
Copy link
Collaborator

homura commented Mar 8, 2024

Hi, @coder-van, your PR is inspiring, and I plan to make some improvements to it based on your work

@coder-van
Copy link
Author

Hi, @coder-van, your PR is inspiring, and I plan to make some improvements to it based on your work

As for the details of the code, I may need some time to adjust it with the team. Thank you for your careful review, it is very helpful to me

@homura
Copy link
Collaborator

homura commented Mar 8, 2024

Hi, @coder-van, I've created a PR at #648 for addressing the issue #643. Thanks for your contribution again. I hope that this #648 PR will better express the optimizations I mentioned earlier.

@coder-van
Copy link
Author

I've read your code of PR #648, it's great, made me realize that in this project, higher code quality is needed, thank you.

@coder-van
Copy link
Author

After seeing your code, I suddenly realized that I wrote it in a simple way to complete the task, and you wrote it in order to achieve a complete solution, higher goals, and output better results. I saw several points from your output. What I praise:

  1. Process all .mol in the directory (broader view);
  2. Check for circular references (very rigorous);
  3. More rigorous import checking, and processing of the content of comment blocks (I didn’t realize it at all);
  4. Detailed test cases;

There may be a point that can be considered for optimization. Currently, after all files are read and the import statements are processed, the existence of dependent files is only checked when recursively building the path. Is it better to do it earlier?

@homura
Copy link
Collaborator

homura commented Mar 9, 2024

after all files are read and the import statements are processed, the existence of dependent files is only checked when recursively building the path. Is it better to do it earlier?

Exactly. We can detect the existence of the molecule file while building the dependency graph so that the process could exit earlier if any dependence is missed

@homura
Copy link
Collaborator

homura commented Mar 20, 2024

@coder-van Thx for your PR that inspired me to create the #648 as an improvement. However, after reviewing your PR, I noticed a few issues, such as the lack of circular checking and test cases. I apologize for being nitpicky and closing it

@homura homura closed this Mar 20, 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.

Support the import statement of molecule for the codegen
2 participants