-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(remark-plugin-npm2yarn): migrate package to TS #5931
Merged
+128
−116
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71eba8d
refactor(remark-plugin-npm2yarn): migrate package to TS
duanwilliam 3f7cb76
fix(remark-plugin-npm2yarn): type as unified Plugin
duanwilliam c25a971
refactor(remark-plugin-npm2yarn): standardize code style with remark …
duanwilliam efe9d63
Use unist-util-visit
Josh-Cena 52d0bfb
Use export =
Josh-Cena e1a0e8d
Remove unneeded includes option
Josh-Cena 264c07a
Fix tests
Josh-Cena 075b4de
Migrate test to TS
Josh-Cena b894444
Make output look better
Josh-Cena File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 was deleted.
Oops, something went wrong.
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,95 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import type {Code, Content, Literal} from 'mdast'; | ||
import type {Plugin, Transformer} from 'unified'; | ||
import type {Node, Parent} from 'unist'; | ||
import visit from 'unist-util-visit'; | ||
import npmToYarn from 'npm-to-yarn'; | ||
|
||
interface PluginOptions { | ||
sync?: boolean; | ||
} | ||
|
||
// E.g. global install: 'npm i' -> 'yarn' | ||
const convertNpmToYarn = (npmCode: string) => npmToYarn(npmCode, 'yarn'); | ||
|
||
const transformNode = (node: Code, isSync: boolean) => { | ||
const groupIdProp = isSync ? ' groupId="npm2yarn"' : ''; | ||
const npmCode = node.value; | ||
const yarnCode = convertNpmToYarn(node.value); | ||
return [ | ||
{ | ||
type: 'jsx', | ||
value: `<Tabs${groupIdProp}>\n<TabItem value="npm">`, | ||
}, | ||
{ | ||
type: node.type, | ||
lang: node.lang, | ||
value: npmCode, | ||
}, | ||
{ | ||
type: 'jsx', | ||
value: '</TabItem>\n<TabItem value="yarn" label="Yarn">', | ||
}, | ||
{ | ||
type: node.type, | ||
lang: node.lang, | ||
value: yarnCode, | ||
}, | ||
{ | ||
type: 'jsx', | ||
value: '</TabItem>\n</Tabs>', | ||
}, | ||
] as Content[]; | ||
}; | ||
|
||
const isImport = (node: Node): node is Literal => node.type === 'import'; | ||
const isParent = (node: Node): node is Parent => | ||
Array.isArray((node as Parent).children); | ||
const matchNode = (node: Node): node is Code => | ||
node.type === 'code' && (node as Code).meta === 'npm2yarn'; | ||
const nodeForImport: Literal = { | ||
type: 'import', | ||
value: | ||
"import Tabs from '@theme/Tabs';\nimport TabItem from '@theme/TabItem';", | ||
}; | ||
|
||
const plugin: Plugin<[PluginOptions?]> = (options = {}) => { | ||
const {sync = false} = options; | ||
let transformed = false; | ||
let alreadyImported = false; | ||
const transformer: Transformer = (root) => { | ||
visit(root, (node: Node) => { | ||
if (isImport(node) && node.value.includes('@theme/Tabs')) { | ||
alreadyImported = true; | ||
} | ||
if (isParent(node)) { | ||
let index = 0; | ||
while (index < node.children.length) { | ||
const child = node.children[index]; | ||
if (matchNode(child)) { | ||
const result = transformNode(child, sync); | ||
node.children.splice(index, 1, ...result); | ||
index += result.length; | ||
transformed = true; | ||
} else { | ||
index += 1; | ||
} | ||
} | ||
} | ||
}); | ||
if (transformed && !alreadyImported) { | ||
(root as Parent).children.unshift(nodeForImport); | ||
} | ||
}; | ||
return transformer; | ||
}; | ||
|
||
// To continue supporting `require('npm2yarn')` without the `.default` ㄟ(▔,▔)ㄏ | ||
// TODO change to export default after migrating to ESM | ||
export = plugin; | ||
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,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"incremental": true, | ||
"tsBuildInfoFile": "./lib/.tsbuildinfo", | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's annoying me due to not working with Babel, isn't there a better way to handle that interop problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is:
This will be transpiled as:
Because
exports
is an alias formodule.exports
, the last line decouples theexports
reference frommodule.exports
and makes the second last line useless. However this makes everyone happy