-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: static import analyzes tools (#3)
Co-authored-by: Daniel Roe <daniel@roe.dev>
- Loading branch information
Showing
15 changed files
with
2,431 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
coverage | ||
dist | ||
test/fixture/imports.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ jobs: | |
cache: yarn | ||
- run: yarn install | ||
- run: yarn lint | ||
- run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { expect } from 'chai' | ||
import { findDynamicImports, findStaticImports, parseStaticImport } from '../lib/index.mjs' | ||
|
||
// -- Static import -- | ||
|
||
const staticTests = { | ||
|
||
'import defaultMember from "module-name";': { | ||
specifier: 'module-name', | ||
defaultImport: 'defaultMember' | ||
}, | ||
'import * as name from "module-name ";': { | ||
specifier: 'module-name', | ||
namespacedImport: 'name' | ||
}, | ||
'import * as name from "module-name "; //test': { | ||
specifier: 'module-name', | ||
namespacedImport: 'name' | ||
}, | ||
'import { member } from " module-name";': { | ||
specifier: 'module-name', | ||
namedImports: { member: 'member' } | ||
}, | ||
'import { member as alias } from "module-name";': { | ||
specifier: 'module-name', | ||
namedImports: { member: 'alias' } | ||
}, | ||
'import { member1, member2 as alias2, member3 as alias3 } from "module-name";': { | ||
specifier: 'module-name', | ||
namedImports: { member1: 'member1', member2: 'alias2', member3: 'alias3' } | ||
}, | ||
'import { member1, /* member0point5, */ member2 as alias2, member3 as alias3 } from "module-name";': { | ||
specifier: 'module-name', | ||
namedImports: { member1: 'member1', member2: 'alias2', member3: 'alias3' } | ||
}, | ||
'import defaultMember, { member, /* test */ member } from "module-name";': { | ||
specifier: 'module-name', | ||
defaultImport: 'defaultMember', | ||
namedImports: { member: 'member' } | ||
}, | ||
'import defaultMember, * as name from "module-name";': { | ||
specifier: 'module-name', | ||
defaultImport: 'defaultMember', | ||
namespacedImport: 'name' | ||
}, | ||
'import "module-name";': { | ||
specifier: 'module-name' | ||
} | ||
} | ||
|
||
staticTests[`import { | ||
member1, | ||
// test | ||
member2 | ||
} from "module-name";`] = { | ||
specifier: 'module-name', | ||
namedImports: { member1: 'member1', member2: 'member2' } | ||
} | ||
|
||
staticTests[`import { | ||
member1, | ||
member2 | ||
} from "module-name";`] = { | ||
specifier: 'module-name', | ||
namedImports: { member1: 'member1', member2: 'member2' } | ||
} | ||
|
||
staticTests[`import { | ||
Component | ||
} from '@angular2/core';`] = { | ||
specifier: '@angular2/core', | ||
type: 'static', | ||
namedImports: { Component: 'Component' } | ||
} | ||
|
||
// -- Dynamic import -- | ||
const dynamicTests = { | ||
'const { test, /* here */, another, } = await import ( "module-name" );': { | ||
expression: '"module-name"' | ||
}, | ||
'var promise = import ( "module-name" );': { | ||
expression: '"module-name"' | ||
}, | ||
'import ( "module-name" );': { | ||
expression: '"module-name"' | ||
}, | ||
'import(foo("123"))': { | ||
expression: 'foo("123")' | ||
}, | ||
'import("abc").then(r => r.default)': { | ||
expression: '"abc"' | ||
} | ||
} | ||
|
||
describe('findStaticImports', () => { | ||
for (const [input, test] of Object.entries(staticTests)) { | ||
it(input.replace(/\n/g, '\\n'), () => { | ||
const matches = findStaticImports(input) | ||
expect(matches.length).to.equal(1) | ||
|
||
const match = matches[0] | ||
expect(match.type).to.equal('static') | ||
|
||
expect(match.specifier).to.equal(test.specifier) | ||
|
||
const parsed = parseStaticImport(match) | ||
if (test.defaultImport) { | ||
expect(parsed.defaultImport).to.equals(test.defaultImport) | ||
} | ||
if (test.namedImports) { | ||
expect(parsed.namedImports).to.eql(test.namedImports) | ||
} | ||
if (test.namespacedImport) { | ||
expect(parsed.namespacedImport).to.eql(test.namespacedImport) | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
describe('findDynamicImports', () => { | ||
for (const [input, test] of Object.entries(dynamicTests)) { | ||
it(input.replace(/\n/g, '\\n'), () => { | ||
const matches = findDynamicImports(input) | ||
expect(matches.length).to.equal(1) | ||
const match = matches[0] | ||
expect(match.type).to.equal('dynamic') | ||
expect(match.expression.trim()).to.equal(test.expression) | ||
}) | ||
} | ||
}) |
Oops, something went wrong.