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

fix return type become tuple type from interface type #124

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-gyb",
"version": "0.12.0",
"version": "0.12.1",
"description": "Generate Native API based on TS interface",
"repository": {
"type": "git",
Expand Down
38 changes: 15 additions & 23 deletions src/parser/ValueParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ export class ValueParser {
}

let nullable = false;
let isTuple = false;
const valueTypes: ValueType[] = [];
const tupleMembers: Field[] = [];
const valueTypes: NonEmptyType[] = [];
let hasTupleType = false;
const tupleOrInterfaceTypes: (TupleType | InterfaceType)[] = [];
const literalValues: {
type: BasicTypeValue.string | BasicTypeValue.number;
value: Value;
Expand All @@ -295,23 +295,15 @@ export class ValueParser {
return;
}

const newValueType = this.valueTypeFromTypeNode(typeNode);
const newValueType = this.valueTypeFromTypeNode(typeNode) as NonEmptyType;

if (valueTypes.length === 0) {
isTuple = isInterfaceType(newValueType) || isTupleType(newValueType);
valueTypes.push(newValueType);
if (isTupleType(newValueType)) {
hasTupleType = true;
tupleOrInterfaceTypes.push(newValueType);
}

if (isTuple) {
if (isInterfaceType(newValueType) || isTupleType(newValueType)) {
tupleMembers.push(...newValueType.members);
} else {
throw new ValueParserError(
`type ${node.getText()} is invalid`,
'Use `multiple tuple types` or `union value types` or `type union`'
);
}
} else {
valueTypes.push(newValueType);
if (isInterfaceType(newValueType)) {
tupleOrInterfaceTypes.push(newValueType);
}
});

Expand Down Expand Up @@ -348,24 +340,24 @@ export class ValueParser {
return literalKind;
}

if (valueTypes.length === 0 && tupleMembers.length === 0) {
if (valueTypes.length === 0) {
throw new ValueParserError(
`type ${node.getText()} is invalid`,
'Type must contain one supported non empty type'
);
}

if (valueTypes.length > 0 && tupleMembers.length > 0) {
if (hasTupleType && valueTypes.length !== tupleOrInterfaceTypes.length) {
throw new ValueParserError(
`mixing ${node.getText()} is invalid`,
'Type must contain types or tuples'
);
}

if (isTuple) {
if (hasTupleType) {
const value: TupleType = {
kind: ValueTypeKind.tupleType,
members: tupleMembers,
members: tupleOrInterfaceTypes.map((type) => type.members).flat(),
};
if (nullable) {
const optionalType: OptionalType = {
Expand All @@ -378,7 +370,7 @@ export class ValueParser {
}

if (valueTypes.length === 1) {
if (!isOptionalType(valueTypes[0]) && nullable) {
if (nullable) {
return {
kind: ValueTypeKind.optionalType,
wrappedType: valueTypes[0],
Expand Down
39 changes: 37 additions & 2 deletions test/value-parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ describe('ValueParser', () => {
expect(parseFunc()).to.deep.equal({ name: 'mockedMethod', parameters: [], returnType: null, isAsync: true, documentation: '', });
})
});

it('Return type', () => {
let customTypesCode = `
export interface SelectionRect {
start: number;
}
`
const methodCode = `
mockedMethod(): SelectionRect;
`;
withTempMethodParser(methodCode, parseFunc => {
expect(parseFunc()).to.deep.equal({
name: 'mockedMethod',
parameters: [],
returnType: {
name: 'SelectionRect',
customTags: {},
methods: [],
documentation: '',
kind: 'interfaceType',
members: [
{
documentation: '',
name: 'start',
type: {
kind: 'basicType',
value: 'number',
},
},
],
},
isAsync: false,
documentation: '',
});
}, new Set(), customTypesCode)
});
});

describe('Parameters type', () => {
Expand Down Expand Up @@ -184,7 +220,6 @@ describe('ValueParser', () => {
}
`;
testValueType('merged interface and tuple', 'InterfaceWithStringField | { numberField: number }', tupleWithMembersType, new Set(), interfacesCode);
testValueType('merged interfaces to tuple', 'InterfaceWithStringField | InterfaceWithNumberField', tupleWithMembersType, new Set(), interfacesCode);
})

describe('Parse enum type', () => {
Expand Down Expand Up @@ -287,7 +322,7 @@ describe('ValueParser', () => {
testValueType('string interface dictionary', 'DictionaryInterface', { kind: ValueTypeKind.dictionaryType, keyType: DictionaryKeyType.string, valueType: numberType }, new Set(), dictionaryCode);
});

describe('Parse optional type', () => {
describe('Parse union type', () => {
it('Empty types union', () => {
const valueTypeCode = 'null | undefined';
withTempValueParser(valueTypeCode, parseFunc => {
Expand Down
Loading