Skip to content

Commit 1315106

Browse files
BlaszNoviny
authored andcommitted
Handle importing JSON files (#22)
* Handle importing JSON files and fix context not being passed through for default props converter JSON files are now detected and intentionally not parsed when imported as parsing them throws errors.
1 parent 0fa5494 commit 1315106

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

__fixtures__/test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "some cool package",
3+
"version": "0.0.1"
4+
}

__snapshots__/test.js.snap

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ Object {
171171
}
172172
`;
173173

174+
exports[`Should handle importing JSON files 1`] = `
175+
Object {
176+
"classes": Array [
177+
Object {
178+
"kind": "object",
179+
"members": Array [
180+
Object {
181+
"default": Object {
182+
"importKind": "value",
183+
"kind": "import",
184+
"moduleSpecifier": "./__fixtures__/test",
185+
"name": "name",
186+
"referenceIdName": "name",
187+
},
188+
"key": Object {
189+
"kind": "id",
190+
"name": "a",
191+
},
192+
"kind": "property",
193+
"optional": false,
194+
"value": Object {
195+
"kind": "string",
196+
},
197+
},
198+
],
199+
"name": Object {
200+
"kind": "id",
201+
"name": "Component",
202+
"type": null,
203+
},
204+
},
205+
],
206+
"kind": "program",
207+
}
208+
`;
209+
174210
exports[`Should handle rest element 1`] = `
175211
Object {
176212
"classes": Array [
@@ -1084,6 +1120,7 @@ Object {
10841120
"value": Object {
10851121
"kind": "generic",
10861122
"value": Object {
1123+
"external": true,
10871124
"importKind": "type",
10881125
"kind": "import",
10891126
"moduleSpecifier": "react",
@@ -2046,6 +2083,7 @@ Object {
20462083
Object {
20472084
"kind": "spread",
20482085
"value": Object {
2086+
"external": true,
20492087
"importKind": "value",
20502088
"kind": "import",
20512089
"moduleSpecifier": "somewhere",
@@ -2304,6 +2342,7 @@ Object {
23042342
"value": Object {
23052343
"kind": "generic",
23062344
"value": Object {
2345+
"external": true,
23072346
"importKind": "type",
23082347
"kind": "import",
23092348
"moduleSpecifier": "react",
@@ -2365,6 +2404,7 @@ Object {
23652404
"value": Object {
23662405
"kind": "generic",
23672406
"value": Object {
2407+
"external": true,
23682408
"importKind": "type",
23692409
"kind": "import",
23702410
"moduleSpecifier": "react",
@@ -2433,6 +2473,7 @@ Object {
24332473
"value": Object {
24342474
"kind": "generic",
24352475
"value": Object {
2476+
"external": true,
24362477
"importKind": "type",
24372478
"kind": "import",
24382479
"moduleSpecifier": "react",
@@ -2502,6 +2543,7 @@ Object {
25022543
"value": Object {
25032544
"kind": "generic",
25042545
"value": Object {
2546+
"external": true,
25052547
"importKind": "type",
25062548
"kind": "import",
25072549
"moduleSpecifier": "react",

index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ export type * from './kinds'
44
import * as K from './kinds'
55
*/
66

7+
const nodePath = require('path');
78
const createBabelFile = require("babel-file");
8-
const { loadImportSync } = require("babel-file-loader");
9+
const { loadFileSync, resolveImportFilePathSync } = require("babel-file-loader");
910
const { isFlowIdentifier } = require("babel-flow-identifiers");
1011
const { findFlowBinding } = require("babel-flow-scope");
1112
const { getIdentifierKind } = require("babel-identifiers");
@@ -138,7 +139,7 @@ converters.Program = (path, context) /*: K.Program*/ => {
138139
function convertReactComponentClass(path, context) {
139140
let params = path.get("superTypeParameters").get("params");
140141
let props = params[0];
141-
let defaultProps = getDefaultProps(path);
142+
let defaultProps = getDefaultProps(path, context);
142143

143144
let classProperties = convert(props, { ...context, mode: "type" });
144145
classProperties.name = convert(path.get("id"), {
@@ -859,7 +860,19 @@ function importConverterGeneral(path, context) /*: K.Import */ {
859860
};
860861
}
861862

862-
let file = loadImportSync(path.parentPath, context.resolveOptions);
863+
const filePath = resolveImportFilePathSync(path.parentPath, context.resolveOptions);
864+
865+
// Don't attempt to parse JSON
866+
if (nodePath.extname(filePath) === '.json') {
867+
return {
868+
kind: "import",
869+
importKind,
870+
name,
871+
moduleSpecifier,
872+
};
873+
}
874+
875+
let file = loadFileSync(filePath);
863876

864877
let id;
865878
if (path.node.imported) {
@@ -933,6 +946,12 @@ function extractReactTypes(
933946
) {
934947
let plugins = ["jsx"];
935948

949+
if (resolveOptions && !resolveOptions.extensions) {
950+
// The resolve package that babel-file-loader uses only resolves .js files by default instead of the
951+
// default extension list of node (.js, .json and .node) so add .json back here.
952+
resolveOptions.extensions = ['.js', '.json'];
953+
}
954+
936955
if (typeSystem === "flow") plugins.push("flow");
937956
else if (typeSystem === "typescript") plugins.push("typescript");
938957
else throw new Error('typeSystem must be either "flow" or "typescript"');

test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,19 @@ const TESTS = [
809809
code: `
810810
class Component extends React.Component<{ a: number[] }> {}
811811
`
812+
},
813+
{
814+
name: "Should handle importing JSON files",
815+
typeSystem: "flow",
816+
code: `
817+
import { name, version } from "./__fixtures__/test";
818+
819+
class Component extends React.Component<{ a: string }> {
820+
defaultProps = {
821+
a: name,
822+
}
823+
}
824+
`
812825
}
813826
];
814827

@@ -825,7 +838,8 @@ for (let testCase of TESTS) {
825838

826839
testFn(testCase.name, () => {
827840
let code = stripIndent(testCase.code);
828-
let result = extractReactTypes(code, testCase.typeSystem);
841+
// Pass in file name so we can resolve imports to files in __fixtures__
842+
let result = extractReactTypes(code, testCase.typeSystem, __filename);
829843
expect(result).toMatchSnapshot();
830844
});
831845
}

0 commit comments

Comments
 (0)