Skip to content

Commit

Permalink
feat: add new feature to match analyzed route in-property accessing o…
Browse files Browse the repository at this point in the history
…f route properties
  • Loading branch information
dalisoft committed Oct 17, 2021
1 parent e1dabac commit 841bc91
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 6 deletions.
109 changes: 109 additions & 0 deletions src/extractors/scopes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
export default (line: string): string[] => {
const matches = [];
const cases = {
SCOPE: 0,
CURVE_SCOPE: 0,
QUOTE: 0,
TRAILING: 0,
ENDLINES: 0,
DOTS: 0,
COMMAS: 0
};

let rebuild = '';
for (const char of line) {
switch (char) {
case '(': {
cases.SCOPE++;

rebuild += char;
break;
}
case "'":
case '"': {
cases.QUOTE++;

rebuild += char;
break;
}
case '{': {
cases.CURVE_SCOPE++;

rebuild += char;
break;
}
case ')': {
if (cases.SCOPE > 0) {
cases.SCOPE++;
rebuild += char;
} else {
// rebuild = '';
}
break;
}
case '}': {
if (cases.CURVE_SCOPE > 0) {
cases.CURVE_SCOPE++;

if (rebuild.length > 0) {
matches.push(rebuild);
rebuild = '';
}
}
break;
}
case ':':
case '=': {
cases.ENDLINES++;

if (rebuild.length > 0) {
matches.push(rebuild);
rebuild = '';
}
break;
}
case '.': {
cases.DOTS++;

if (rebuild.length > 0) {
matches.push(rebuild);
rebuild = '';
}
break;
}
case ',': {
cases.COMMAS++;

if (rebuild.length > 0) {
matches.push(rebuild);
rebuild = '';
}
break;
}
case ' ': {
break;
}
case ';': {
if (cases.ENDLINES > 0) {
cases.ENDLINES++;

if (rebuild.length > 0) {
matches.push(rebuild);
rebuild = '';
}
}
break;
}
default: {
rebuild += char;

break;
}
}
}
if (rebuild.length > 0) {
matches.push(rebuild);
}

return matches;
};
26 changes: 23 additions & 3 deletions src/finders/request/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IBlock } from '../../../types/interfaces';
import scopeExtractor from '../../extractors/scopes';
import requestModeBodyFinder from './body';
import requestModeCookiesFinder from './cookies';
import requestModeHeadersFinder from './headers';
Expand All @@ -10,13 +11,29 @@ export default function caseRequestModeFinder(
line: string,
index: number
): IBlock | null | undefined {
const requestPropertyCaseFindMatch = line.match(/req.(.*)?(;|\.|\()/);
const requestPropertyCaseFindMatch = line.match(
/(request|req).(.*)?(;|\.|\()/
);

let res = null;
let input;
let _;
let _key;
let _skey;

if (requestPropertyCaseFindMatch) {
[_, _key] = requestPropertyCaseFindMatch;
[input] = requestPropertyCaseFindMatch;
[_, _key, _skey] = scopeExtractor(input);

if (_skey) {
return {
link: _skey,
linked: false,
line_index: index,
key: _skey,
mode: _key as any
};
}

// Headers matching
res = requestModeHeadersFinder(_, _key, line, index);
Expand All @@ -38,6 +55,7 @@ export default function caseRequestModeFinder(

// Params matching
res = requestModeParamsFinder(_, _key, line, index);

if (res !== undefined) {
if (res !== null) {
res.mode = 'params';
Expand Down Expand Up @@ -73,9 +91,11 @@ export default function caseRequestModeFinder(
}
} else {
// Method matching
const reqIndex = line.indexOf('req');
const requestIndex = line.indexOf('request');
res = requestMethodFinder(
_ as unknown as string,
line.substr(line.indexOf('req')),
line.substr(requestIndex !== -1 ? requestIndex : reqIndex),
line,
index
);
Expand Down
12 changes: 9 additions & 3 deletions src/finders/request/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ export default function requestMethodFinder(
return undefined;
}

if (extracted[2].includes('req.') && !extracted[2].includes('()')) {
if (
(extracted[2].includes('req.') || extracted[2].includes('request.')) &&
!extracted[2].includes('()')
) {
return {
link: extracted[1],
linked: false,
Expand All @@ -27,7 +30,10 @@ export default function requestMethodFinder(
// uWebSockets.js has native `getMethod` and `getUrl` support
return undefined;
}
if (_key === 'req;' && extracted[1].charAt(0) === '{') {
if (
(_key === 'req;' || _key === 'request;') &&
extracted[1].charAt(0) === '{'
) {
key = extracted[1].substr(1);
key = key.substr(0, key.length - 1).trim();
link = key;
Expand All @@ -43,7 +49,7 @@ export default function requestMethodFinder(
key
};
}
if (_key.endsWith('req')) {
if (_key.endsWith('req') || _key.endsWith('request')) {
return {
link: extracted[1],
linked: false,
Expand Down
13 changes: 13 additions & 0 deletions tests/analyze.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ describe('request property parse', () => {
mode: 'params'
}
]);
expect(
analyze((request, response) => {
response.end(request.params.id);
})
).toEqual([
{
key: 'id',
line_index: 1,
link: 'id',
linked: false,
mode: 'params'
}
]);
});
it('req.query', () => {
expect(
Expand Down

0 comments on commit 841bc91

Please sign in to comment.