Skip to content

Commit

Permalink
Merge pull request #20 from recifejs/feature/alias-import-no-default
Browse files Browse the repository at this point in the history
Validating alias in the import not default
  • Loading branch information
andrelmlins committed Jul 1, 2020
2 parents a7d0dec + c596705 commit 237cadc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
4 changes: 2 additions & 2 deletions example/src/controllers/admin/TestController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Query, Mutation } from 'recife';

import CommentAFilter, { CommentCreate, CommentDelete } from '../../inputs/CommentInput';
import CommentAFilter, { CommentCreate as CommentCreateA, CommentDelete } from '../../inputs/CommentInput';
import CommentModel from '../../models/CommentModel';

export default class TestController {
Expand All @@ -22,7 +22,7 @@ export default class TestController {
}

@Mutation({ name: 'registerComment' })
createComment(input: CommentCreate): CommentModel {
createComment(input: CommentCreateA): CommentModel {
return this.commentDefault;
}

Expand Down
21 changes: 14 additions & 7 deletions src/compiler/GraphCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Graph from './token/Graph';
import InputCompiler from './InputCompiler';
import ImportDeclaration from './token/ImportDeclaration';
import { isExport, getNameExportDefault } from '../helpers/exportHelper';
import NameImportType from './type/NameImportType';
import Input from './token/Input';

class GraphCompiler {
private static _instance: GraphCompiler;
Expand Down Expand Up @@ -66,27 +68,32 @@ class GraphCompiler {
const graph = new Graph(member, node, this.path, isDefault, this.sourceFile);

if (graph.type) {
this.graphs.push(graph);

if (graph.params) {
this.createInput(graph.params.type);
const input = this.createInput(graph.params.type);
graph.params.type = input!.name;
}

this.graphs.push(graph);
}
}
});
}

private createInput(className: string) {
private createInput(className: string): Input | undefined {
let importDeclaration: ImportDeclaration | undefined = undefined;
let nameImport: NameImportType | undefined = undefined;

this.imports.forEach((importDecl: ImportDeclaration) => {
if (importDecl.names.some(item => item.name === className)) {
const nameImportSearch = importDecl.names.find(item => (item.nameAlias || item.name) === className);
if (nameImportSearch) {
importDeclaration = importDecl;
nameImport = nameImportSearch;
}
});

if (importDeclaration) {
InputCompiler.Instance.compile(importDeclaration, this.program!, className);
if (importDeclaration && nameImport) {
const input = InputCompiler.Instance.compile(importDeclaration, this.program!, nameImport!);
return input;
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/compiler/InputCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ScalarCompiler from './ScalarCompiler';
import TypeCompiler from './TypeCompiler';
import ImportDeclaration from './token/ImportDeclaration';
import Input from './token/Input';
import NameImportType from './type/NameImportType';

class InputCompiler {
private static _instance: InputCompiler;
Expand All @@ -25,16 +26,21 @@ class InputCompiler {
this.inputs = [];
}

compile(importDeclaration: ImportDeclaration, program: ts.Program, className: string) {
if (!this.existInput(importDeclaration, className)) {
compile(importDeclaration: ImportDeclaration, program: ts.Program, nameImport: NameImportType): Input {
const inputSearch = this.findInput(importDeclaration, nameImport.name);

if (!inputSearch) {
this.sourceFile = program.getSourceFile(importDeclaration.getPath());
const input = new Input(importDeclaration, className, this.sourceFile);
const input = new Input(importDeclaration, nameImport, this.sourceFile);
this.inputs.push(input);
return input;
}

return inputSearch;
}

private existInput(importDeclaration: ImportDeclaration, className: String): boolean {
return this.inputs.some(item => item.name === className && item.path === importDeclaration.getPath());
private findInput(importDeclaration: ImportDeclaration, className: String): Input | undefined {
return this.inputs.find(item => item.name === className && item.path === importDeclaration.getPath());
}

expand() {
Expand Down
18 changes: 8 additions & 10 deletions src/compiler/token/ImportDeclaration.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as ts from 'typescript';
import path from 'path';

type NameImportType = {
name: string;
exportDefault: boolean;
};
import NameImportType from '../type/NameImportType';

class ImportDeclaration {
public names: NameImportType[] = [];
Expand All @@ -24,11 +20,13 @@ class ImportDeclaration {
}

if (node.importClause.namedBindings) {
node.importClause.namedBindings.forEachChild((node: ts.Node) => {
this.names.push({
name: node.getText(sourceFile),
exportDefault: false
});
node.importClause.namedBindings.forEachChild(node => {
if (ts.isImportSpecifier(node))
this.names.push({
name: node.propertyName ? node.propertyName.getText(sourceFile) : node.name.getText(sourceFile),
nameAlias: node.propertyName ? node.name.getText(sourceFile) : undefined,
exportDefault: false
});
});
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/compiler/token/Input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ts from 'typescript';
import Field from './Field';
import ImportDeclaration from './ImportDeclaration';
import { findNodeExportDefault } from '../../helpers/exportHelper';
import NameImportType from '../type/NameImportType';

class Input {
public name: string;
Expand All @@ -10,12 +11,10 @@ class Input {
private sourceFile?: ts.SourceFile;
private exportDefault: boolean;

constructor(importDeclaration: ImportDeclaration, className: string, sourceFile?: ts.SourceFile) {
const nameImport = importDeclaration.names.find(item => item.name === className);

this.exportDefault = nameImport!.exportDefault;
constructor(importDeclaration: ImportDeclaration, nameImport: NameImportType, sourceFile?: ts.SourceFile) {
this.exportDefault = nameImport.exportDefault;
this.path = importDeclaration.getPath();
this.name = className;
this.name = nameImport.name;
this.sourceFile = sourceFile;
this.fields = [];

Expand Down Expand Up @@ -44,14 +43,23 @@ class Input {
private compileType(node: ts.Node) {
if (ts.isTypeAliasDeclaration(node)) {
if (this.exportDefault || node.name.getText(this.sourceFile) === this.name) {
if (this.exportDefault) {
this.name = node.name.getText(this.sourceFile);
}
this.compileTypeLiteral(node);
}
} else if (ts.isClassDeclaration(node)) {
if (this.exportDefault || node.name!.getText(this.sourceFile) === this.name) {
if (this.exportDefault) {
this.name = node.name!.getText(this.sourceFile);
}
this.compileClass(node);
}
} else if (ts.isInterfaceDeclaration(node)) {
if (this.exportDefault || node.name.getText(this.sourceFile) === this.name) {
if (this.exportDefault) {
this.name = node.name.getText(this.sourceFile);
}
this.compileInterface(node);
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/type/NameImportType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type NameImportType = {
name: string;
nameAlias?: string;
exportDefault: boolean;
};

export default NameImportType;

0 comments on commit 237cadc

Please sign in to comment.