fix: restore operator overloads, fix casts, imports, and compound assignment#55
Merged
fix: restore operator overloads, fix casts, imports, and compound assignment#55
Conversation
- Auto-derive operator names from C# token when [Name] is absent (+ → add, - → subtract, * → multiply, / → divide, etc.) - Generate overload dispatcher for operators sharing the same token with different parameter types (e.g., Money * bigint vs Money * Money) - Merge same-path imports into a single line in ImportCollector - Transpile explicit casts between bigint/Decimal/number types (BigInteger ↔ decimal ↔ int conversions) - Rewrite Math.Round/Floor/Ceiling to Decimal.js instance methods when the argument is System.Decimal - Fix const/let detection for top-level statements (use CompilationUnit as scope instead of individual GlobalStatementSyntax) - Fix hasRootIndex for executables (derive from files, not exports) - Add SampleOperatorOverloading with Money record struct and 13 TS tests
…53) The compiler-synthesized entry point type has an empty namespace, causing the import collector to treat same-namespace types as different-namespace and emit barrel aliases (from "#") instead of relative paths (from "./x"). Now falls back to the project's root namespace when currentNs is empty. Closes #53
- Compound assignment with user-defined operators (x += y) is now lowered to x = x.$add(y) when the semantic model resolves the operator to a UserDefinedOperator method. - Numeric literals implicitly converted to BigInteger now emit with the n suffix (150 → 150n) by checking ConvertedType in LiteralHandler. Closes #54
The top-level statement import fix also affects this sample — program.ts now imports TodoStore via relative path instead of barrel alias.
There was a problem hiding this comment.
Pull request overview
This PR fixes several TypeScript transpilation edge cases discovered while adding the new SampleOperatorOverloading sample, primarily around operator overloading, numeric casts, import generation/merging, and top-level statement handling.
Changes:
- Add operator auto-naming and overload dispatch for user-defined operators, plus compound-assignment lowering (
+=→$add()). - Fix numeric literal/cast emission for
BigInteger/decimal/bigintinterop and rewriteMath.Round/Floor/Ceilingfordecimal. - Consolidate duplicate imports, adjust top-level statement mutation detection, and add the SampleOperatorOverloading sample + TS tests.
Reviewed changes
Copilot reviewed 34 out of 34 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Metano.Tests/TypeMappingTranspileTests.cs | Updates UUID import assertions to accommodate merged imports. |
| tests/Metano.Tests/OperatorTranspileTests.cs | Adds coverage for operator auto-derived names and [Name] overrides. |
| src/Metano.Compiler.TypeScript/Transformation/StatementHandler.cs | Fixes mutation detection scope for top-level statements. |
| src/Metano.Compiler.TypeScript/Transformation/RecordClassTransformer.cs | Adds operator token→name mapping and operator overload dispatcher generation. |
| src/Metano.Compiler.TypeScript/Transformation/OperatorHandler.cs | Lowers user-defined compound assignments to $opName calls. |
| src/Metano.Compiler.TypeScript/Transformation/LiteralHandler.cs | Emits n-suffixed bigint literals when targeting BigInteger. |
| src/Metano.Compiler.TypeScript/Transformation/InvocationHandler.cs | Rewrites System.Math calls for decimal arguments to decimal.js instance methods. |
| src/Metano.Compiler.TypeScript/Transformation/ImportCollector.cs | Merges multiple imports from the same module path into one statement. |
| src/Metano.Compiler.TypeScript/Transformation/ExpressionTransformer.cs | Implements explicit cast lowering for decimal/BigInteger conversions. |
| src/Metano.Compiler.TypeScript/PackageJsonWriter.cs | Adjusts # import alias generation based on presence of root barrel. |
| spec/08-feature-support-matrix.md | Updates roadmap/status notes for packaging and cross-package behavior. |
| spec/04-functional-requirements.md | Extends FR-030 and adds FR-046/FR-047 requirements. |
| samples/SampleOperatorOverloading/SampleOperatorOverloading.csproj | Adds new sample project wired to transpiler output. |
| samples/SampleOperatorOverloading/Program.cs | Adds top-level-statement sample exercising imports and compound assignment. |
| samples/SampleOperatorOverloading/NoSameMoneyCurrencyException.cs | Adds sample exception type for currency mismatch. |
| samples/SampleOperatorOverloading/Money.cs | Adds Money type with overloaded operators and decimal/BigInteger conversions. |
| samples/SampleOperatorOverloading/Currency.cs | Adds Currency enum for sample. |
| js/sample-todo/src/todo-list.ts | Consolidates metano-runtime imports into a single line. |
| js/sample-todo-service/src/todos.ts | Consolidates metano-runtime imports into a single line. |
| js/sample-todo-service/package.json | Adds "#" import mapping to root barrel. |
| js/sample-operator-overloading/tsconfig.json | Adds TS build config for the new operator-overloading JS sample. |
| js/sample-operator-overloading/test/money.test.ts | Adds Bun tests covering generated operator helpers and dispatch. |
| js/sample-operator-overloading/src/program.ts | Adds generated program output verifying top-level statement fixes. |
| js/sample-operator-overloading/src/no-same-money-currency-exception.ts | Adds generated exception class output. |
| js/sample-operator-overloading/src/money.ts | Adds generated Money class with operator helpers and dispatchers. |
| js/sample-operator-overloading/src/index.ts | Adds generated barrel file for the sample. |
| js/sample-operator-overloading/src/currency.ts | Adds generated Currency enum output. |
| js/sample-operator-overloading/package.json | Adds JS package manifest for the new sample. |
| js/sample-issue-tracker/src/shared-kernel/user-id.ts | Consolidates metano-runtime imports into a single line. |
| js/sample-issue-tracker/src/planning/domain/sprint.ts | Consolidates metano-runtime imports into a single line. |
| js/sample-issue-tracker/src/issues/domain/issue-id.ts | Consolidates metano-runtime imports into a single line. |
| js/sample-issue-tracker/src/issues/application/issue-queries.ts | Consolidates metano-runtime imports into a single line (incl. type import). |
| Metano.slnx | Adds SampleOperatorOverloading project to the solution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix generate script in sample-operator-overloading package.json - Reuse transformed LHS in compound operator assignment (avoid double eval) - Restrict Math decimal rewrite to single-argument overloads only - Update docstring to include Math.Abs in decimal rewrite - Fix hasRootIndex to check outputPrefix path for subdirectory output - Strengthen UUID import assertions with regex pattern matching - Fix CSharpier formatting on Currency.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pack of fixes discovered while adding the SampleOperatorOverloading project:
+→add,*→multiply) when[Name]is absent — operators were silently skipped beforeMoney * bigintvsMoney * Money) now generate runtime dispatchers with type checksbigint/Decimal/numbernow emit correct code (new Decimal(x.toString()),BigInt(x.toFixed(0)), etc.)System.Decimalconst→letfor mutated locals, relative imports instead of barrel alias, compound assignment (+=) lowered to$add()callBigIntegeremit withnsuffixTest plan
Closes #53
Closes #54