Skip to content

Commit 9ebe980

Browse files
committed
fix(ElasticApiParser): Add rule to cleanUpSource(), cause dox does not understand methods which defined via square brackets
1 parent 6fed21d commit 9ebe980

File tree

3 files changed

+101
-49
lines changed

3 files changed

+101
-49
lines changed

src/ElasticApiParser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ export default class ElasticApiParser {
143143
static cleanUpSource(code: string): string {
144144
// remove invalid markup
145145
// {<<api-param-type-boolean,`Boolean`>>} converted to {Boolean}
146-
const codeCleaned = code.replace(/{<<.+`(.*)`.+}/gi, '{$1}');
146+
let codeCleaned = code.replace(/{<<.+`(.*)`.+}/gi, '{$1}');
147+
148+
// replace api.indices.prototype['delete'] = ca({
149+
// on api.indices.prototype.delete = ca({
150+
codeCleaned = codeCleaned.replace(/(api.*)\['(.+)'\](.*ca)/gi, '$1.$2$3');
147151

148152
return codeCleaned;
149153
}
@@ -203,7 +207,7 @@ export default class ElasticApiParser {
203207
}
204208

205209
static getMethodName(str: string): string | string[] {
206-
const parts = str.replace(/\['(.+)'\]/, '.$1').split('.');
210+
const parts = str.split('.');
207211
if (parts[0] === 'api') {
208212
parts.shift();
209213
}

src/__tests__/ElasticApiParser-test.js

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,6 @@ import ElasticApiParser from '../ElasticApiParser';
1717

1818
const apiPartialPath = path.resolve(__dirname, '../__mocks__/apiPartial.js');
1919

20-
const code = `
21-
api.cat.prototype.allocation = ca({
22-
params: {
23-
format: { type: 'string' },
24-
bytes: { type: 'enum', options: ['b', 'k', 'kb'] },
25-
local: { type: 'boolean' },
26-
masterTimeout: { type: 'time', name: 'master_timeout' },
27-
h: { type: 'list' },
28-
help: { type: 'boolean', 'default': false },
29-
v: { type: 'boolean', 'default': false }
30-
},
31-
urls: [
32-
{
33-
fmt: '/_cat/allocation/<%=nodeId%>',
34-
req: { nodeId: { type: 'list' }}
35-
}, {
36-
fmt: '/_cat/allocation'
37-
}, {
38-
fmt: '/<%=index%>/<%=type%>/_update_by_query',
39-
req: {
40-
index: {
41-
type: 'list'
42-
},
43-
type: {
44-
type: 'list'
45-
}
46-
}
47-
}, {
48-
fmt: '/<%=index%>/_update_by_query',
49-
req: {
50-
index: {
51-
type: 'list'
52-
}
53-
}
54-
}
55-
]
56-
});`;
57-
5820
describe('ElasticApiParser', () => {
5921
let parser;
6022

@@ -136,6 +98,24 @@ describe('ElasticApiParser', () => {
13698
});
13799
});
138100

101+
describe('cleanUpSource()', () => {
102+
it('should {<<api-param-type-boolean,`Boolean`>>} convert to {Boolean}', () => {
103+
expect(
104+
ElasticApiParser.cleanUpSource(
105+
`@param {<<api-param-type-boolean,\`Boolean\`>>} params.analyzeWildcard`
106+
)
107+
).toEqual(`@param {Boolean} params.analyzeWildcard`);
108+
});
109+
110+
it("should api['delete'] convert to api.delete", () => {
111+
expect(
112+
ElasticApiParser.cleanUpSource(
113+
`api.indices.prototype['delete'] = ca({`
114+
)
115+
).toEqual(`api.indices.prototype.delete = ca({`);
116+
});
117+
});
118+
139119
describe('parseParamsDescription()', () => {
140120
it('should return descriptions for fields', () => {
141121
const source = ElasticApiParser.cleanUpSource(
@@ -164,7 +144,47 @@ describe('ElasticApiParser', () => {
164144

165145
describe('codeToSettings()', () => {
166146
it('should return settings as object from ca({ settings })', () => {
167-
expect(ElasticApiParser.codeToSettings(code)).toMatchObject({
147+
expect(
148+
ElasticApiParser.codeToSettings(
149+
`
150+
api.cat.prototype.allocation = ca({
151+
params: {
152+
format: { type: 'string' },
153+
bytes: { type: 'enum', options: ['b', 'k', 'kb'] },
154+
local: { type: 'boolean' },
155+
masterTimeout: { type: 'time', name: 'master_timeout' },
156+
h: { type: 'list' },
157+
help: { type: 'boolean', 'default': false },
158+
v: { type: 'boolean', 'default': false }
159+
},
160+
urls: [
161+
{
162+
fmt: '/_cat/allocation/<%=nodeId%>',
163+
req: { nodeId: { type: 'list' }}
164+
}, {
165+
fmt: '/_cat/allocation'
166+
}, {
167+
fmt: '/<%=index%>/<%=type%>/_update_by_query',
168+
req: {
169+
index: {
170+
type: 'list'
171+
},
172+
type: {
173+
type: 'list'
174+
}
175+
}
176+
}, {
177+
fmt: '/<%=index%>/_update_by_query',
178+
req: {
179+
index: {
180+
type: 'list'
181+
}
182+
}
183+
}
184+
]
185+
});`
186+
)
187+
).toMatchObject({
168188
params: {
169189
bytes: { options: ['b', 'k', 'kb'], type: 'enum' },
170190
format: { type: 'string' },
@@ -201,20 +221,12 @@ describe('ElasticApiParser', () => {
201221
expect(ElasticApiParser.getMethodName('api.updateByQuery')).toEqual(
202222
'updateByQuery'
203223
);
204-
205-
expect(ElasticApiParser.getMethodName(`api['delete']`)).toEqual(
206-
'delete'
207-
);
208224
});
209225

210226
it('should return array of string', () => {
211227
expect(
212228
ElasticApiParser.getMethodName('api.cat.prototype.allocation')
213229
).toEqual(['cat', 'allocation']);
214-
215-
expect(
216-
ElasticApiParser.getMethodName(`api.indices.prototype['delete']`)
217-
).toEqual(['indices', 'delete']);
218230
});
219231
});
220232

src/__tests__/__snapshots__/ElasticApiParser-test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ Object {
206206
"resolve": [Function],
207207
"type": "Elastic_Cat",
208208
},
209+
"indices": Object {
210+
"resolve": [Function],
211+
"type": "Elastic_Indices",
212+
},
209213
"search": Object {
210214
"args": Object {
211215
"_source": Object {
@@ -590,6 +594,38 @@ Object {
590594
"allocation",
591595
],
592596
},
597+
"indices.delete": Object {
598+
"argsDescriptions": Object {
599+
"index": "A comma-separated list of indices to delete; use \`_all\` or \`*\` string to delete all indices",
600+
"masterTimeout": "Specify timeout for connection to master",
601+
"timeout": "Explicit operation timeout",
602+
},
603+
"argsSettings": Object {
604+
"method": "DELETE",
605+
"params": Object {
606+
"masterTimeout": Object {
607+
"name": "master_timeout",
608+
"type": "time",
609+
},
610+
"timeout": Object {
611+
"type": "time",
612+
},
613+
},
614+
"url": Object {
615+
"fmt": "/<%=index%>",
616+
"req": Object {
617+
"index": Object {
618+
"type": "list",
619+
},
620+
},
621+
},
622+
},
623+
"description": "Perform a [indices.delete](https://www.elastic.co/guide/en/elasticsearch/reference/5.x/indices-delete-index.html) request",
624+
"elasticMethod": Array [
625+
"indices",
626+
"delete",
627+
],
628+
},
593629
"search": Object {
594630
"argsDescriptions": Object {
595631
"[params.defaultOperator=OR]": "The default operator for query string query (AND or OR)",

0 commit comments

Comments
 (0)