Skip to content

Commit e7fcb43

Browse files
authored
Merge pull request #21 from sirbrillig/add-js-colon-def
Add support for JS properties and typedefs
2 parents e646cc2 + 5f72e80 commit e7fcb43

File tree

6 files changed

+236
-16
lines changed

6 files changed

+236
-16
lines changed

src/general.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const readdir = util.promisify(fs.readdir);
3535

3636
/**
3737
* @typedef {object} SearchConfig
38-
* @property {FileType|null} type
39-
* @property {boolean} verbose
40-
* @property {boolean} showLineNumbers
41-
* @property {boolean} disableColor
38+
* @property {FileType|null} [type]
39+
* @property {boolean} [verbose]
40+
* @property {boolean} [showLineNumbers]
41+
* @property {boolean} [disableColor]
4242
* @property {SearchTool} searchTool
4343
* @property {Glob} path
4444
*/

src/lang/js.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,33 @@
11
// @format
22

3+
const declarationKeywords = [
4+
'let',
5+
'var',
6+
'const',
7+
'function',
8+
'class',
9+
'interface',
10+
'type',
11+
];
12+
313
function getRegexp(symbol) {
4-
return `\\b((let\\s|var\\s|const\\s|function\\s|class\\s|interface\\s|type\\s|prototype\\.)${symbol}\\b|${symbol}\\([^)]*\\)\\s*\\{)`;
14+
const symbolWithDeclaration = `\\b(${declarationKeywords.map(addTrailingSpace).join('|')})${symbol}\\b`;
15+
const prototypeDeclaration = `\\bprototype\\.${symbol}\\b`
16+
const methodShorthand = `\\b${symbol}\\([^)]*\\)\\s*(:[^{]+)?\\{`;
17+
const propertyLonghand = `\\b${symbol}:\\s*`;
18+
const typedef = `@typedef\\s(\\{[^}]+\\}\\s)?${symbol}\\b`;
19+
const regexpParts = [
20+
symbolWithDeclaration,
21+
prototypeDeclaration,
22+
methodShorthand,
23+
propertyLonghand,
24+
typedef,
25+
];
26+
return `(${[regexpParts.join('|')]})`;
27+
}
28+
29+
function addTrailingSpace(keyword) {
30+
return keyword + '\\s';
531
}
632

733
module.exports = getRegexp;

tests/fixtures/js-parent/src/db.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
export function queryDb() {}
2+
export function queryDbFake() {}
23

34
const makeQuery = () => {};
5+
const makeQueryFake = () => {};
46

57
function parseQuery() {
68
const buildParser = () => {};
9+
const buildParserFake = () => {};
10+
}
11+
12+
function parseQueryFake() {
713
}
814

915
const objectWithFunctionShorthand = {
1016
shorthandFunction() {
1117
return 'hi';
18+
},
19+
shorthandFunctionFake() {
20+
return 'hi';
21+
}
22+
};
23+
24+
const objectWithFunctionLonghand = {
25+
longhandFunction: function() {
26+
return 'hi';
27+
},
28+
longhandFunctionFake: function() {
29+
return 'hi';
30+
}
31+
};
32+
33+
const objectWithArrowFunctionLonghand = {
34+
longhandArrowFunction: () => {
35+
return 'hi';
36+
},
37+
longhandArrowFunctionFake: () => {
38+
return 'hi';
1239
}
1340
};
41+
42+
const objectWithPropertyLonghand = {
43+
longhandProperty: 'hello',
44+
longhandPropertyFake: 'hello',
45+
};
46+
47+
const objectWithPropertyShorthand = {
48+
shorthandProperty,
49+
shorthandPropertyFake,
50+
};
51+
52+
const arrayDef = [
53+
longhandFunction,
54+
longhandArrowFunction,
55+
longhandProperty,
56+
shorthandProperty,
57+
];

tests/fixtures/js/db.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
export function queryDb() {}
2+
export function queryDbFake() {}
23

34
const makeQuery = () => {};
5+
const makeQueryFake = () => {};
46

57
function parseQuery() {
68
const buildParser = () => {};
9+
const buildParserFake = () => {};
10+
}
11+
12+
function parseQueryFake() {
713
}
814

