Skip to content

Commit eaa60f9

Browse files
committed
Interface merging, no comment merging
1 parent 7194fc0 commit eaa60f9

File tree

9 files changed

+847
-1365
lines changed

9 files changed

+847
-1365
lines changed

package.json

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devonfw/ts-merger",
3-
"version": "2.1.1",
3+
"version": "2.2.0",
44
"description": "2-way TypeScript Merger",
55
"author": {
66
"name": "Capgemini",
@@ -19,7 +19,7 @@
1919
"test": "mocha-webpack --webpack-config webpack.config.js \"test/**/*_test.ts\"",
2020
"test-watch": "mocha-webpack --webpack-config webpack.config.js --watch \"test/**/*_test.ts\"",
2121
"prepublish": "npm run build && npm run test && npm run bundle",
22-
"bundle": "webpack --define process.env.NODE_ENV=\"production\""
22+
"bundle": "webpack -p"
2323
},
2424
"keywords": [
2525
"TypeScript",
@@ -28,22 +28,20 @@
2828
"license": "Apache-2.0",
2929
"devDependencies": {
3030
"@types/chai": "^3.4.35",
31-
"@types/mocha": "^5.2.5",
32-
"@types/node": "^10.12.21",
33-
"chai": "^4.1.0",
31+
"@types/mocha": "^2.2.39",
32+
"@types/node": "^7.0.22",
33+
"chai": "^3.5.0",
3434
"cpx": "^1.5.0",
35-
"mocha": "^5.0.1",
35+
"mocha": "^3.2.0",
3636
"mocha-webpack": "1.1.0",
3737
"source-map-support": "0.4.3",
3838
"ts-loader": "^2.1.0",
3939
"tslint": "^5.5.0",
4040
"tslint-language-service": "^0.9.6",
4141
"typescript": "^2.2.1",
42-
"webpack": "^3.0.0"
43-
},
44-
"dependencies": {
45-
"tsutils": "^3.8.0"
42+
"webpack": "^3.4.1"
4643
},
44+
"dependencies": {},
4745
"directories": {
4846
"test": "test"
4947
},

src/components/classDeclaration/ClassDeclaration.ts

-49
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import { PropertyDeclaration } from './members/property/PropertyDeclaration';
33
import { Decorator } from '../decorator/Decorator';
44
import { FileDeclaration } from '../general/FileDeclaration';
55
import { Method } from './members/method/Method';
6-
import { forEachComment } from 'tsutils/util';
7-
import * as ts from 'typescript/lib/typescript';
86
/**
97
* Defines the structure of class objects
108
*
@@ -15,7 +13,6 @@ export class ClassDeclaration extends FileDeclaration {
1513
private decorators: Decorator[];
1614
private methods: Method[];
1715
private properties: PropertyDeclaration[];
18-
private comments: string[];
1916
private construct: Constructor;
2017

2118
constructor() {
@@ -24,7 +21,6 @@ export class ClassDeclaration extends FileDeclaration {
2421
this.methods = [];
2522
this.properties = [];
2623
this.construct = new Constructor();
27-
this.comments = [];
2824
}
2925

3026
getConstructor() {
@@ -84,54 +80,9 @@ export class ClassDeclaration extends FileDeclaration {
8480
this.methods = methods;
8581
}
8682

87-
addComment(comment: string) {
88-
this.comments.push(comment);
89-
}
90-
91-
getComments() {
92-
return this.comments;
93-
}
94-
95-
setComments(comments: string[]) {
96-
this.comments = comments;
97-
}
98-
99-
parseComments(fileClass: ts.Node, sourceFile: ts.SourceFile) {
100-
// Now I need the position of the declared class
101-
let text: string = sourceFile.getFullText();
102-
let regex: string = 'class +(' + this.getIdentifier() + ')';
103-
104-
let match = text.match(regex);
105-
106-
if (match == null) return;
107-
108-
let declarationPos: number = text.indexOf(match[0]);
109-
forEachComment(
110-
fileClass,
111-
(sourceFile, comment) => {
112-
if (comment.end < declarationPos) {
113-
let commentText: string = sourceFile.substring(
114-
comment.pos,
115-
comment.end,
116-
);
117-
this.comments.push(commentText);
118-
}
119-
},
120-
sourceFile,
121-
);
122-
}
123-
12483
toString(): String {
12584
let classDeclaration: String[] = [];
12685

127-
if (this.comments.length > 0) {
128-
this.comments.forEach((comment) => {
129-
classDeclaration.push(comment);
130-
classDeclaration.push('\n');
131-
});
132-
}
133-
classDeclaration.push('\n');
134-
13586
this.decorators.forEach((decorator) => {
13687
classDeclaration.push(decorator.toString(), '\n');
13788
});

src/components/interfaceDeclaration/InterfaceDeclaration.ts

-49
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { FileDeclaration } from '../general/FileDeclaration';
22
import { InterfaceProperty } from './members/InterfaceProperty';
33
import { InterfaceMethod } from './members/method/InterfaceMethod';
4-
import { forEachComment } from 'tsutils/util';
5-
import * as ts from 'typescript/lib/typescript';
64
/**
75
* Defines the structure of interface objects
86
*
@@ -12,14 +10,12 @@ import * as ts from 'typescript/lib/typescript';
1210
export class InterfaceDeclaration extends FileDeclaration {
1311
private methods: InterfaceMethod[];
1412
private properties: InterfaceProperty[];
15-
private comments: string[];
1613
private index: string;
1714

1815
constructor() {
1916
super();
2017
this.methods = [];
2118
this.properties = [];
22-
this.comments = [];
2319
}
2420

2521
getIndex() {
@@ -62,54 +58,9 @@ export class InterfaceDeclaration extends FileDeclaration {
6258
this.methods = methods;
6359
}
6460

65-
addComment(comment: string) {
66-
this.comments.push(comment);
67-
}
68-
69-
getComments() {
70-
return this.comments;
71-
}
72-
73-
setComments(comments: string[]) {
74-
this.comments = comments;
75-
}
76-
77-
parseComments(fileInterface: ts.Node, sourceFile: ts.SourceFile) {
78-
// Now I need the position of the declared class
79-
let text: string = sourceFile.getFullText();
80-
let regex: string = 'interface +(' + this.getIdentifier() + ')';
81-
82-
let match = text.match(regex);
83-
84-
if (match == null) return;
85-
86-
let declarationPos: number = text.indexOf(match[0]);
87-
forEachComment(
88-
fileInterface,
89-
(sourceFile, comment) => {
90-
if (comment.end < declarationPos) {
91-
let commentText: string = sourceFile.substring(
92-
comment.pos,
93-
comment.end,
94-
);
95-
this.comments.push(commentText);
96-
}
97-
},
98-
sourceFile,
99-
);
100-
}
101-
10261
toString(): String {
10362
let interfaceDeclaration: String[] = [];
10463

105-
if (this.comments.length > 0) {
106-
this.comments.forEach((comment) => {
107-
interfaceDeclaration.push(comment);
108-
interfaceDeclaration.push('\n');
109-
});
110-
}
111-
interfaceDeclaration.push('\n');
112-
11364
super.getModifiers().forEach((modifier) => {
11465
interfaceDeclaration.push(modifier, ' ');
11566
});

src/tools/MappingTools.ts

-2
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ export function mapClass(
348348
break;
349349
}
350350
});
351-
classTo.parseComments(fileClass, sourceFile);
352351
}
353352
return classTo;
354353
}
@@ -393,7 +392,6 @@ export function mapInterface(fileInterface: any, sourceFile: ts.SourceFile) {
393392
break;
394393
}
395394
});
396-
interfaceTo.parseComments(fileInterface, sourceFile);
397395
}
398396
return interfaceTo;
399397
}

src/tools/MergerTools.ts

+1-32
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@ export function mergeClass(
7474
patchOverrides,
7575
);
7676

77-
mergeComments(
78-
baseClass.getComments(),
79-
patchClass.getComments(),
80-
patchOverrides,
81-
);
82-
8377
mergeDecorators(
8478
baseClass.getDecorators(),
8579
patchClass.getDecorators(),
@@ -114,11 +108,6 @@ export function mergeInterface(
114108
patchOverrides,
115109
);
116110

117-
mergeComments(
118-
baseInterface.getComments(),
119-
patchInterface.getComments(),
120-
patchOverrides,
121-
);
122111
mergeInterfaceProperties(
123112
baseInterface.getProperties(),
124113
patchInterface.getProperties(),
@@ -265,26 +254,6 @@ export function mergeHeritages(
265254
}
266255
}
267256

268-
export function mergeComments(
269-
baseComments: string[],
270-
patchComments: string[],
271-
patchOverrides: boolean,
272-
) {
273-
let exists: boolean;
274-
275-
patchComments.forEach((patchComment, index) => {
276-
let isNotRemoved: boolean = true;
277-
baseComments.forEach((baseComment) => {
278-
if (patchOverrides) {
279-
if (isNotRemoved) {
280-
baseComments.splice(index, 1, patchComment);
281-
isNotRemoved = false;
282-
}
283-
}
284-
});
285-
});
286-
}
287-
288257
export function mergeInterfaceProperties(
289258
baseProperties: InterfaceProperty[],
290259
patchProperties: InterfaceProperty[],
@@ -392,4 +361,4 @@ export function mergeFunction(
392361
patchOverrides: boolean,
393362
) {
394363
baseFunction.merge(patchFunction, patchOverrides);
395-
}
364+
}

test/class_test.ts

-37
Original file line numberDiff line numberDiff line change
@@ -155,41 +155,4 @@ describe('Merging class declarations', () => {
155155
});
156156
});
157157

158-
describe('comments on class definitions', () => {
159-
const base = `/* Test this */
160-
// Bla test
161-
class a { }`,
162-
patch = `/* Test that */
163-
// Ble test
164-
class a {}`;
165-
166-
it('should be accumulated.', () => {
167-
const result: String[] = merge(base, patch, false)
168-
.split('\n') // get each individual line
169-
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
170-
.filter((value) => value != ''); // remove empty lines
171-
expect(result.indexOf('/* Test this */')).to.be.greaterThan(
172-
-1,
173-
'first base comment should be present in class a',
174-
);
175-
expect(result.indexOf('// Bla test')).to.be.greaterThan(
176-
0,
177-
'second base comment should be present in class a',
178-
);
179-
});
180-
it('should accumulate patch comments with patchOverride.', () => {
181-
const result: String[] = merge(base, patch, true)
182-
.split('\n') // get each individual line
183-
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
184-
.filter((value) => value != ''); // remove empty lines
185-
expect(result.indexOf('/* Test that */')).to.be.greaterThan(
186-
-1,
187-
'first patch comment should be present in class',
188-
);
189-
expect(result.indexOf('// Ble test')).to.be.greaterThan(
190-
0,
191-
'second patch comment should be present in class',
192-
);
193-
});
194-
});
195158
});

test/interface_test.ts

+2-40
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('Merging interface declarations', () => {
7171
let regex = /\s*interface\s+[ab]\s*\{\}\s*interface\s+[ab].*/;
7272
expect(regex.test(result.toString())).true;
7373
});
74-
it('should be accumulated with patchOverride (should not make a difference).', () => {
74+
it('should be accumulated with patchOverride (should not make any difference).', () => {
7575
const result: String = merge(patch, base, false)
7676
.split('\n')
7777
.map((value) => value.trim())
@@ -97,7 +97,7 @@ describe('Merging interface declarations', () => {
9797
).length,
9898
).to.be.equal(1, 'misformed class declaration');
9999
});
100-
it('should be accumulated with patchOverride (should not make a difference).', () => {
100+
it('should be accumulated with patchOverride (should not make any difference).', () => {
101101
const result: String[] = merge(base, patch, true)
102102
.split('\n') // get each individual line
103103
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
@@ -109,42 +109,4 @@ describe('Merging interface declarations', () => {
109109
).to.be.equal(1, 'misformed class declaration');
110110
});
111111
});
112-
113-
describe('multiple comments on interface definitions', () => {
114-
const base = `/* Test this */
115-
// Bla test
116-
interface a { }`,
117-
patch = `/* Test that */
118-
// Ble test
119-
interface a {}`;
120-
121-
it('should be accumulated.', () => {
122-
const result: String[] = merge(base, patch, false)
123-
.split('\n') // get each individual line
124-
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
125-
.filter((value) => value != ''); // remove empty lines
126-
expect(result.indexOf('/* Test this */')).to.be.greaterThan(
127-
-1,
128-
'first base comment should be present in interface a',
129-
);
130-
expect(result.indexOf('// Bla test')).to.be.greaterThan(
131-
0,
132-
'second base comment should be present in interface a',
133-
);
134-
});
135-
it('should be accumulated with patchOverride (should not make a difference).', () => {
136-
const result: String[] = merge(base, patch, true)
137-
.split('\n') // get each individual line
138-
.map((value) => value.trim()) // trim all lines (no white spaces at the beginning and end of a line)
139-
.filter((value) => value != ''); // remove empty lines
140-
expect(result.indexOf('/* Test that */')).to.be.greaterThan(
141-
-1,
142-
'first patch comment should be present in interface',
143-
);
144-
expect(result.indexOf('// Ble test')).to.be.greaterThan(
145-
0,
146-
'second patch comment should be present in interface',
147-
);
148-
});
149-
});
150112
});

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
"module": "commonjs",
4-
"target": "es2015",
4+
"target": "es5",
55
"noImplicitAny": false,
66
"sourceMap": true,
77
"outDir": "build",

0 commit comments

Comments
 (0)