Skip to content

Commit

Permalink
Fix type references in parameters (#227)
Browse files Browse the repository at this point in the history
* Retain deep requires in parameter types

* Add changelog entry and bump version number

* Add test case

* Fix ast file resolution in actions test

* Fix tests on windows
  • Loading branch information
daogrady authored Apr 29, 2024
1 parent b58963c commit 3120af3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

## Version 0.21.0 - TBD

## Version 0.20.2 - 2024-04-29
### Fixed
- Referring to a property's type in a function/ action parameter no longer refers to the enclosing entity

## Version 0.20.1 - 2024-04-24
### Fixed
- Void actions no longer crash the type generation process
Expand Down
2 changes: 1 addition & 1 deletion lib/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class Visitor {
const paramType = this.resolver.resolveAndRequire(type, file)
return this.inlineDeclarationResolver.getPropertyDatatype(
paramType,
paramType.typeInfo.isArray ? paramType.typeName : paramType.typeInfo.inflection.singular
paramType.typeInfo.isArray || paramType.typeInfo.isDeepRequire ? paramType.typeName : paramType.typeInfo.inflection.singular
)
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cap-js/cds-typer",
"version": "0.20.1",
"version": "0.20.2",
"description": "Generates .ts files for a CDS model to receive code completion in VS Code",
"main": "index.js",
"repository": "github:cap-js/cds-typer",
Expand Down
13 changes: 11 additions & 2 deletions test/unit/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ describe('Actions', () => {

beforeAll(async () => {
paths = (await prepareUnitTest('actions/model.cds', locations.testOutput('actions_test'))).paths
astwBound = new ASTWrapper(path.join(paths[1], 'index.ts'))
astwUnbound = new ASTWrapper(path.join(paths[2], 'index.ts'))
astwBound = new ASTWrapper(path.join(paths.find(p => p.endsWith('S')), 'index.ts'))
astwUnbound = new ASTWrapper(path.join(paths.find(p => p.endsWith('actions_test')), 'index.ts'))
})

test('Bound', async () => {
Expand Down Expand Up @@ -183,4 +183,13 @@ describe('Actions', () => {
&& checkKeyword(type.args[1], 'never')
).toBe(true)
})

test ('typeof Parameter Referring to Correct Type', async () => {
checkFunction(astwUnbound.tree.find(node => node.name === 'freetypeof'), {
modifiersCheck: (modifiers = []) => !modifiers.some(check.isStatic),
callCheck: type => check.isNullable(type, [check.isVoid]),
returnTypeCheck: type => check.isNullable(type, [check.isVoid]),
parameterCheck: ({members: [fst]}) => check.isNullable(fst.type, [check.isIndexedAccessType])
})
})
})
7 changes: 6 additions & 1 deletion test/unit/files/actions/model.cds
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ using elsewhere.ExternalType from './elsewhere';
using elsewhere.ExternalType2 from './elsewhere';
using ExternalInRoot from './root';

entity Foo {
bar: Integer;
}

service S {
entity E {}
actions {
Expand Down Expand Up @@ -33,6 +37,7 @@ action free (param: String) returns { a: Integer; b: String } ;
action free2 () returns ExternalType;
action free3 () returns ExternalInRoot;
action freevoid ();
action freetypeof (p: Foo:bar);

action free4 () returns {
foo: ExternalType2
Expand All @@ -42,4 +47,4 @@ entity NoActions {}
entity ParameterlessActions {}
actions {
action a()
}
}

0 comments on commit 3120af3

Please sign in to comment.