Skip to content

Fix ts-morph insertImportDeclaration and use hash-based constant identifiers#4842

Merged
adrians5j merged 27 commits intonextfrom
copilot/update-api-and-admin-extensions
Jan 6, 2026
Merged

Fix ts-morph insertImportDeclaration and use hash-based constant identifiers#4842
adrians5j merged 27 commits intonextfrom
copilot/update-api-and-admin-extensions

Conversation

Copy link
Contributor

Copilot AI commented Jan 5, 2026

The code was incorrectly using defaultImport for named imports in ts-morph's insertImportDeclaration API, which would generate malformed import statements. Additionally, the identifier generation was using non-constant values (timestamps and long PascalCase paths), which should be replaced with hash-based constant identifiers.

Changes

  • defineApiExtension.ts:

    • Use namedImports: [{ name: exportName, alias: exportNameAlias }] for aliased imports
    • Use namedImports: ["createContextPlugin"] for simple named imports
    • Replace Case.pascal(extensionFilePath) with ApiExtension_<10-char-hash> using SHA-256 hash of file path
  • AdminExtension.ts:

    • Use namedImports: [{ name: "Extension", alias: componentName }] for aliased imports
    • Replace timestamp-based Case.pascal("Something" + Date.now()) + "Extension" with AdminExtension_<10-char-hash> using SHA-256 hash of file path

Example

ts-morph API fix:

// Before:
source.insertImportDeclaration(index, {
    defaultImport: `{ ${exportName} as ${exportNameAlias} }`,
    moduleSpecifier: importPath
});

// After:
source.insertImportDeclaration(index, {
    namedImports: [{ name: exportName, alias: exportNameAlias }],
    moduleSpecifier: importPath
});

Hash-based identifier generation:

// Before (defineApiExtension.ts):
const exportNameAlias = Case.pascal(extensionFilePath);
// Result: MyExtensionsMyApiExtensionsMyExtensionTs

// After:
const hash = crypto.createHash("sha256").update(extensionFilePath).digest("hex");
const exportNameAlias = `ApiExtension_${hash.slice(-10)}`;
// Result: ApiExtension_a1b2c3d4e5

// Before (AdminExtension.ts):
const componentName = Case.pascal("Something" + Date.now()) + "Extension";
// Result: Something1736086560000Extension (changes every time)

// After:
const hash = crypto.createHash("sha256").update(params.src).digest("hex");
const componentName = `AdminExtension_${hash.slice(-10)}`;
// Result: AdminExtension_f6e7d8c9b0 (constant for same file)

The hash-based approach ensures identifiers are constant (same file path always generates the same identifier), unique (SHA-256 with 10 characters provides sufficient uniqueness), and readable (short identifiers like ApiExtension_xyz).

Original prompt

please update code in #file:defineApiExtension.ts so that it follows rules outlined in slack convo

do the same for #file:AdminExtension.ts

The user has attached the following files from their workspace:

  • packages/app-admin/src/extensions/AdminExtension.ts
  • pasted_image_1.png
  • packages/project/src/defineExtension/defineApiExtension.ts

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

adrians5j and others added 23 commits December 12, 2025 15:07
# Conflicts:
#	packages/cli/files/references.json
- Modified defineExtension.ts to return React component with .def, .getDefinition() properties
- Maintained backward compatibility with .definition and .ReactComponent
- Updated all index.ts files to use .def instead of .definition
- Updated all usage files to remove .ReactComponent accessor
- Updated ProjectConfigModel to support both old and new API

Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
…nsion.ts and AdminExtension.ts

Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Copilot AI changed the title [WIP] Update code to follow rules from Slack conversation Fix ts-morph insertImportDeclaration to use namedImports instead of defaultImport Jan 5, 2026
Copilot AI requested a review from adrians5j January 5, 2026 13:51
@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Jest tests have been initiated (for more information, click here). ✨

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Vitest tests have been initiated (for more information, click here). ✨

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Cypress E2E tests have been initiated (for more information, click here). ✨

@github-actions
Copy link

github-actions bot commented Jan 5, 2026

Cypress E2E tests have been initiated (for more information, click here). ✨

@adrians5j
Copy link
Member

Looks like you did not receive my sshot. Here it is:
image

Note the ApiExtension_xyz comment.

retry @copilot

Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 5, 2026

Looks like you did not receive my sshot. Here it is:
image

...

Updated both files to use hash-based constant identifiers. Changes in commit d75de3d:

  • defineApiExtension.ts: Now generates ApiExtension_<10-char-hash> using SHA-256 hash of the file path
  • AdminExtension.ts: Now generates AdminExtension_<10-char-hash> instead of timestamp-based names

The identifiers are now constant (same path = same hash) and use the last 10 characters of the SHA-256 hash for uniqueness.

Copilot AI changed the title Fix ts-morph insertImportDeclaration to use namedImports instead of defaultImport Fix ts-morph insertImportDeclaration and use hash-based constant identifiers Jan 5, 2026
@adrians5j adrians5j changed the base branch from copilot/refactor-definition-return-type to next January 6, 2026 06:12
@Pavel910 Pavel910 added this to the 6.0.0 milestone Jan 6, 2026
@adrians5j adrians5j marked this pull request as ready for review January 6, 2026 06:36
@adrians5j adrians5j merged commit 79a8291 into next Jan 6, 2026
16 of 17 checks passed
@adrians5j adrians5j deleted the copilot/update-api-and-admin-extensions branch March 5, 2026 08:22
Copilot AI added a commit to loomery/aibenchmarking-webiny-js-backend-ai-arena that referenced this pull request Mar 6, 2026
Co-authored-by: adrians5j <adrian@webiny.com>
Co-authored-by: Adrian Smijulj <adrian1358@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
Copilot AI added a commit to loomery/aibenchmarking-webiny-js-backend-ai-arena that referenced this pull request Mar 6, 2026
Co-authored-by: adrians5j <adrian@webiny.com>
Co-authored-by: Adrian Smijulj <adrian1358@gmail.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: adrians5j <5121148+adrians5j@users.noreply.github.com>
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