Skip to content

Commit

Permalink
Fix imports with "as" in their name (#7)
Browse files Browse the repository at this point in the history
* fix imports with "as" in their name

* bump version

* make test more explicit
  • Loading branch information
cody-dot-js authored Aug 26, 2019
1 parent 61f5778 commit 322ade5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
38 changes: 28 additions & 10 deletions __tests__/parseNamedImports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ test("when given a default import and named imports it returns the named imports
alias: "useEffect"
}
];
const defaultImport = 'import React, { useState, useEffect } from "react";';
const result = parseNamedImports(defaultImport);
const importExample = 'import React, { useState, useEffect } from "react";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});
Expand All @@ -35,8 +35,8 @@ test("when given named imports only it returns the named imports", () => {
alias: "useEffect"
}
];
const defaultImport = 'import { useState, useEffect } from "react";';
const result = parseNamedImports(defaultImport);
const importExample = 'import { useState, useEffect } from "react";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});
Expand All @@ -48,24 +48,42 @@ test("when given an aliased named import it returns the aliased named imports",
alias: "useFoo"
}
];
const defaultImport = 'import { useState as useFoo } from "react";';
const result = parseNamedImports(defaultImport);
const importExample = 'import { useState as useFoo } from "react";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});

test("when given an aliased named import with 'as' in the name it returns the aliased named imports", () => {
const expected = [
{
name: "asFoo",
alias: "asBar"
},
{
name: "asImport",
alias: "asImport"
}
];
const importExample =
'import { asFoo as asBar, asImport } from "module-name";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});

test("when given a default import it returns an empty list", () => {
const expected = [];
const defaultImport = 'import React from "react";';
const result = parseNamedImports(defaultImport);
const importExample = 'import React from "react";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});

test("when given a star import it returns an empty list", () => {
const expected = [];
const defaultImport = 'import * as myPrecious from "react";';
const result = parseNamedImports(defaultImport);
const importExample = 'import * as myPrecious from "react";';
const result = parseNamedImports(importExample);

expect(result).toStrictEqual(expected);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parse-static-imports",
"description": "Gracefully parse ECMAScript static imports 💃",
"version": "1.0.4",
"version": "1.0.5",
"author": "Cody A Price <[email protected]>",
"bugs": {
"url": "https://github.com/dev-cprice/parse-static-imports/issues"
Expand Down
4 changes: 2 additions & 2 deletions src/parseNamedImports.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function createNamedImport(str = "") {
const asIdx = str.indexOf("as");
const asIdx = str.indexOf(" as ");

if (asIdx < 0) {
const name = str.trim();
return { name, alias: name };
}

const alias = str.substring(asIdx + 2).trim();
const alias = str.substring(asIdx + 4).trim();
const name = str.substring(0, asIdx).trim();

return { name, alias };
Expand Down

0 comments on commit 322ade5

Please sign in to comment.