Skip to content

Commit 131eaa2

Browse files
committed
small fix to file loading
1 parent 4aa6638 commit 131eaa2

File tree

3 files changed

+76
-65
lines changed

3 files changed

+76
-65
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# extract-react-types
22

3+
## 0.14.3
4+
5+
- call to `loadFileSync` in `ExportNamedDeclaration` was not being passed in the loaderOpts, causing an error in the parsing. Options are now passed through correctly.
6+
- Fix Id to have additional optional property.
7+
38
## 0.14.2
49

510
- fix decorator plugin implementation

index.js

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
resolveImportFilePathSync
1212
} = require('babel-file-loader');
1313
const { isFlowIdentifier } = require('babel-flow-identifiers');
14-
const { getTypeBinding } = require("babel-type-scopes");
14+
const { getTypeBinding } = require('babel-type-scopes');
1515
const { getIdentifierKind } = require('babel-identifiers');
1616
const { isReactComponentClass } = require('babel-react-components');
1717
const createBabylonOptions = require('babylon-options');
@@ -600,7 +600,7 @@ converters.Identifier = (path, context) /*: K.Id*/ => {
600600
let kind = getIdentifierKind(path);
601601
let name = path.node.name;
602602

603-
if (context.mode === "value") {
603+
if (context.mode === 'value') {
604604
let res = {};
605605
if (kind === 'reference') {
606606
let binding = path.scope.getBinding(name);
@@ -680,21 +680,21 @@ converters.Identifier = (path, context) /*: K.Id*/ => {
680680
} else if (isTsIdentifier(path)) {
681681
let foundPath = path.scope.getBinding(name);
682682
if (
683-
foundPath && (
684-
foundPath.path.isImportDefaultSpecifier() ||
685-
foundPath.path.isImportNamespaceSpecifier() ||
686-
foundPath.path.isImportSpecifier())
683+
foundPath &&
684+
(foundPath.path.isImportDefaultSpecifier() ||
685+
foundPath.path.isImportNamespaceSpecifier() ||
686+
foundPath.path.isImportSpecifier())
687687
) {
688688
return convert(foundPath.path, context);
689689
}
690690

691691
let tsBinding = getTypeBinding(path, name);
692692
if (!tsBinding) {
693693
return {
694-
kind: "id",
695-
name,
694+
kind: 'id',
695+
name
696696
};
697-
};
697+
}
698698
bindingPath = tsBinding.path.parentPath;
699699
} else {
700700
bindingPath = path.scope.getBinding(name);
@@ -706,7 +706,10 @@ converters.Identifier = (path, context) /*: K.Id*/ => {
706706
}
707707

708708
// If path is a descendant of bindingPath and share the same name, this is a recursive type.
709-
if (path.isDescendant(bindingPath) && bindingPath.get('id').node.name === name) {
709+
if (
710+
path.isDescendant(bindingPath) &&
711+
bindingPath.get('id').node.name === name
712+
) {
710713
return { kind: 'id', name };
711714
}
712715

@@ -824,8 +827,8 @@ converters.TSUndefinedKeyword = (path, context) /*: K.Void */ => {
824827

825828
converters.TSTypeLiteral = (path, context) /*: K.Obj*/ => {
826829
return {
827-
kind: 'object',
828-
members: path.get('members').map(memberPath => convert(memberPath, context))
830+
kind: 'object',
831+
members: path.get('members').map(memberPath => convert(memberPath, context))
829832
};
830833
};
831834

@@ -834,13 +837,13 @@ converters.TSPropertySignature = (path, context) /*: K.Property */ => {
834837
kind: 'property',
835838
optional: !!path.node.optional,
836839
key: convert(path.get('key'), context),
837-
value: convert(path.get('typeAnnotation'), context),
838-
}
840+
value: convert(path.get('typeAnnotation'), context)
841+
};
839842
};
840843

841-
converters.TSTypeAliasDeclaration = (path, context) /*: K.Obj */ => {
844+
converters.TSTypeAliasDeclaration = (path, context) /*: K.Obj */ => {
842845
return convert(path.get('typeAnnotation'), context);
843-
}
846+
};
844847

845848
converters.TSLiteralType = (path) /*: K.String */ => {
846849
return {
@@ -856,14 +859,14 @@ converters.TSTypeReference = (path, context) /*: K.Generic */ => {
856859
return {
857860
kind: 'generic',
858861
typeParams: convert(typeParameters, context),
859-
value: convert(path.get('typeName'), context),
860-
}
862+
value: convert(path.get('typeName'), context)
863+
};
861864
}
862865

863866
return {
864-
kind: "generic",
865-
value: convert(path.get("typeName"), context)
866-
}
867+
kind: 'generic',
868+
value: convert(path.get('typeName'), context)
869+
};
867870
};
868871

869872
converters.TSUnionType = (path, context) /*: K.Union*/ => {
@@ -881,16 +884,15 @@ converters.TSTupleType = (path, context) /*: K.Tuple*/ => {
881884
};
882885

883886
converters.TSFunctionType = (path, context) /*: K.Generic */ => {
884-
const parameters = path.get("parameters").map(p => convertParameter(p, context));
885-
const returnType = convert(
886-
path.get("typeAnnotation"),
887-
context
888-
);
887+
const parameters = path
888+
.get('parameters')
889+
.map(p => convertParameter(p, context));
890+
const returnType = convert(path.get('typeAnnotation'), context);
889891

890892
return {
891-
kind: "generic",
893+
kind: 'generic',
892894
value: {
893-
kind: "function",
895+
kind: 'function',
894896
returnType,
895897
parameters
896898
}
@@ -902,20 +904,20 @@ converters.TSMethodSignature = (path, context) /*: K.Property */ => {
902904
kind: 'property',
903905
optional: !!path.node.optional,
904906
key: convert(path.get('key'), context),
905-
value: convertMethodCall(path, context),
906-
}
907-
}
907+
value: convertMethodCall(path, context)
908+
};
909+
};
908910

909911
converters.TSCallSignatureDeclaration = (path, context) /*: K.Property */ => {
910912
return {
911913
kind: 'property',
912914
key: {
913-
kind: 'string',
915+
kind: 'string'
914916
},
915917
optional: false,
916918
value: convertMethodCall(path, context)
917919
};
918-
}
920+
};
919921

920922
converters.TSInterfaceDeclaration = (path, context) /*: K.Obj */ => {
921923
const extendedTypes = extendedTypesMembers(path, context);
@@ -924,12 +926,12 @@ converters.TSInterfaceDeclaration = (path, context) /*: K.Obj */ => {
924926
kind: 'object',
925927
// Merge the current interface members with any extended members
926928
members: interfaceType.members.concat(extendedTypes)
927-
}
929+
};
928930
};
929931

930932
converters.TSExpressionWithTypeArguments = (path, context) /*: K.Id */ => {
931-
return convert(path.get('expression'), context)
932-
}
933+
return convert(path.get('expression'), context);
934+
};
933935

