Skip to content

Commit 0585b51

Browse files
committed
handle duplicated TypeDeclaration names in sort, fix #59
1 parent 861dc6c commit 0585b51

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
1414
high state of flux, you're at risk of it changing without notice.
1515

16+
# 0.4.5
17+
18+
- **Bug Fix**
19+
- handle duplicated `TypeDeclaration` names in `sort`, fix #59 (@gcanti)
20+
1621
# 0.4.4
1722

1823
- **Bug Fix**

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "io-ts-codegen",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"description": "Code generation for io-ts",
55
"files": [
66
"lib"

src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,11 @@ export const getNodeDependencies = (node: Node): Array<string> => {
622622
export function getTypeDeclarationGraph(declarations: Array<TypeDeclaration | CustomTypeDeclaration>): Graph {
623623
const graph: Graph = {}
624624
declarations.forEach(d => {
625-
const vertex = (graph[d.name] = new Vertex(d.name))
625+
const name = d.name
626+
if (graph.hasOwnProperty(name)) {
627+
throw new Error(`duplicated name: ${JSON.stringify(name)}`)
628+
}
629+
const vertex = (graph[name] = new Vertex(name))
626630
vertex.afters.push(...getNodeDependencies(d))
627631
})
628632
return graph

test/sort.ts

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ import * as assert from 'assert'
22
import * as t from '../src'
33

44
describe('sort', () => {
5+
it('should handle duplicate declarations', () => {
6+
const declarations: Array<t.TypeDeclaration> = [
7+
t.typeDeclaration('A', t.stringType),
8+
t.typeDeclaration('A', t.numberType)
9+
]
10+
assert.throws(
11+
() => {
12+
t.sort(declarations)
13+
},
14+
(e: any) => e.message === 'duplicated name: "A"'
15+
)
16+
})
17+
518
it('should handle custom type declarations', () => {
619
const declarations = [
720
t.typeDeclaration('Person', t.typeCombinator([t.property('id', t.identifier('UserId'))]), true),

0 commit comments

Comments
 (0)