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

Support Typescript generate scaffold #2111

Merged
merged 17 commits into from
Oct 29, 2023
Merged

Support Typescript generate scaffold #2111

merged 17 commits into from
Oct 29, 2023

Conversation

bz888
Copy link
Contributor

@bz888 bz888 commented Oct 19, 2023

Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes #2098

codegen:generate working with ts-manifest, instead of targetting .yaml it will now be updating the .ts manifest with chosen abi methods

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist

  • I have tested locally
  • I have performed a self review of my changes
  • Updated any relevant documentation
  • Linked to any relevant issues
  • I have added tests relevant to my changes
  • Any dependent changes have been merged and published in downstream modules
  • My code is up to date with the base branch
  • I have updated relevant changelogs. We suggest using chan

packages/cli/CHANGELOG.md Outdated Show resolved Hide resolved
packages/cli/src/controller/init-controller.ts Outdated Show resolved Hide resolved
@@ -113,27 +114,131 @@ export function findReplace(manifest: string, replacer: RegExp, value: string):
return manifest.replace(replacer, value);
}

export function replaceArrayValueInTsManifest(manifest: string, key: string, newValue: string): string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to generalise this. Im also a little confused with the naming. It says to replace an array value, but takes a key rather than an index.

@@ -113,27 +114,131 @@ export function findReplace(manifest: string, replacer: RegExp, value: string):
return manifest.replace(replacer, value);
}

export function replaceArrayValueInTsManifest(manifest: string, key: string, newValue: string): string {
const start = manifest.indexOf(`${key}:`);
if (start === -1) return manifest; // If the value is not found, return the original manifest
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think rather than this function doing a no-op it would be better to throw and let the caller decide what to do.

Same with the open brackets check

if (start === -1) return manifest; // If the value is not found, return the original manifest

let openBrackets = 0;
let startIndex, endIndex;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing types

Comment on lines 223 to 232
let openBraces = 0;
let startIndex = 0;
const components: string[] = [];

for (let i = 0; i < content.length; i++) {
if (content[i] === '{') {
if (openBraces === 0) startIndex = i;
openBraces++;
} else if (content[i] === '}') {
openBraces--;
if (openBraces === 0) {
components.push(content.slice(startIndex, i + 1).trim());
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could be deduped with above functions too

packages/cli/src/utils/utils.ts Show resolved Hide resolved
@@ -152,6 +160,23 @@ export function constructDatasources(userInput: UserInput): EthereumDs {

const assets = new Map([[abiName, {file: userInput.abiPath}]]);

if (isTs) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think for readability, reduced union types and easier testing it would be worth splitting these functions into yaml and TS versions

packages/cli/src/controller/init-controller.ts Outdated Show resolved Hide resolved
@@ -113,27 +114,131 @@ export function findReplace(manifest: string, replacer: RegExp, value: string):
return manifest.replace(replacer, value);
}

export function replaceArrayValueInTsManifest(manifest: string, key: string, newValue: string): string {
const start = manifest.indexOf(`${key}:`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if there are multiple occurrences?

@stwiname stwiname changed the title Fix/ts generate scaffold Support Typescript generate scaffold Oct 19, 2023
Comment on lines 131 to 148
[cleanEvents, cleanFunctions] = filterExistingMethods(
selectedEvents,
selectedFunctions,
existingDs,
address,
yamlExtractor
);

constructedEvents = constructMethod<EventFragment>(cleanEvents);
constructedFunctions = constructMethod<FunctionFragment>(cleanFunctions);

const userInput: UserInput = {
startBlock: startBlock,
functions: constructedFunctions,
events: constructedEvents,
abiPath: `./abis/${abiFileName}`,
address: address,
};
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 de-duped

Copy link
Collaborator

@stwiname stwiname left a comment

Choose a reason for hiding this comment

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

Just a couple of minor things

Comment on lines 292 to 295
if (casedInputAddress && d.options.address) {
return casedInputAddress.toLowerCase() === d.options.address.toLowerCase();
return casedInputAddress === d.options.address.toLowerCase();
}
return casedInputAddress === d.options.address || (!casedInputAddress && !d.options.address);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't make much sense. The casedInputAddress === d.options.address outside of the if will always be false.

Also why is .toLowerCase() removed here?

splitArrayString(dataSources)
.filter((d) => {
const match = d.match(ADDRESS_REG);
return match && match[1].toLowerCase() === casedInputAddress;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this check needs to be added.

Suggested change
return match && match[1].toLowerCase() === casedInputAddress;
return match && match.length >= 2 && match[1].toLowerCase() === casedInputAddress;

@bz888 bz888 force-pushed the fix/ts-generate-scaffold branch from b5e6c4e to 26850a3 Compare October 24, 2023 23:58
@stwiname stwiname merged commit 65b1636 into main Oct 29, 2023
1 of 2 checks passed
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.

Rework scaffold codegen:generate for ts-manifest
2 participants