934936
converters.TSInterfaceBody = (path, context) /*: K.Obj */ => {
935937
return {
@@ -939,33 +941,33 @@ converters.TSInterfaceBody = (path, context) /*: K.Obj */ => {
939941
};
940942

941943
converters.TSTypeAnnotation = (path, context) => {
942-
return convert(path.get("typeAnnotation"), context);
944+
return convert(path.get('typeAnnotation'), context);
943945
};
944946

945947
converters.TSQualifiedName = (path, context) /*: K.Id */ => {
946-
const left = convert(path.get("left"), context);
947-
const right = convert(path.get("right"), context);
948+
const left = convert(path.get('left'), context);
949+
const right = convert(path.get('right'), context);
948950

949951
return {
950952
kind: 'id',
951-
name: `${left.name}.${right.name}`,
952-
}
953+
name: `${left.name}.${right.name}`
954+
};
953955
};
954956

955957
converters.TSEnumDeclaration = (path, context) /*: K.Union */ => {
956-
const { name } = path.get("id").node;
957-
const types = path.get("members").map(p => {
958+
const { name } = path.get('id').node;
959+
const types = path.get('members').map(p => {
958960
const member = convert(p, context);
959961
return {
960962
kind: member.kind,
961-
name: `${name}.${member.name}`,
962-
}
963+
name: `${name}.${member.name}`
964+
};
963965
});
964-
return { kind: "union", types };
966+
return { kind: 'union', types };
965967
};
966968

967969
converters.TSEnumMember = (path, context) => {
968-
return convert(path.get("id"), context);
970+
return convert(path.get('id'), context);
969971
};
970972

971973
converters.TSArray = (path, context) /*: K.Any */ => {
@@ -979,7 +981,10 @@ converters.TSArrayType = (path, context) /*: K.ArrayType */ => {
979981
};
980982
};
981983

982-
converters.TSTypeParameterInstantiation = (path, context) /*: K.TypeParams */ => {
984+
converters.TSTypeParameterInstantiation = (
985+
path,
986+
context
987+
) /*: K.TypeParams */ => {
983988
return {
984989
kind: 'typeParams',
985990
params: path.get('params').map(param => convert(param, context))
@@ -1010,18 +1015,20 @@ converters.ArrayTypeAnnotation = (path, context) /*: K.ArrayType*/ => {
10101015

10111016
converters.TSIntersectionType = (path, context) /*: K.Intersection*/ => {
10121017
const types = path.get('types').map(type => convert(type, context));
1013-
return { kind: 'intersection', types }
1014-
}
1018+
return { kind: 'intersection', types };
1019+
};
10151020

1016-
converters.TSIndexSignature = (path, context) /*: K.Property */ =>{
1021+
converters.TSIndexSignature = (path, context) /*: K.Property */ => {
10171022
const id = path.get('parameters')[0];
10181023
return {
10191024
kind: 'property',
10201025
key: {
10211026
kind: 'id',
1022-
name: `[${convert(id, context).name}: ${convert(id.get('typeAnnotation'), context).kind}]`
1027+
name: `[${convert(id, context).name}: ${
1028+
convert(id.get('typeAnnotation'), context).kind
1029+
}]`
10231030
},
1024-
value: convert(path.get('typeAnnotation'), context),
1031+
value: convert(path.get('typeAnnotation'), context)
10251032
};
10261033
};
10271034

@@ -1038,8 +1045,8 @@ converters.TSNullKeyword = (path, context) /*: K.Null */ => {
10381045
};
10391046

10401047
converters.TSThisType = (path, context) /*:K.This */ => {
1041-
return { kind: 'custom', value: 'this' }
1042-
}
1048+
return { kind: 'custom', value: 'this' };
1049+
};
10431050

10441051
function extendedTypesMembers(path, context) {
10451052
const members = path.get('extends');
@@ -1123,7 +1130,7 @@ function importConverterGeneral(path, context) /*: K.Import */ {
11231130
}
11241131

11251132
let exported = matchExported(file, name);
1126-
1133+
11271134
if (!exported) {
11281135
exported = recursivelyResolveExportAll(file.path, context, name);
11291136

@@ -1258,7 +1265,7 @@ converters.ExportNamedDeclaration = (path, context) /*: K.Export */ => {
12581265
context.resolveOptions
12591266
);
12601267

1261-
file = loadFileSync(actualPath);
1268+
file = loadFileSync(actualPath, context.parserOpts);
12621269
// We need to calculate name from the specifiers, I think knowing that there
12631270
// will always be one specifier
12641271
let resolvedValue = matchExported(file, name);
@@ -1291,11 +1298,10 @@ converters.ImportSpecifier = (path, context) /*: K.Import */ => {
12911298
};
12921299

12931300
function convertMethodCall(path, context) /*: K.Func */ {
1294-
const parameters = path.get('parameters').map(p => convertParameter(p, context));
1295-
const returnType = convert(
1296-
path.get('typeAnnotation'),
1297-
context
1298-
);
1301+
const parameters = path
1302+
.get('parameters')
1303+
.map(p => convertParameter(p, context));
1304+
const returnType = convert(path.get('typeAnnotation'), context);
12991305

13001306
return {
13011307
kind: 'function',
@@ -1306,10 +1312,10 @@ function convertMethodCall(path, context) /*: K.Func */ {
13061312

13071313
function mapComment(comment) {
13081314
return {
1309-
type: comment.type === "CommentLine" ? "commentLine" : "commentBlock",
1315+
type: comment.type === 'CommentLine' ? 'commentLine' : 'commentBlock',
13101316
value: normalizeComment(comment),
13111317
raw: comment.value
1312-
}
1318+
};
13131319
}
13141320

13151321
function attachCommentProperty(source, dest, name) {

kinds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type TypeParamsDeclaration = {
1212
kind: "typeParamsDeclaration",
1313
params: Array<AnyTypeKind>
1414
};
15-
export type Id = { kind: "id", name: string, type?: ?$Diff<AnyKind, Id> };
15+
export type Id = { kind: "id", name: string, type?: ?$Diff<AnyKind, Id>, referenceIdName?: string };
1616
export type TemplateLiteral = {
1717
kind: "templateLiteral",
1818
expressions: Array<AnyValueKind>,

0 commit comments

Comments
 (0)