Skip to content

Commit

Permalink
fix: resolve linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoomen committed May 1, 2022
1 parent d2c203a commit 7fee3af
Show file tree
Hide file tree
Showing 17 changed files with 45 additions and 863 deletions.
754 changes: 3 additions & 751 deletions .eslintrc.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 80,
"printWidth": 100,
"singleQuote": true,
"trailingComma": "all"
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"conventional-changelog-cli": "^2.2.2",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.14.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ if (languageParser) {
const sourceCode = fs.readFileSync(filepath, { encoding: 'utf8', flag: 'r' });

const tree = parser.parse(sourceCode);
const parserService = getParserService(language, [
tree.rootNode,
lineNumber,
nodeTypes,
]);
const parserService = getParserService(language, [tree.rootNode, lineNumber, nodeTypes]);
if (parserService) {
parserService.traverse(tree.rootNode);
parserService.output();
Expand Down
5 changes: 1 addition & 4 deletions src/parsers/base-parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ export class BaseParserService {
}
}

protected runNodeParser(
parser: (node: SyntaxNode) => void,
node: SyntaxNode,
): void {
protected runNodeParser(parser: (node: SyntaxNode) => void, node: SyntaxNode): void {
this.done = true;
parser.bind(this)(node);
}
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/bash.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ enum NodeType {
FUNCTION_DEFINITION = 'function_definition',
}

export class BashParserService
extends BaseParserService
implements CustomParserService {
export class BashParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down
17 changes: 4 additions & 13 deletions src/parsers/c.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ enum NodeType {
FIELD_DECLARATION = 'field_declaration',
}

export class CParserService
extends BaseParserService
implements CustomParserService {
export class CParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -82,18 +80,11 @@ export class CParserService
.shift()?.text;

childNode.children
.filter((n: SyntaxNode) =>
['parameter_list', 'parameter_declaration'].includes(n.type),
)
.filter((n: SyntaxNode) => ['parameter_list', 'parameter_declaration'].includes(n.type))
.map((n: SyntaxNode) =>
n.children.filter(
(cn: SyntaxNode) => cn.type === 'parameter_declaration',
),
)
.reduce(
(items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr],
[],
n.children.filter((cn: SyntaxNode) => cn.type === 'parameter_declaration'),
)
.reduce((items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr], [])
.forEach((n: SyntaxNode) => {
const param: Record<string, any> = { name: null, type: null };

Expand Down
31 changes: 7 additions & 24 deletions src/parsers/cpp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ enum NodeType {
FUNCTION_DECLARATOR = 'function_declarator',
}

export class CppParserService
extends BaseParserService
implements CustomParserService {
export class CppParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private lineNumber: number,
Expand All @@ -32,10 +30,7 @@ export class CppParserService
switch (node.type) {
case NodeType.TEMPLATE_DECLARATION: {
const childNode: SyntaxNode = node.children
.filter(
(n: SyntaxNode) =>
n.previousSibling?.type === 'template_parameter_list',
)
.filter((n: SyntaxNode) => n.previousSibling?.type === 'template_parameter_list')
.shift() as SyntaxNode;
this.lineNumber = childNode.startPosition.row;
this.traverse(childNode);
Expand Down Expand Up @@ -133,9 +128,7 @@ export class CppParserService
// Method name.
childNode.children
.filter((n: SyntaxNode) =>
['identifier', 'scoped_identifier', 'field_identifier'].includes(
n.type,
),
['identifier', 'scoped_identifier', 'field_identifier'].includes(n.type),
)
.forEach((n: SyntaxNode) => {
if (['identifier', 'field_identifier'].includes(n.type)) {
Expand All @@ -149,9 +142,7 @@ export class CppParserService

// Parameters.
childNode.children
.filter((n: SyntaxNode) =>
['parameter_list', 'parameter_declaration'].includes(n.type),
)
.filter((n: SyntaxNode) => ['parameter_list', 'parameter_declaration'].includes(n.type))
.map((n: SyntaxNode) =>
n.children.filter((cn: SyntaxNode) =>
[
Expand All @@ -162,10 +153,7 @@ export class CppParserService
].includes(cn.type),
),
)
.reduce(
(items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr],
[],
)
.reduce((items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr], [])
.forEach((n: SyntaxNode) => {
const param: Record<string, any> = { name: null, type: null };

Expand Down Expand Up @@ -239,9 +227,7 @@ export class CppParserService
});
}

private getTypeParameters(
node: SyntaxNode,
): Array<Record<'name', null | string>> {
private getTypeParameters(node: SyntaxNode): Array<Record<'name', null | string>> {
const typeparams: Array<Record<'name', null | string>> = [];

node.children
Expand All @@ -255,10 +241,7 @@ export class CppParserService
].includes(cn.type),
),
)
.reduce(
(items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr],
[],
)
.reduce((items: SyntaxNode[], curr: SyntaxNode[]) => [...items, ...curr], [])
.forEach((n: SyntaxNode) => {
const param: Record<string, any> = { name: null };

Expand Down
4 changes: 3 additions & 1 deletion src/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type ParserService =
export function getParserService(
language: ValueOf<Language>,
args: [SyntaxNode, number, string[]],
): ParserService | undefined {
): ParserService | null {
switch (language) {
case Language.PHP:
return new PhpParserService(...args);
Expand Down Expand Up @@ -64,4 +64,6 @@ export function getParserService(
console.error(`Could not get parser service for unknown language: ${language}`);
break;
}

return null;
}
8 changes: 2 additions & 6 deletions src/parsers/java.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ enum NodeType {
METHOD_DECLARATION = 'method_declaration',
}

export class JavaParserService
extends BaseParserService
implements CustomParserService {
export class JavaParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -77,9 +75,7 @@ export class JavaParserService

case 'formal_parameters': {
childNode.children
.filter((n: SyntaxNode) =>
['formal_parameter', 'spread_parameter'].includes(n.type),
)
.filter((n: SyntaxNode) => ['formal_parameter', 'spread_parameter'].includes(n.type))
.forEach((cn: SyntaxNode) => {
const param: Record<string, any> = { name: null, type: null };

Expand Down
8 changes: 2 additions & 6 deletions src/parsers/lua.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ enum NodeType {
FUNCTION_DEFINITION = 'function_definition',
}

export class LuaParserService
extends BaseParserService
implements CustomParserService {
export class LuaParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -63,9 +61,7 @@ export class LuaParserService
switch (childNode.type) {
case 'function_name': {
this.result.name =
childNode.childCount > 0
? childNode.children.pop()?.text
: childNode.text;
childNode.childCount > 0 ? childNode.children.pop()?.text : childNode.text;
break;
}

Expand Down
16 changes: 6 additions & 10 deletions src/parsers/php.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ enum NodeType {
PROPERTY_DECLARATION = 'property_declaration',
}

export class PhpParserService
extends BaseParserService
implements CustomParserService {
export class PhpParserService extends BaseParserService implements CustomParserService {
constructor(
private readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -93,17 +91,15 @@ export class PhpParserService
return fqn;
}

private getClassPropertyTypeViaConstructor(
node: SyntaxNode,
): string | undefined {
private getClassPropertyTypeViaConstructor(node: SyntaxNode): string | null {
const propertyName: string | undefined = node?.children
.filter((n: SyntaxNode) => n.type === 'property_element')
.shift()
?.children.shift()
?.children.pop()?.text;

if (!propertyName) {
return;
return null;
}

const constructorNode: SyntaxNode | undefined = node?.parent?.children
Expand All @@ -117,7 +113,7 @@ export class PhpParserService
.shift();

if (!constructorNode) {
return;
return null;
}

let paramName: string | undefined;
Expand All @@ -143,7 +139,7 @@ export class PhpParserService
});

if (!paramName) {
return;
return null;
}

const paramType: string | undefined = constructorNode?.children
Expand All @@ -162,7 +158,7 @@ export class PhpParserService
)
.shift()?.text;

return paramType;
return paramType as string;
}

private parseClassProperty(node: SyntaxNode): void {
Expand Down
15 changes: 3 additions & 12 deletions src/parsers/python.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ enum NodeType {
MODULE = 'module',
}

export class PythonParserService
extends BaseParserService
implements CustomParserService {
export class PythonParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -74,10 +72,7 @@ export class PythonParserService
.forEach((cn: SyntaxNode, index: number) => {
// If this is a class method then we don't want to add the first
// parameter, because that will be the 'self' keyword.
if (
node.parent?.parent?.type === 'class_definition' &&
index === 0
) {
if (node.parent?.parent?.type === 'class_definition' && index === 0) {
return;
}

Expand All @@ -98,11 +93,7 @@ export class PythonParserService
param.name = pn.text;
}

if (
['list_splat_pattern', 'dictionary_splat_pattern'].includes(
pn.type,
)
) {
if (['list_splat_pattern', 'dictionary_splat_pattern'].includes(pn.type)) {
param.name = pn.children
.filter((cpn: SyntaxNode) => cpn.type === 'identifier')
.shift()?.text;
Expand Down
4 changes: 1 addition & 3 deletions src/parsers/ruby.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ enum NodeType {
SINGLETON_METHOD = 'singleton_method',
}

export class RubyParserService
extends BaseParserService
implements CustomParserService {
export class RubyParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down
14 changes: 4 additions & 10 deletions src/parsers/rust.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ enum NodeType {
FUNCTION_ITEM = 'function_item',
}

export class RustParserService
extends BaseParserService
implements CustomParserService {
export class RustParserService extends BaseParserService implements CustomParserService {
constructor(
readonly rootNode: SyntaxNode,
private readonly lineNumber: number,
Expand Down Expand Up @@ -66,10 +64,7 @@ export class RustParserService

case 'generic_type': {
const hasResultReturnType = childNode.children
.filter(
(n: SyntaxNode) =>
n.type === 'type_identifier' && n.text === 'Result',
)
.filter((n: SyntaxNode) => n.type === 'type_identifier' && n.text === 'Result')
.shift();
if (hasResultReturnType) {
this.result.errors = true;
Expand All @@ -82,9 +77,8 @@ export class RustParserService
.filter((n: SyntaxNode) => n.type === 'parameter')
.forEach((child: SyntaxNode) => {
this.result.parameters.push({
name: child.children
.filter((n: SyntaxNode) => n.type === 'identifier')
.shift()?.text,
name: child.children.filter((n: SyntaxNode) => n.type === 'identifier').shift()
?.text,
});
});
break;
Expand Down
Loading

0 comments on commit 7fee3af

Please sign in to comment.