Skip to content

Commit

Permalink
use proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
so1ve committed May 7, 2024
1 parent 109e094 commit 6590f0c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
6 changes: 3 additions & 3 deletions packages/language-core/lib/parsers/scriptRanges.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { TextRange } from '../types';
import type * as ts from 'typescript';
import { BindingTypes, getNodeText, getStartEnd, parseBindings } from '../utils/parseBindings';
import { createTscApiShim } from '../utils/tscApiShim';
import { injectTscApiShim } from '../utils/tscApiShim';

export interface ScriptRanges extends ReturnType<typeof parseScriptRanges> { }

export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.SourceFile, hasScriptSetup: boolean, withNode: boolean) {

const tscApiShim = createTscApiShim(ts);
ts = injectTscApiShim(ts);

let exportDefault: (TextRange & {
expression: TextRange,
Expand All @@ -27,7 +27,7 @@ export function parseScriptRanges(ts: typeof import('typescript'), ast: ts.Sourc
if (ts.isExportAssignment(raw)) {

let node: ts.AsExpression | ts.ExportAssignment | ts.ParenthesizedExpression = raw;
while (tscApiShim.isAsExpression(node.expression) || ts.isParenthesizedExpression(node.expression)) { // fix https://github.com/vuejs/language-tools/issues/1882
while (ts.isAsExpression(node.expression) || ts.isParenthesizedExpression(node.expression)) { // fix https://github.com/vuejs/language-tools/issues/1882
node = node.expression;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/utils/parseBindings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as ts from 'typescript';
import type { TextRange, VueCompilerOptions } from '../types';
import { createTscApiShim } from './tscApiShim';
import { injectTscApiShim } from './tscApiShim';

export enum BindingTypes {
NoUnref,
Expand All @@ -13,7 +13,7 @@ export function parseBindings(
sourceFile: ts.SourceFile,
vueCompilerOptions?: VueCompilerOptions,
) {
const tscApiShim = createTscApiShim(ts);
ts = injectTscApiShim(ts);
const bindingRanges: TextRange[] = [];
// `bindingTypes` may include some bindings that are not in `bindingRanges`, such as `foo` in `defineProps({ foo: Number })`
const bindingTypes = new Map<string, BindingTypes>();
Expand Down Expand Up @@ -182,9 +182,9 @@ export function parseBindings(
}
function getValueNode(node: ts.Node) {
if (
tscApiShim.isAsExpression(node)
ts.isAsExpression(node)
|| ts.isSatisfiesExpression(node)
|| tscApiShim.isTypeAssertionExpression(node)
|| ts.isTypeAssertionExpression(node)
|| ts.isParenthesizedExpression(node)
|| ts.isNonNullExpression(node)
|| ts.isExpressionWithTypeArguments(node)
Expand Down
24 changes: 17 additions & 7 deletions packages/language-core/lib/utils/tscApiShim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type * as ts from 'typescript';
/**
* Provide missing functions in tsc context.
*/
export function createTscApiShim(ts: typeof import('typescript')) {
export function injectTscApiShim(ts: typeof import('typescript')) {
function isAsExpression(node: ts.Node): node is ts.AsExpression {
return node.kind === ts.SyntaxKind.AsExpression;
}
Expand All @@ -14,9 +14,19 @@ export function createTscApiShim(ts: typeof import('typescript')) {
return node.kind === ts.SyntaxKind.TemplateExpression;
}

return {
isAsExpression,
isTypeAssertionExpression,
isTemplateExpression,
};
};
return new Proxy(ts, {
get(target, key) {
if (key === 'isAsExpression') {
return isAsExpression;
}
else if (key === 'isTypeAssertionExpression') {
return isTypeAssertionExpression;
}
else if (key === 'isTemplateExpression') {
return isTemplateExpression;
}

return target[key as keyof typeof target];
}
});
};

0 comments on commit 6590f0c

Please sign in to comment.