-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat(vue-script-setup-converter): Remove defineComponent from import declaration #53
Merged
wattanx
merged 13 commits into
wattanx:main
from
inouetakuya:feat-vue-script-setup-converter-remove-define-component
May 6, 2024
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cd39faf
hasNamedImportIdentifier
inouetakuya de32c29
removeNamedImportIdentifier
inouetakuya 05833b2
feat(vue-script-setup-converter): Remove defineComponent from import …
inouetakuya 968989c
Support defineNuxtComponent
inouetakuya f0a5ca6
Do not make side effects
inouetakuya 539a3de
convertDefineComponentImport
inouetakuya cea4082
Use convertDefineComponentImport in convertSrc
inouetakuya 005dcbe
fix: Do not make side effects to sourceFile
inouetakuya a28d604
test: Add tests to defineComponentImportConverter.test.ts
inouetakuya 25d7805
renamed: packages/vue-script-setup-converter/src/lib/converter/define…
inouetakuya fe59bc9
Rename convertDefineComponentImport to convertImportDeclaration
inouetakuya ff5b4a8
format
inouetakuya 8f2a338
Delete unnecessary lang
inouetakuya 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
100 changes: 100 additions & 0 deletions
100
packages/vue-script-setup-converter/src/lib/converter/defineComponentImportConverter.test.ts
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,100 @@ | ||
import { expect, describe, it } from "vitest"; | ||
import { ScriptTarget, Project } from "ts-morph"; | ||
import { parse } from "@vue/compiler-sfc"; | ||
import prettier from "prettier"; | ||
import parserTypeScript from "prettier/parser-typescript"; | ||
import { convertDefineComponentImport } from "./defineComponentImportConverter"; | ||
|
||
const parseScript = (input: string, lang: "js" | "ts" = "js") => { | ||
const { | ||
descriptor: { script }, | ||
} = parse(input); | ||
|
||
const project = new Project({ | ||
tsConfigFilePath: "tsconfig.json", | ||
compilerOptions: { | ||
target: ScriptTarget.Latest, | ||
}, | ||
}); | ||
|
||
const sourceFile = project.createSourceFile("s.tsx", script?.content ?? ""); | ||
const convertedImportDeclarationText = | ||
convertDefineComponentImport(sourceFile); | ||
|
||
const formatedText = prettier.format(convertedImportDeclarationText, { | ||
parser: "typescript", | ||
plugins: [parserTypeScript], | ||
}); | ||
|
||
return formatedText; | ||
}; | ||
|
||
describe("convertDefineComponentImport", () => { | ||
describe("when defineComponent is imported", () => { | ||
const source = `<script> | ||
import { defineComponent, ref } from 'vue'; | ||
|
||
export default defineComponent({ | ||
name: 'HelloWorld', | ||
}) | ||
</script>`; | ||
|
||
it("returns import declaration text removed defineComponent", () => { | ||
const output = parseScript(source); | ||
const expected = 'import { ref } from "vue";\n'; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe("when only defineComponent is imported", () => { | ||
const source = `<script> | ||
import { defineComponent } from 'vue'; | ||
|
||
export default defineComponent({ | ||
name: 'HelloWorld', | ||
}) | ||
</script>`; | ||
|
||
it("returns blank", () => { | ||
const output = parseScript(source); | ||
const expected = ""; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe("when defineNuxtComponent is imported", () => { | ||
const source = `<script> | ||
import { defineNuxtComponent, ref } from '#imports'; | ||
|
||
export default defineNuxtComponent({ | ||
name: 'HelloWorld', | ||
}) | ||
</script>`; | ||
|
||
it("returns import declaration text removed defineNuxtComponent", () => { | ||
const output = parseScript(source); | ||
const expected = 'import { ref } from "#imports";\n'; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
|
||
describe("when only defineNuxtComponent is imported", () => { | ||
const source = `<script> | ||
import { defineNuxtComponent } from '#imports'; | ||
|
||
export default defineNuxtComponent({ | ||
name: 'HelloWorld', | ||
}) | ||
</script>`; | ||
|
||
it("returns blank", () => { | ||
const output = parseScript(source); | ||
const expected = ""; | ||
|
||
expect(output).toBe(expected); | ||
}); | ||
}); | ||
}); |
39 changes: 39 additions & 0 deletions
39
packages/vue-script-setup-converter/src/lib/converter/defineComponentImportConverter.ts
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,39 @@ | ||||||
import type { ImportDeclaration, SourceFile } from "ts-morph"; | ||||||
import { hasNamedImportIdentifier } from "../helpers/module"; | ||||||
|
||||||
export const convertDefineComponentImport = (sourceFile: SourceFile) => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it is a convert to
Suggested change
|
||||||
let importDeclarationText = ""; | ||||||
|
||||||
sourceFile.getImportDeclarations().forEach((importDeclaration) => { | ||||||
if (hasNamedImportIdentifier(importDeclaration, "defineComponent")) { | ||||||
importDeclarationText = convertToImportDeclarationText( | ||||||
importDeclaration, | ||||||
"defineComponent" | ||||||
); | ||||||
} | ||||||
if (hasNamedImportIdentifier(importDeclaration, "defineNuxtComponent")) { | ||||||
importDeclarationText = convertToImportDeclarationText( | ||||||
importDeclaration, | ||||||
"defineNuxtComponent" | ||||||
); | ||||||
} | ||||||
}); | ||||||
|
||||||
return importDeclarationText; | ||||||
}; | ||||||
|
||||||
const convertToImportDeclarationText = ( | ||||||
importDeclaration: ImportDeclaration, | ||||||
identifier: string | ||||||
) => { | ||||||
const filteredNamedImports = importDeclaration | ||||||
.getNamedImports() | ||||||
.map((namedImport) => namedImport.getText()) | ||||||
.filter((text) => text !== identifier); | ||||||
|
||||||
if (filteredNamedImports.length === 0) return ""; | ||||||
|
||||||
return `import { ${filteredNamedImports.join( | ||||||
"," | ||||||
)} } from '${importDeclaration.getModuleSpecifierValue()}';`; | ||||||
}; |
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
Oops, something went wrong.
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.
Looking here,
convertDefineComponentImport
looks good. I preferconverterImportDeclaration
todefineComponentImport
.