-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove update template context logic
- Loading branch information
1 parent
2e1ff3d
commit bd53809
Showing
21 changed files
with
340 additions
and
463 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import { isGloballyWhitelisted } from '@vue/shared'; | ||
import type * as ts from 'typescript/lib/tsserverlibrary'; | ||
|
||
export function walkInterpolationFragment( | ||
ts: typeof import('typescript/lib/tsserverlibrary'), | ||
code: string, | ||
cb: (fragment: string, offset: number | undefined) => void, | ||
localVars: Record<string, number>, | ||
) { | ||
|
||
let ctxVarOffsets: number[] = []; | ||
let localVarOffsets: number[] = []; | ||
|
||
const ast = ts.createSourceFile('/foo.ts', code, ts.ScriptTarget.ESNext); | ||
const varCb = (localVar: ts.Identifier) => { | ||
if ( | ||
!!localVars[localVar.text] || | ||
isGloballyWhitelisted(localVar.text) || | ||
localVar.text === '$style' | ||
) { | ||
localVarOffsets.push(localVar.getStart(ast)); | ||
} | ||
else { | ||
ctxVarOffsets.push(localVar.getStart(ast)); | ||
} | ||
}; | ||
ast.forEachChild(node => walkIdentifiers(ts, node, varCb, localVars)); | ||
|
||
ctxVarOffsets = ctxVarOffsets.sort((a, b) => a - b); | ||
localVarOffsets = localVarOffsets.sort((a, b) => a - b); | ||
|
||
if (ctxVarOffsets.length) { | ||
|
||
cb(code.substring(0, ctxVarOffsets[0]), 0); | ||
|
||
for (let i = 0; i < ctxVarOffsets.length - 1; i++) { | ||
cb('__VLS_ctx.', undefined); | ||
cb(code.substring(ctxVarOffsets[i], ctxVarOffsets[i + 1]), ctxVarOffsets[i]); | ||
} | ||
|
||
cb('__VLS_ctx.', undefined); | ||
cb(code.substring(ctxVarOffsets[ctxVarOffsets.length - 1]), ctxVarOffsets[ctxVarOffsets.length - 1]); | ||
} | ||
else { | ||
cb(code, 0); | ||
} | ||
} | ||
|
||
function walkIdentifiers( | ||
ts: typeof import('typescript/lib/tsserverlibrary'), | ||
node: ts.Node, | ||
cb: (varNode: ts.Identifier) => void, | ||
localVars: Record<string, number>, | ||
) { | ||
|
||
const blockVars: string[] = []; | ||
|
||
if (ts.isIdentifier(node)) { | ||
cb(node); | ||
} | ||
else if (ts.isPropertyAccessExpression(node)) { | ||
walkIdentifiers(ts, node.expression, cb, localVars); | ||
} | ||
else if (ts.isVariableDeclaration(node)) { | ||
|
||
colletVars(ts, node.name, blockVars); | ||
|
||
for (const varName of blockVars) | ||
localVars[varName] = (localVars[varName] ?? 0) + 1; | ||
|
||
if (node.initializer) | ||
walkIdentifiers(ts, node.initializer, cb, localVars); | ||
} | ||
else if (ts.isArrowFunction(node)) { | ||
|
||
const functionArgs: string[] = []; | ||
|
||
for (const param of node.parameters) | ||
colletVars(ts, param.name, functionArgs); | ||
|
||
for (const varName of functionArgs) | ||
localVars[varName] = (localVars[varName] ?? 0) + 1; | ||
|
||
walkIdentifiers(ts, node.body, cb, localVars); | ||
|
||
for (const varName of functionArgs) | ||
localVars[varName]--; | ||
} | ||
else if (ts.isObjectLiteralExpression(node)) { | ||
for (const prop of node.properties) { | ||
if (ts.isPropertyAssignment(prop)) { | ||
walkIdentifiers(ts, prop.initializer, cb, localVars); | ||
} | ||
} | ||
} | ||
else if (ts.isTypeReferenceNode(node)) { | ||
// ignore | ||
} | ||
else { | ||
node.forEachChild(node => walkIdentifiers(ts, node, cb, localVars)); | ||
} | ||
|
||
for (const varName of blockVars) | ||
localVars[varName]--; | ||
} | ||
|
||
export function colletVars( | ||
ts: typeof import('typescript/lib/tsserverlibrary'), | ||
node: ts.Node, | ||
result: string[], | ||
) { | ||
if (ts.isIdentifier(node)) { | ||
result.push(node.text); | ||
} | ||
else if (ts.isObjectBindingPattern(node)) { | ||
for (const el of node.elements) { | ||
colletVars(ts, el.name, result); | ||
} | ||
} | ||
else if (ts.isArrayBindingPattern(node)) { | ||
for (const el of node.elements) { | ||
if (ts.isBindingElement(el)) { | ||
colletVars(ts, el.name, result); | ||
} | ||
} | ||
} | ||
else { | ||
node.forEachChild(node => colletVars(ts, node, result)); | ||
} | ||
} |
Oops, something went wrong.