915
const objectWithFunctionShorthand = {
1016
shorthandFunction() {
1117
return 'hi';
18+
},
19+
shorthandFunctionFake() {
20+
return 'hi';
21+
}
22+
};
23+
24+
const objectWithFunctionLonghand = {
25+
longhandFunction: function() {
26+
return 'hi';
27+
},
28+
longhandFunctionFake: function() {
29+
return 'hi';
30+
}
31+
};
32+
33+
const objectWithArrowFunctionLonghand = {
34+
longhandArrowFunction: () => {
35+
return 'hi';
36+
},
37+
longhandArrowFunctionFake: () => {
38+
return 'hi';
1239
}
1340
};
41+
42+
const objectWithPropertyLonghand = {
43+
longhandProperty: 'hello',
44+
longhandPropertyFake: 'hello',
45+
};
46+
47+
const objectWithPropertyShorthand = {
48+
shorthandProperty,
49+
shorthandPropertyFake,
50+
};
51+
52+
const arrayDef = [
53+
longhandFunction,
54+
longhandArrowFunction,
55+
longhandProperty,
56+
shorthandProperty,
57+
];

tests/fixtures/js/misc.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export function queryDbTS(): string {}
2+
export function queryDbTSFake(): string {}
3+
4+
const makeQueryTS: MyFunc = (): string => {};
5+
const makeQueryTSFake: MyFunc = (): string => {};
6+
7+
function parseQueryTS(): string {
8+
const buildParserTS: MyFunc = (): string => {};
9+
const buildParserTSFake: MyFunc = (): string => {};
10+
}
11+
12+
function parseQueryTSFake: MyFunc(): string {
13+
}
14+
15+
const objectWithFunctionShorthandTS = {
16+
shorthandFunctionTS(): string {
17+
return 'hi';
18+
},
19+
shorthandFunctionTSFake(): string {
20+
return 'hi';
21+
}
22+
};
23+
24+
const objectWithFunctionLonghandTS = {
25+
longhandFunctionTS: function(): string {
26+
return 'hi';
27+
},
28+
longhandFunctionTSFake: function(): string {
29+
return 'hi';
30+
}
31+
};
32+
33+
const objectWithArrowFunctionLonghandTS = {
34+
longhandArrowFunctionTS: (): string => {
35+
return 'hi';
36+
},
37+
longhandArrowFunctionTSFake: (): string => {
38+
return 'hi';
39+
}
40+
};
41+
42+
const objectWithPropertyLonghandTS = {
43+
longhandPropertyTS: 'hello',
44+
longhandPropertyTSFake: 'hello',
45+
};
46+
47+
const objectWithPropertyShorthandTS = {
48+
shorthandPropertyTS,
49+
shorthandPropertyTSFake,
50+
};
51+
52+
const arrayDef = [
53+
longhandFunctionTS,
54+
longhandArrowFunctionTS,
55+
longhandPropertyTS,
56+
shorthandPropertyTS,
57+
];
58+
59+
interface AnInterface {
60+
foo: string;
61+
}
62+
63+
type AType = 'something' | 'somethingelse';
64+
65+
/**
66+
* @typedef {object} TypeDefObject
67+
* @property {number} loaded Number of bytes already transferred
68+
* @property {number} total Total number of bytes to transfer
69+
*/
70+
71+
/**
72+
* @typedef TypeDefSimple
73+
* @property {number} loaded Number of bytes already transferred
74+
* @property {number} total Total number of bytes to transfer
75+
*/

tests/search.test.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,58 @@ const { search } = require('../src/general');
33

44
describe.each([
55
['queryDb', 'js', 'js', 1],
6-
['makeQuery', 'js', 'js', 3],
7-
['parseQuery', 'js', 'js', 5],
8-
['parseQuery', 'js', 'js_2_files', 5],
9-
['parseQuery', 'js', 'js_directory', 5],
10-
['objectWithFunctionShorthand', 'js', 'js', 9],
11-
['shorthandFunction', 'js', 'js', 10],
12-
['shorthandFunction', undefined, 'js', 10], // auto-detect type
13-
['shorthandFunction', undefined, 'js_2_files', 10], // auto_detect type
14-
['shorthandFunction', undefined, 'js_directory', 10], // auto_detect type
15-
['shorthandFunction', undefined, 'js_parent_directory', 10], // auto_detect type
6+
['queryDbTS', 'js', 'ts', 1],
7+
['makeQuery', 'js', 'js', 4],
8+
['makeQueryTS', 'js', 'ts', 4],
9+
['parseQuery', 'js', 'js', 7],
10+
['parseQuery', 'js', 'js_2_files', 7],
11+
['parseQuery', 'js', 'js_directory', 7],
12+
['parseQueryTS', 'js', 'ts', 7],
13+
['objectWithFunctionShorthand', 'js', 'js', 15],
14+
['objectWithFunctionShorthandTS', 'js', 'ts', 15],
15+
['shorthandFunction', 'js', 'js', 16],
16+
['shorthandFunctionTS', 'js', 'ts', 16],
17+
['shorthandFunction', undefined, 'js', 16], // auto-detect type
18+
['shorthandFunctionTS', undefined, 'ts', 16], // auto-detect type
19+
['shorthandFunction', undefined, 'js_2_files', 16], // auto_detect type
20+
['shorthandFunction', undefined, 'js_directory', 16], // auto_detect type
21+
['shorthandFunction', undefined, 'js_parent_directory', 16], // auto_detect type
22+
['longhandFunction', undefined, 'js_parent_directory', 25], // auto_detect type
23+
['longhandFunctionTS', undefined, 'ts', 25], // auto_detect type
24+
['longhandArrowFunction', undefined, 'js_parent_directory', 34], // auto_detect type
25+
['longhandArrowFunctionTS', undefined, 'ts', 34], // auto_detect type
26+
['longhandProperty', undefined, 'js_parent_directory', 43], // auto_detect type
27+
['longhandPropertyTS', undefined, 'ts', 43], // auto_detect type
28+
['AnInterface', undefined, 'ts', 59], // auto_detect type
29+
['AType', undefined, 'ts', 63], // auto_detect type
30+
['TypeDefObject', undefined, 'ts', 66], // auto_detect type
31+
['TypeDefSimple', undefined, 'ts', 72], // auto_detect type
1632
['queryDb', 'php', 'php', 2],
1733
['$makeQuery', 'php', 'php', 4],
1834
['parseQuery', 'php', 'php', 6],
1935
['Foo', 'php', 'php', 11],
2036
['Bar', 'php', 'php', 14],
2137
['Zoom', 'php', 'php', 17],
2238
['Zoom', undefined, 'php', 17], // auto-detect type
23-
])("search('%s', {type: '%s', path: '%s'})", (symbol, type, fixtureType, expectedLine) => {
39+
])("search('%s', {type: '%s', path: '%s'})",
40+
41+
/**
42+
* @param {string} symbol
43+
* @param {import('../src/general').FileType} type
44+
* @param {string} fixtureType
45+
* @param {number} expectedLine
46+
*/
47+
(symbol, type, fixtureType, expectedLine) => {
2448
test(`finds line '${expectedLine}'`, async () => {
2549
const path = getFixtureForType(fixtureType);
50+
51+
/** @type {import('../src/general').SearchConfig} */
2652
const config = {
2753
type,
2854
searchTool: 'ripgrep',
2955
path,
3056
};
57+
3158
const results = await search(symbol, config);
3259
expect(results.length).toEqual(1);
3360
const result = results[0];
@@ -43,6 +70,8 @@ describe.each([
4370
*/
4471
function getFixtureForType(type) {
4572
switch (type) {
73+
case 'ts':
74+
return './tests/fixtures/js/misc.ts';
4675
case 'js':
4776
return './tests/fixtures/js/db.js';
4877
case 'js_2_files':
@@ -64,6 +93,8 @@ function getFixtureForType(type) {
6493
*/
6594
function getExpectedResultPath(type) {
6695
switch (type) {
96+
case 'ts':
97+
return './tests/fixtures/js/misc.ts';
6798
case 'js':
6899
case 'js_2_files':
69100
case 'js_directory':

0 commit comments

Comments
 (0)