Skip to content
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

Provide features in script block #296

Merged
merged 2 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/compiler-tsx/src/vue/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ export function compileWithDecodedSourceMap(
builder.append(
[
`import * as ${resolvedOptions.typeIdentifier} from '${resolvedOptions.typeCheckModuleName}';`,
`import { ${['defineComponent', 'GlobalComponents']
.map(
(id) => `${id} as ${resolvedOptions.internalIdentifierPrefix}${id}`,
)
.join(', ')} } from '${resolvedOptions.runtimeModuleName}';`,
`declare const ${
resolvedOptions.internalIdentifierPrefix
}defineComponent: typeof import(${JSON.stringify(
resolvedOptions.runtimeModuleName,
)}).defineComponent;`,
`type ${
resolvedOptions.internalIdentifierPrefix
}GlobalComponents = import(${JSON.stringify(
resolvedOptions.runtimeModuleName,
)}).GlobalComponents;`,
].join('\n'),
)
builder.nextLine()
Expand Down
165 changes: 99 additions & 66 deletions packages/compiler-tsx/test/__snapshots__/baseline.js

Large diffs are not rendered by default.

165 changes: 99 additions & 66 deletions packages/compiler-tsx/test/__snapshots__/baseline.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

exports[`Vue to TSX compiler if-else-if.vue: if-else-if.vue 1`] = `
"import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

const __VueDX___Script_Component = __VueDX__defineComponent({});
Expand Down Expand Up @@ -97,7 +98,8 @@ export default class IfElseIf extends IfElseIfPublic {

exports[`Vue to TSX compiler nested-for-slot.vue: nested-for-slot.vue 1`] = `
"import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

const __VueDX___Script_Component = __VueDX__defineComponent({});
Expand Down Expand Up @@ -219,7 +221,8 @@ export default class NestedForSlot extends NestedForSlotPublic {

exports[`Vue to TSX compiler ts-script-template.vue: ts-script-template.vue 1`] = `
"import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

import { defineComponent } from 'vue'
Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-tsx/test/fixtures/if-else-if.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

const __VueDX___Script_Component = __VueDX__defineComponent({});
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler-tsx/test/fixtures/if-else-if.tsx.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/compiler-tsx/test/fixtures/nested-for-slot.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

const __VueDX___Script_Component = __VueDX__defineComponent({});
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as __VueDX__TypeCheck from 'vuedx~runtime';
import { defineComponent as __VueDX__defineComponent, GlobalComponents as __VueDX__GlobalComponents } from 'vue';
declare const __VueDX__defineComponent: typeof import("vue").defineComponent;
type __VueDX__GlobalComponents = import("vue").GlobalComponents;
//#region <script>

import { defineComponent } from 'vue'
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 49 additions & 75 deletions packages/typescript-plugin-vue/src/features/CodeFixService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug } from '@vuedx/shared'
import { VueSFCDocument } from '@vuedx/vue-virtual-textdocument'
import { inject, injectable } from 'inversify'
import type { TSLanguageService, TypeScript } from '../contracts/TypeScript'
import { FilesystemService } from '../services/FilesystemService'
Expand All @@ -16,7 +16,6 @@ export class CodeFixService
private readonly fs: FilesystemService,
) {}

@debug()
public getCodeFixesAtPosition(
fileName: string,
start: number,
Expand All @@ -25,97 +24,72 @@ export class CodeFixService
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): readonly TypeScript.CodeFixAction[] {
return this.#resolveCodeFixActions(
this.fs.isVueFile(fileName)
? this.#getVueCodeFixesAtPosition(
fileName,
return (
this.pick(fileName, start, {
script: (file) => {
const span = file.findGeneratedTextSpan({
start,
end,
errorCodes,
formatOptions,
preferences,
length: end - start,
})
if (span == null) return []

return this.processCodeFixActions(
this.ts.service.getCodeFixesAtPosition(
file.generatedFileName,
span.start,
span.start + span.length,
errorCodes,
formatOptions,
preferences,
),
)
: this.ts.service.getCodeFixesAtPosition(
fileName,
start,
end,
errorCodes,
formatOptions,
preferences,
),
},
}) ?? []
)
}

#getVueCodeFixesAtPosition(
fileName: string,
start: number,
end: number,
errorCodes: readonly number[],
public getCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): readonly TypeScript.CodeFixAction[] {
const block = this.fs.getVueFile(fileName)
if (block == null) return []

const genreatedStart = block.generatedOffsetAt(start)
const generatedEnd = block.generatedOffsetAt(end)
if (genreatedStart == null || generatedEnd == null) return []
): TypeScript.CombinedCodeActions {
const file = this.fs.getVueFile(scope.fileName)
if (file == null) return { changes: [], commands: [] }

return this.ts.service.getCodeFixesAtPosition(
block.generatedFileName,
genreatedStart,
generatedEnd,
errorCodes,
const result = this.ts.service.getCombinedCodeFix(
{ ...scope, fileName: file.generatedFileName },
fixId,
formatOptions,
preferences,
)

return {
...result,
changes: this.fs.resolveAllFileTextChanges(result.changes),
}
}

#resolveCodeFixActions(
private pick<R>(
fileName: string,
position: number,
fns: Record<string, (file: VueSFCDocument) => R>,
): R | undefined {
const file = this.fs.getVueFile(fileName)
if (file == null) return
const block = file.getBlockAt(position)
if (block == null) return
const fn = fns[block.type]
if (fn == null) return
return fn(file)
}

public processCodeFixActions(
fixes: readonly TypeScript.CodeFixAction[],
): TypeScript.CodeFixAction[] {
return fixes.map((fix) => ({
...fix,
changes: this.fs.resolveAllFileTextChanges(fix.changes),
}))
}

@debug()
public getCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): TypeScript.CombinedCodeActions {
const result = this.fs.isVueFile(scope.fileName)
? this.#getVueCombinedCodeFix(scope, fixId, formatOptions, preferences)
: this.ts.service.getCombinedCodeFix(
scope,
fixId,
formatOptions,
preferences,
)

return {
commands: result.commands,
changes: this.fs.resolveAllFileTextChanges(result.changes),
}
}

#getVueCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): TypeScript.CombinedCodeActions {
const file = this.fs.getVueFile(scope.fileName)
if (file == null) return { changes: [] }

return this.ts.service.getCombinedCodeFix(
{ type: 'file', fileName: file.generatedFileName },
fixId,
formatOptions,
preferences,
)
}
}
Loading