-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduced a new codemod to remove unused imports once finish
- Loading branch information
Sergio
committed
Oct 26, 2020
1 parent
a56f44d
commit c5bdbe1
Showing
9 changed files
with
174 additions
and
26 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
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
14 changes: 14 additions & 0 deletions
14
transforms/__testfixtures__/remove-unused-imports/remove-unused-imports.input.js
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,14 @@ | ||
import { React, core } from "react"; | ||
import { date, number } from "@lingui/core"; | ||
import { t } from "@lingui/macro"; | ||
|
||
export default () => { | ||
const [gi, setgi] = React.useState(false) | ||
return ( | ||
<> | ||
{t`Some example`} | ||
{date(new Date())} | ||
<div>hola</div> | ||
</> | ||
) | ||
} |
14 changes: 14 additions & 0 deletions
14
transforms/__testfixtures__/remove-unused-imports/remove-unused-imports.output.js
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,14 @@ | ||
import { React } from "react"; | ||
import { date } from "@lingui/core"; | ||
import { t } from "@lingui/macro"; | ||
|
||
export default () => { | ||
const [gi, setgi] = React.useState(false) | ||
return ( | ||
<> | ||
{t`Some example`} | ||
{date(new Date())} | ||
<div>hola</div> | ||
</> | ||
) | ||
} |
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,10 @@ | ||
import { defineTest } from "jscodeshift/dist/testUtils"; | ||
|
||
describe("We remove all the unused imports", () => { | ||
defineTest( | ||
__dirname, | ||
"remove-unused-imports", | ||
null, | ||
"remove-unused-imports/remove-unused-imports", | ||
); | ||
}); |
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,60 @@ | ||
import { Transform } from "jscodeshift"; | ||
|
||
const transform: Transform = (fileInfo, api, options) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
const filterAndTransformRequires = path => { | ||
const specifiers = path.value.specifiers; | ||
const parentScope = j(path).closestScope(); | ||
return ( | ||
specifiers.filter(importPath => { | ||
const varName = importPath.local.name; | ||
const requireName = importPath.imported | ||
? importPath.imported.name | ||
: importPath.local.name; | ||
const scopeNode = path.scope.node; | ||
|
||
// We need this to make sure the JSX transform can use `React` | ||
if (requireName === "React") { | ||
return false; | ||
} | ||
|
||
// console.debug("parsing require named ", requireName); | ||
// Remove required vars that aren't used. | ||
const identifierUsages = parentScope | ||
.find(j.Identifier, { name: varName }) | ||
// Ignore require vars | ||
.filter(identifierPath => identifierPath.parentPath.value !== importPath); | ||
const decoratorUsages = parentScope.find(j.ClassDeclaration).filter((it: any) => { | ||
return ( | ||
(it.value.decorators || []).filter( | ||
decorator => decorator.expression.name === varName | ||
).length > 0 | ||
); | ||
}); | ||
|
||
if (!identifierUsages.size() && !decoratorUsages.size()) { | ||
path.value.specifiers = path.value.specifiers.filter( | ||
it => (it === importPath) === false | ||
); | ||
if (path.value.specifiers.length === 0) { | ||
j(path).remove(); | ||
} | ||
return true; | ||
} | ||
}).length > 0 | ||
); | ||
}; | ||
|
||
const didTransform = | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter(filterAndTransformRequires) | ||
.size() > 0; | ||
|
||
return didTransform ? root.toSource(options.printOptions) : null; | ||
}; | ||
|
||
|
||
export default transform; |
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