Skip to content

Commit 5cc9a7d

Browse files
committedFeb 5, 2019
Index signature merged
1 parent 305998c commit 5cc9a7d

File tree

4 files changed

+42
-10
lines changed

4 files changed

+42
-10
lines changed
 

‎src/components/interfaceDeclaration/InterfaceDeclaration.ts

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ export class InterfaceDeclaration extends FileDeclaration {
7878
});
7979
}
8080

81+
if (this.index != null && this.index.length > 0) {
82+
interfaceDeclaration.push(this.index);
83+
}
84+
8185
interfaceDeclaration.push('\n}\n');
8286

8387
return interfaceDeclaration.join('');

‎src/tools/MergerTools.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ export function mergeInterface(
109109
patchInterface.getMethods(),
110110
patchOverrides,
111111
);
112-
mergeIndexSignature(
113-
baseInterface.getIndex(),
114-
patchInterface.getIndex(),
115-
patchOverrides,
112+
baseInterface.setIndex(
113+
mergeIndexSignature(
114+
baseInterface.getIndex(),
115+
patchInterface.getIndex(),
116+
patchOverrides,
117+
),
116118
);
117119
}
118120

@@ -191,7 +193,12 @@ export function mergeIndexSignature(
191193
baseIndex: string,
192194
patchIndex: string,
193195
patchOverrides: boolean,
194-
) {}
196+
): string {
197+
if (patchOverrides) {
198+
baseIndex = patchIndex;
199+
}
200+
return baseIndex;
201+
}
195202

196203
export function mergeInterfaceProperties(
197204
baseProperties: InterfaceProperty[],

‎test/export_test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ import { expect } from 'chai';
33
import 'mocha';
44

55
describe('Merging exports', () => {
6-
let testResources = './test/resources/export/';
7-
let baseTestResources = testResources + 'base/';
8-
let patchTestResources = testResources + 'patch/';
9-
let outputTestTempResources = testResources + 'output/';
10-
116
describe('should accumulate exports from', () => {
127
describe('disjunct export sets', () => {
138
const base = `export { a } from 'b';

‎test/interface_field_test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,30 @@ describe('Merging interface fields', () => {
8080
);
8181
});
8282
});
83+
84+
describe('should add the index signature from', () => {
85+
const base = `interface a { [key: string]: any; }`,
86+
patch = `interface a { [key: string]: string; }`;
87+
88+
it('from the patch.', () => {
89+
const result: String[] = merge(base, patch, false)
90+
.split('\n') // get each individual line
91+
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
92+
.filter((value) => value != ''); // remove empty lines
93+
expect(result.indexOf('[key: string]: any;')).to.be.greaterThan(
94+
0,
95+
'base index should be present in interface a',
96+
);
97+
});
98+
it('from the patch with patchOverride.', () => {
99+
const result: String[] = merge(base, patch, true)
100+
.split('\n')
101+
.map((value) => value.trim())
102+
.filter((value) => value != '');
103+
expect(result.indexOf('[key: string]: string;')).to.be.greaterThan(
104+
0,
105+
'declaration should be present in interface a',
106+
);
107+
});
108+
});
83109
});

0 commit comments

Comments
 (0)
Please sign in to comment.