-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vue-script-setup-converter): Remove defineComponent from import …
…declaration (#53) * hasNamedImportIdentifier * removeNamedImportIdentifier * feat(vue-script-setup-converter): Remove defineComponent from import declaration * Support defineNuxtComponent * Do not make side effects * convertDefineComponentImport * Use convertDefineComponentImport in convertSrc * fix: Do not make side effects to sourceFile * test: Add tests to defineComponentImportConverter.test.ts * renamed: packages/vue-script-setup-converter/src/lib/converter/defineComponentImportConverter.ts -> packages/vue-script-setup-converter/src/lib/converter/importDeclarationConverter.ts * Rename convertDefineComponentImport to convertImportDeclaration * format * Delete unnecessary lang
- Loading branch information
1 parent
e96b153
commit cbadbf5
Showing
8 changed files
with
228 additions
and
7 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
99 changes: 99 additions & 0 deletions
99
packages/vue-script-setup-converter/src/lib/converter/importDeclarationConverter.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,99 @@ | ||
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 { convertImportDeclaration } from "./importDeclarationConverter"; | ||
|
||
const parseScript = (input: string) => { | ||
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 = convertImportDeclaration(sourceFile); | ||
|
||
const formatedText = prettier.format(convertedImportDeclarationText, { | ||
parser: "typescript", | ||
plugins: [parserTypeScript], | ||
}); | ||
|
||
return formatedText; | ||
}; | ||
|
||
describe("convertImportDeclaration", () => { | ||
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/importDeclarationConverter.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 convertImportDeclaration = (sourceFile: SourceFile) => { | ||
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
53 changes: 53 additions & 0 deletions
53
packages/vue-script-setup-converter/src/lib/helpers/module.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,53 @@ | ||
import { expect, describe, it } from "vitest"; | ||
import { ScriptTarget, Project, ImportDeclaration } from "ts-morph"; | ||
import { parse } from "@vue/compiler-sfc"; | ||
import { hasNamedImportIdentifier } from "./module"; | ||
|
||
const getSourceFile = (input: string, lang: "js" | "ts" = "js") => { | ||
const { | ||
descriptor: { script }, | ||
} = parse(input); | ||
|
||
const project = new Project({ | ||
tsConfigFilePath: "tsconfig.json", | ||
compilerOptions: { | ||
target: ScriptTarget.Latest, | ||
}, | ||
}); | ||
|
||
return project.createSourceFile("s.tsx", script?.content ?? ""); | ||
}; | ||
|
||
describe("helpers/module", () => { | ||
describe("hasNamedImportIdentifier", () => { | ||
describe("when importDeclaration includes target namedImport", () => { | ||
const source = `<script>import { defineComponent, ref } from 'vue';</script>`; | ||
|
||
it("returns true", () => { | ||
const sourceFile = getSourceFile(source); | ||
const importDeclaration = sourceFile.getImportDeclaration("vue"); | ||
const result = hasNamedImportIdentifier( | ||
importDeclaration as ImportDeclaration, | ||
"defineComponent" | ||
); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
}); | ||
|
||
describe("when importDeclaration does not include target namedImport", () => { | ||
const source = `<script>import { ref } from 'vue';</script>`; | ||
|
||
it("returns true", () => { | ||
const sourceFile = getSourceFile(source); | ||
const importDeclaration = sourceFile.getImportDeclaration("vue"); | ||
const result = hasNamedImportIdentifier( | ||
importDeclaration as ImportDeclaration, | ||
"defineComponent" | ||
); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/vue-script-setup-converter/src/lib/helpers/module.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,12 @@ | ||
import type { ImportDeclaration } from "ts-morph"; | ||
|
||
export const hasNamedImportIdentifier = ( | ||
importDeclaration: ImportDeclaration, | ||
identifier: string | ||
): boolean => { | ||
return Boolean( | ||
importDeclaration.getNamedImports().find((namedImport) => { | ||
return namedImport.getName() === identifier; | ||
}) | ||
); | ||
}; |