Skip to content

Commit f781b20

Browse files
jonchardyaciccarello
authored andcommitted
Category fixes (TypeStrong#1008)
- Fix an issue where if no members were categorized during lump categorization, they would still be put in the default category instead of not using categories. - Strip out category tag from comment upon categorizing
1 parent bb23e6d commit f781b20

File tree

4 files changed

+1270
-44
lines changed

4 files changed

+1270
-44
lines changed

src/lib/converter/plugins/CategoryPlugin.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ export class CategoryPlugin extends ConverterComponent {
113113
}
114114

115115
private lumpCategorize(obj: ContainerReflection) {
116-
if (obj instanceof ContainerReflection) {
117-
if (obj.children && obj.children.length > 0) {
118-
obj.categories = CategoryPlugin.getReflectionCategories(obj.children);
119-
}
120-
if (obj.categories && obj.categories.length > 1) {
121-
obj.categories.sort(CategoryPlugin.sortCatCallback);
122-
}
116+
if (!obj.children || obj.children.length === 0) {
117+
return;
118+
}
119+
obj.categories = CategoryPlugin.getReflectionCategories(obj.children);
120+
if (obj.categories && obj.categories.length > 1) {
121+
obj.categories.sort(CategoryPlugin.sortCatCallback);
122+
} else if (obj.categories.length === 1 && obj.categories[0].title === CategoryPlugin.defaultCategory) {
123+
// no categories if everything is uncategorized
124+
obj.categories = undefined;
123125
}
124126
}
125127

@@ -167,11 +169,11 @@ export class CategoryPlugin extends ConverterComponent {
167169
function extractCategoryTag(comment: Comment) {
168170
const tags = comment.tags;
169171
if (tags) {
170-
for (let i = 0; i < tags.length; i++) {
171-
if (tags[i].tagName === 'category') {
172-
let tag = tags[i].text;
173-
return tag.trim();
174-
}
172+
const tagIndex = tags.findIndex(tag => tag.tagName === 'category');
173+
if (tagIndex >= 0) {
174+
const tag = tags[tagIndex].text;
175+
tags.splice(tagIndex, 1);
176+
return tag.trim();
175177
}
176178
}
177179
return '';

src/test/converter.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,60 @@ describe('Converter', function() {
9898
});
9999
});
100100

101+
describe('Converter with categorizeByGroup=false', function() {
102+
const base = Path.join(__dirname, 'converter');
103+
const categoryDir = Path.join(base, 'category');
104+
const classDir = Path.join(base, 'class');
105+
let app: Application;
106+
107+
before('constructs', function() {
108+
app = new Application({
109+
mode: 'Modules',
110+
logger: 'none',
111+
target: 'ES5',
112+
module: 'CommonJS',
113+
experimentalDecorators: true,
114+
categorizeByGroup: false,
115+
jsx: 'react'
116+
});
117+
});
118+
119+
let result: ProjectReflection | undefined;
120+
121+
describe('category', () => {
122+
it('converts fixtures', function() {
123+
resetReflectionID();
124+
result = app.convert(app.expandInputFiles([categoryDir]));
125+
Assert(result instanceof ProjectReflection, 'No reflection returned');
126+
});
127+
128+
it('matches specs', function() {
129+
const specs = JSON.parse(FS.readFileSync(Path.join(categoryDir, 'specs-with-lump-categories.json')).toString());
130+
let data = JSON.stringify(result!.toObject(), null, ' ');
131+
data = data.split(normalizePath(base)).join('%BASE%');
132+
133+
compareReflections(JSON.parse(data), specs);
134+
});
135+
});
136+
137+
// verify that no categories are used when not specified during lump categorization
138+
describe('class', () => {
139+
it('converts fixtures', function() {
140+
resetReflectionID();
141+
result = app.convert(app.expandInputFiles([classDir]));
142+
Assert(result instanceof ProjectReflection, 'No reflection returned');
143+
});
144+
145+
it('matches specs', function() {
146+
const specs = JSON.parse(FS.readFileSync(Path.join(classDir, 'specs.json')).toString());
147+
let data = JSON.stringify(result!.toObject(), null, ' ');
148+
data = data.split(normalizePath(base)).join('%BASE%');
149+
150+
compareReflections(JSON.parse(data), specs);
151+
});
152+
});
153+
});
154+
101155
describe('Converter with excludeNotExported=true', function() {
102156
const base = Path.join(__dirname, 'converter');
103157
const exportWithLocalDir = Path.join(base, 'export-with-local');

0 commit comments

Comments
 (0)