Skip to content

Commit

Permalink
A bunch of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emmatown committed Dec 30, 2019
1 parent 7f01a38 commit 60bbc51
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 79 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-emus-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@magical-types/convert-type": patch
---

Add `getPropTypesType` to get the TypeScript Type of a component type
5 changes: 5 additions & 0 deletions .changeset/ten-frogs-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@magical-types/loader": patch
---

Bump to test - this version does not work
5 changes: 5 additions & 0 deletions .changeset/twenty-scissors-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@magical-types/macro": patch
---

Extract some internals into @magical-types/convert-type
74 changes: 64 additions & 10 deletions packages/convert-type/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
import * as typescript from "typescript";
import { MagicalNode, Property, TypeParameterNode } from "@magical-types/types";
import { InternalError } from "@magical-types/errors";
import { TypeChecker } from "ts-morph";

function getFunctionComponentProps(type: typescript.Type) {
const callSignatures = type.getCallSignatures();

if (callSignatures.length) {
for (const sig of callSignatures) {
const params = sig.getParameters();
if (params.length !== 0) {
return params[0];
}
}
}
}

function getClassComponentProps(type: typescript.Type) {
const constructSignatures = type.getConstructSignatures();

if (constructSignatures.length) {
for (const sig of constructSignatures) {
const instanceType = sig.getReturnType();
const props = instanceType.getProperty("props");
if (props) {
return props;
}
}
}
}

export function getPropTypesType(type: typescript.Type) {
let propsSymbol;
if (type.isUnion()) {
for (let typeInUnion of type.types) {
propsSymbol =
getFunctionComponentProps(typeInUnion) ||
getClassComponentProps(typeInUnion);
if (propsSymbol) {
break;
}
}
} else {
propsSymbol =
getFunctionComponentProps(type) || getClassComponentProps(type);
}

if (!propsSymbol) {
throw new InternalError("could not find props symbol");
}

return getTypeChecker(type).getTypeOfSymbolAtLocation(
propsSymbol,
propsSymbol.valueDeclaration || propsSymbol.declarations![0]
);
}

function getObjectFlags(type: typescript.Type) {
return type.flags & typescript.TypeFlags.Object
Expand Down Expand Up @@ -283,15 +335,6 @@ export let convertType = wrapInCache(
types
};
}
if (type.isIntersection()) {
return {
type: "Intersection",
types: type.types.map((type, index) =>
convertType(type, path.concat("types", index))
)
};
}

if (
!!(getObjectFlags(type) & typescript.ObjectFlags.Reference) &&
(type as typescript.TypeReference).target ===
Expand Down Expand Up @@ -458,6 +501,17 @@ export let convertType = wrapInCache(
name: (type as typescript.UniqueESSymbolType).escapedName.toString()
};
}
// @ts-ignore
if (type.isIntersection()) {
return {
type: "Intersection",
// @ts-ignore
types: type.types.map((type, index) =>
convertType(type, path.concat("types", index))
)
};
}

debugger;

throw new InternalError(
Expand Down
26 changes: 9 additions & 17 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { loader as LoaderType } from "webpack";
import * as typescript from "typescript";
import flatted from "flatted";
import { Project } from "ts-morph";
import { convertType } from "@magical-types/convert-type";
import { Project, FileSystemRefreshResult } from "ts-morph";
import { convertType, getPropTypesType } from "@magical-types/convert-type";

let projectCache = new Map<string, Project>();

Expand All @@ -15,38 +15,30 @@ let loader: LoaderType.Loader = function() {
if (!configFileName) {
throw new Error("No tsconfig.json file could be found");
}
let isFreshProject = false;
if (!projectCache.has(configFileName)) {
isFreshProject = true;
const cachedProject = new Project({
tsConfigFilePath: configFileName,
addFilesFromTsConfig: false,
compilerOptions: {
noEmit: false
}
});
projectCache.set(configFileName, cachedProject);
}
let project = projectCache.get(configFileName)!;
let sourceFile = project.addExistingSourceFile(filename);
project.resolveSourceFileDependencies();
let sourceFiles = project.getSourceFiles();
for (let sourceFile of sourceFiles) {
if (!isFreshProject) {
sourceFile.refreshFromFileSystemSync();
}
this.addDependency(sourceFile.getFilePath());
}
let sourceFile = project.getSourceFileOrThrow(filename);
let exportDeclarations = sourceFile.getExportedDeclarations();
let code = `import * as __flatted from '@magical-types/loader/flatted'\n`;
for (let [exportName, declaration] of exportDeclarations) {
let type = declaration[0].getType();
let type = declaration[0].getType().compilerType;
if (exportName[0].toUpperCase() === exportName[0]) {
type = getPropTypesType(type);
}
code += `export var ${exportName} = __flatted.parse(${JSON.stringify(
flatted.stringify(convertType(type.compilerType, []))
flatted.stringify(convertType(type, []))
)})\n`;
}

return code;
return `export var thing = true`;
};

export default loader;
54 changes: 2 additions & 52 deletions packages/macro/src/get-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,7 @@ import * as BabelTypes from "@babel/types";
import { InternalError } from "@magical-types/errors";
import { Project } from "ts-morph";
import * as flatted from "flatted";
import { convertType } from "@magical-types/convert-type";

function getFunctionComponentProps(type: typescript.Type) {
const callSignatures = type.getCallSignatures();

if (callSignatures.length) {
for (const sig of callSignatures) {
const params = sig.getParameters();
if (params.length !== 0) {
return params[0];
}
}
}
}

function getClassComponentProps(type: typescript.Type) {
const constructSignatures = type.getConstructSignatures();

if (constructSignatures.length) {
for (const sig of constructSignatures) {
const instanceType = sig.getReturnType();
const props = instanceType.getProperty("props");
if (props) {
return props;
}
}
}
}
import { convertType, getPropTypesType } from "@magical-types/convert-type";

export function getTypes(
filename: string,
Expand Down Expand Up @@ -114,30 +87,7 @@ export function getTypes(
);

if (val.exportName === "PropTypes") {
let propsSymbol;
if (type.isUnion()) {
for (let typeInUnion of type.types) {
propsSymbol =
getFunctionComponentProps(typeInUnion) ||
getClassComponentProps(typeInUnion);
if (propsSymbol) {
break;
}
}
} else {
propsSymbol =
getFunctionComponentProps(type) ||
getClassComponentProps(type);
}

if (!propsSymbol) {
throw new InternalError("could not find props symbol");
}

type = typeChecker.getTypeOfSymbolAtLocation(
propsSymbol,
propsSymbol.valueDeclaration || propsSymbol.declarations![0]
);
type = getPropTypesType(type);
}
} else {
if (!jsxOpening.typeArguments) {
Expand Down

0 comments on commit 60bbc51

Please sign in to comment.