forked from mirrors/misskey
enhance: estree-walkerをoxc-walkerに変更 (#17556)
* enhance: estree-walkerをoxc-walkerに変更 * fix lint [ci skip]
This commit is contained in:
parent
5157c277f1
commit
0b4764c68b
7 changed files with 74 additions and 36 deletions
|
|
@ -4,23 +4,12 @@
|
|||
*/
|
||||
|
||||
import { parseAst } from 'rolldown/parseAst';
|
||||
import * as estreeWalker from 'estree-walker';
|
||||
import { walk } from 'oxc-walker';
|
||||
import { assertNever } from '../utils.js';
|
||||
import type { ESTree } from 'rolldown/utils';
|
||||
import type { LocaleInliner, TextModification } from '../locale-inliner.js';
|
||||
import type { Logger } from '../logger.js';
|
||||
|
||||
// WalkerContext is not exported from estree-walker, so we define it here
|
||||
interface WalkerContext {
|
||||
skip: () => void;
|
||||
}
|
||||
|
||||
const walk = estreeWalker.walk as {
|
||||
(node: ESTree.Node, callback: {
|
||||
enter?: (this: WalkerContext, node: ESTree.Node, parent: ESTree.Node | null, property: string | number | symbol | null | undefined) => void;
|
||||
}): void;
|
||||
};
|
||||
|
||||
export function collectModifications(sourceCode: string, fileName: string, fileLogger: Logger, inliner: LocaleInliner): TextModification[] {
|
||||
if (sourceCode === '') return [];
|
||||
let programNode: ESTree.Program;
|
||||
|
|
@ -42,7 +31,7 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|||
// 2) replace all `localStorage.getItem("lang")` with `localeName` variable
|
||||
// 3) replace all `await window.fetch(`/assets/locales/${d}.${x}.json`).then(u=>u.json())` with `localeJson` variable
|
||||
walk(programNode, {
|
||||
enter(this: WalkerContext, node: ESTree.Node) {
|
||||
enter(this, node) {
|
||||
if (node.type === 'Literal' && typeof node.value === 'string' && node.raw) {
|
||||
if (node.raw.substring(1).startsWith(inliner.scriptsDir)) {
|
||||
// we find `scripts/\w+\.js` literal and replace 'scripts' part with locale code
|
||||
|
|
@ -130,13 +119,15 @@ export function collectModifications(sourceCode: string, fileName: string, fileL
|
|||
const toSkip = new Set();
|
||||
toSkip.add(i18nImport);
|
||||
walk(programNode, {
|
||||
enter(this: WalkerContext, node, parent, property) {
|
||||
enter(this, node, parent, ctx) {
|
||||
if (toSkip.has(node)) {
|
||||
// This is the import specifier, skip processing it
|
||||
this.skip();
|
||||
return;
|
||||
}
|
||||
|
||||
const property = ctx.key;
|
||||
|
||||
// We don't care original name part of the import declaration
|
||||
if (node.type === 'ImportDeclaration') this.skip();
|
||||
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@
|
|||
"rollup": "4.60.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"estree-walker": "3.0.3",
|
||||
"i18n": "workspace:*",
|
||||
"magic-string": "0.30.21",
|
||||
"oxc-walker": "1.0.0",
|
||||
"rolldown": "1.0.3",
|
||||
"vite": "8.0.14"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import * as estreeWalker from 'estree-walker';
|
||||
import { walk } from 'oxc-walker';
|
||||
import { RolldownMagicString } from 'rolldown';
|
||||
import { assertType } from './utils.js';
|
||||
import type { ESTree } from 'rolldown/utils';
|
||||
|
|
@ -27,8 +27,7 @@ export function pluginRemoveUnrefI18n(
|
|||
if (!code.includes('unref(i18n)')) return null;
|
||||
const ast = this.parse(code);
|
||||
const magicString = meta.magicString ?? new RolldownMagicString(code);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(estreeWalker.walk as any)(ast, {
|
||||
walk(ast, {
|
||||
enter(node: ESTree.Node) {
|
||||
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'unref'
|
||||
&& node.arguments.length === 1) {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@
|
|||
"@rollup/pluginutils": "5.4.0",
|
||||
"@vitejs/plugin-vue": "6.0.7",
|
||||
"buraha": "0.0.1",
|
||||
"estree-walker": "3.0.3",
|
||||
"frontend-shared": "workspace:*",
|
||||
"i18n": "workspace:*",
|
||||
"icons-subsetter": "workspace:*",
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import * as estreeWalker from 'estree-walker';
|
||||
import { walk } from 'oxc-walker';
|
||||
import type { Plugin } from 'vite';
|
||||
import type { ESTree } from 'rolldown/utils';
|
||||
import { RolldownMagicString } from 'rolldown';
|
||||
|
|
@ -185,7 +185,7 @@ function isClassProperty(node: ESTree.Node | null): node is Extract<ESTree.Node,
|
|||
}
|
||||
|
||||
export function unwindCssModuleClassName(ast: ESTree.Node, magicString: RolldownMagicString): void {
|
||||
(estreeWalker.walk as any)(ast, {
|
||||
walk(ast, {
|
||||
enter(node: ESTree.Node, parent: ESTree.Node | null): void {
|
||||
//#region
|
||||
if (parent?.type !== 'Program') return;
|
||||
|
|
@ -267,7 +267,7 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
*/
|
||||
//#endregion
|
||||
//#region
|
||||
(estreeWalker.walk as any)(render.body, {
|
||||
walk(render.body, {
|
||||
enter(childNode: ESTree.Node) {
|
||||
if (!isCssModuleReference(childNode, ctx.name, key)) return;
|
||||
const actualKey = getMemberPropertyName(childNode.property, childNode.computed);
|
||||
|
|
@ -278,6 +278,9 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
this.replace({
|
||||
type: 'Literal',
|
||||
value: actualValue,
|
||||
raw: JSON.stringify(actualValue),
|
||||
start: childNode.start,
|
||||
end: childNode.end,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
|
@ -314,7 +317,7 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
*/
|
||||
//#endregion
|
||||
//#region
|
||||
(estreeWalker.walk as any)(render.body, {
|
||||
walk(render.body, {
|
||||
enter(childNode: ESTree.Node) {
|
||||
if (!isCssModuleReference(childNode, ctx.name, key)) return;
|
||||
const actualKey = getMemberPropertyName(childNode.property, childNode.computed);
|
||||
|
|
@ -357,7 +360,7 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
*/
|
||||
//#endregion
|
||||
//#region
|
||||
(estreeWalker.walk as any)(render.body, {
|
||||
walk(render.body, {
|
||||
enter(childNode: ESTree.Node, childParent: ESTree.Node | null) {
|
||||
if (childNode.type !== 'CallExpression') return;
|
||||
if (childNode.arguments.length !== 1) return;
|
||||
|
|
@ -404,7 +407,7 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
}
|
||||
const hasRemainingCssModuleReference = Array.from(moduleForest.keys()).some((key) => {
|
||||
let found = false;
|
||||
(estreeWalker.walk as any)(render.body, {
|
||||
walk(render.body, {
|
||||
enter(childNode: ESTree.Node) {
|
||||
if (!isCssModuleAccess(childNode, ctx.name, key)) return;
|
||||
found = true;
|
||||
|
|
@ -417,7 +420,7 @@ export function unwindCssModuleClassName(ast: ESTree.Node, magicString: Rolldown
|
|||
//#region
|
||||
if (node.declarations[0].init.arguments[1].elements.length === 1) {
|
||||
if (componentNode.type === 'Identifier') {
|
||||
(estreeWalker.walk as any)(ast, {
|
||||
walk(ast, {
|
||||
enter(childNode: ESTree.Node) {
|
||||
if (childNode.type !== 'Identifier') return;
|
||||
if (childNode.name !== componentNode.name) return;
|
||||
|
|
|
|||
|
|
@ -116,7 +116,6 @@
|
|||
"cypress": "15.16.0",
|
||||
"eslint-plugin-import": "2.32.0",
|
||||
"eslint-plugin-vue": "10.9.1",
|
||||
"estree-walker": "3.0.3",
|
||||
"happy-dom": "20.9.0",
|
||||
"intersection-observer": "0.12.2",
|
||||
"lightningcss": "1.32.0",
|
||||
|
|
@ -125,6 +124,7 @@
|
|||
"msw": "2.14.6",
|
||||
"msw-storybook-addon": "2.0.7",
|
||||
"nodemon": "3.1.14",
|
||||
"oxc-walker": "1.0.0",
|
||||
"prettier": "3.8.3",
|
||||
"react": "19.2.6",
|
||||
"react-dom": "19.2.6",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue