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

refactor: remove acorn #16238

Merged
merged 6 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,11 @@
"overrides": {
"vite": "workspace:*"
},
"packageExtensions": {
"acorn-walk": {
"peerDependencies": {
"acorn": "*"
},
"peerDependenciesMeta": {
"acorn": {
"optional": true
}
}
}
},
"patchedDependencies": {
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
},
"peerDependencyRules": {
"allowedVersions": {
Expand Down
58 changes: 0 additions & 58 deletions packages/vite/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,64 +587,6 @@ Repository: rollup/plugins

---------------------------------------

## acorn
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Repository: https://github.com/acornjs/acorn.git

> MIT License
>
> Copyright (C) 2012-2022 by various contributors (see AUTHORS)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.

---------------------------------------

## acorn-walk
License: MIT
By: Marijn Haverbeke, Ingvar Stepanyan, Adrian Heine
Repository: https://github.com/acornjs/acorn.git

> MIT License
>
> Copyright (C) 2012-2020 by various contributors (see AUTHORS)
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.

---------------------------------------

## ansi-regex
License: MIT
By: Sindre Sorhus
Expand Down
2 changes: 0 additions & 2 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@
"@rollup/pluginutils": "^5.1.0",
"@types/escape-html": "^1.0.4",
"@types/pnpapi": "^0.0.5",
"acorn": "^8.11.3",
"acorn-walk": "^8.3.2",
"artichokie": "^0.2.0",
"cac": "^6.7.14",
"chokidar": "^3.6.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('parse positives', async () => {
describe('parse negatives', async () => {
it('syntax error', async () => {
expect(await runError('import.meta.glob(')).toMatchInlineSnapshot(
'[SyntaxError: Unexpected token (1:17)]',
'[Error: Invalid glob import syntax: Close parenthesis not found]',
)
})

Expand Down
9 changes: 2 additions & 7 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { posix } from 'node:path'
import MagicString from 'magic-string'
import { init, parse as parseImports } from 'es-module-lexer'
import type { ImportSpecifier } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import { parseAst } from 'rollup/parseAst'
import { dynamicImportToGlob } from '@rollup/plugin-dynamic-import-vars'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
Expand Down Expand Up @@ -55,12 +55,7 @@ function parseDynamicImportPattern(
strings: string,
): DynamicImportPattern | null {
const filename = strings.slice(1, -1)
const ast = (
parseJS(strings, {
ecmaVersion: 'latest',
sourceType: 'module',
}) as any
).body[0].expression
const ast = (parseAst(strings).body[0] as any).expression

const userPatternQuery = dynamicImportToGlob(ast, filename)
if (!userPatternQuery) {
Expand Down
29 changes: 17 additions & 12 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import type {
ImportSpecifier,
} from 'es-module-lexer'
import { init, parse as parseImports } from 'es-module-lexer'
import { parse as parseJS } from 'acorn'
import type { Node } from 'estree'
import { findStaticImports, parseStaticImport } from 'mlly'
import { parseAst } from 'rollup/parseAst'
import type { StaticImport } from 'mlly'
import { ESM_STATIC_IMPORT_RE, parseStaticImport } from 'mlly'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import { makeLegalIdentifier } from '@rollup/pluginutils'
import type { ViteDevServer } from '..'
import {
Expand Down Expand Up @@ -118,11 +118,21 @@ function extractImportedBindings(
}

const exp = source.slice(importSpec.ss, importSpec.se)
const [match0] = findStaticImports(exp)
if (!match0) {
ESM_STATIC_IMPORT_RE.lastIndex = 0
const match = ESM_STATIC_IMPORT_RE.exec(exp)
if (!match) {
return
}
const parsed = parseStaticImport(match0)

const staticImport: StaticImport = {
type: 'static',
code: match[0],
start: match.index,
end: match.index + match[0].length,
imports: match.groups!.imports,
specifier: match.groups!.specifier,
}
const parsed = parseStaticImport(staticImport)
if (!parsed) {
return
}
Expand Down Expand Up @@ -931,12 +941,7 @@ export function transformCjsImport(
importer: string,
config: ResolvedConfig,
): string | undefined {
const node = (
parseJS(importExp, {
ecmaVersion: 'latest',
sourceType: 'module',
}) as any
).body[0] as Node
const node = parseAst(importExp).body[0]

// `export * from '...'` may cause unexpected problem, so give it a warning
if (
Expand Down
99 changes: 50 additions & 49 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@ import { stripLiteral } from 'strip-literal'
import colors from 'picocolors'
import type {
ArrayExpression,
CallExpression,
Expression,
Literal,
MemberExpression,
Node,
SequenceExpression,
SpreadElement,
TemplateLiteral,
} from 'estree'
import { parseExpressionAt } from 'acorn'
import type { CustomPluginOptions, RollupError } from 'rollup'
import { findNodeAt } from 'acorn-walk'
import type { CustomPluginOptions, RollupAstNode, RollupError } from 'rollup'
import MagicString from 'magic-string'
import fg from 'fast-glob'
import { stringifyQuery } from 'ufo'
import type { GeneralImportGlobOptions } from 'types/importGlob'
import { parseAstAsync } from 'rollup/parseAst'
import type { Plugin } from '../plugin'
import type { ViteDevServer } from '../server'
import type { ModuleNode } from '../server/moduleGraph'
Expand Down Expand Up @@ -218,7 +214,7 @@ export async function parseImportGlob(
resolveId: IdResolver,
logger?: Logger,
): Promise<ParsedImportGlob[]> {
let cleanCode
let cleanCode: string
try {
cleanCode = stripLiteral(code)
} catch (e) {
Expand All @@ -236,51 +232,30 @@ export async function parseImportGlob(
return e
}

let ast: CallExpression | SequenceExpression | MemberExpression
let lastTokenPos: number | undefined

try {
ast = parseExpressionAt(code, start, {
ecmaVersion: 'latest',
sourceType: 'module',
ranges: true,
onToken: (token) => {
lastTokenPos = token.end
},
}) as any
} catch (e) {
const _e = e as any
if (_e.message && _e.message.startsWith('Unterminated string constant'))
return undefined!
if (lastTokenPos == null || lastTokenPos <= start) throw _e

// tailing comma in object or array will make the parser think it's a comma operation
// we try to parse again removing the comma
try {
const statement = code.slice(start, lastTokenPos).replace(/[,\s]*$/, '')
ast = parseExpressionAt(
' '.repeat(start) + statement, // to keep the ast position
start,
{
ecmaVersion: 'latest',
sourceType: 'module',
ranges: true,
},
) as any
} catch {
throw _e
}
const end =
findCorrespondingCloseParenthesisPosition(
cleanCode,
start + match[0].length,
) + 1
Comment on lines +235 to +239
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous implementation relied on acorn to find the end position of import.meta.glob(. In this new implementation, it finds the close parenthesis of import.meta.glob( instead.

if (end <= 0) {
throw err('Close parenthesis not found')
}

const found = findNodeAt(ast as any, start, undefined, 'CallExpression')
if (!found) throw err(`Expect CallExpression, got ${ast.type}`)
ast = found.node as unknown as CallExpression
const statementCode = code.slice(start, end)

const rootAst = (await parseAstAsync(statementCode)).body[0]
if (rootAst.type !== 'ExpressionStatement') {
throw err(`Expect CallExpression, got ${rootAst.type}`)
}
const ast = rootAst.expression
if (ast.type !== 'CallExpression') {
throw err(`Expect CallExpression, got ${ast.type}`)
}
if (ast.arguments.length < 1 || ast.arguments.length > 2)
throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`)

const arg1 = ast.arguments[0] as ArrayExpression | Literal | TemplateLiteral
const arg2 = ast.arguments[1] as Node | undefined
const arg2 = ast.arguments[1] as RollupAstNode<Node> | undefined

const globs: string[] = []

Expand Down Expand Up @@ -321,14 +296,12 @@ export async function parseImportGlob(
)

options = parseGlobOptions(
code.slice(arg2.range![0], arg2.range![1]),
arg2.range![0],
code.slice(start + arg2.start, start + arg2.end),
start + arg2.start,
logger,
)
}

const end = ast.range![1]

const globsResolved = await Promise.all(
globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId)),
)
Expand All @@ -348,6 +321,34 @@ export async function parseImportGlob(
return (await Promise.all(tasks)).filter(Boolean)
}

function findCorrespondingCloseParenthesisPosition(
cleanCode: string,
openPos: number,
) {
const closePos = cleanCode.indexOf(')', openPos)
if (closePos < 0) return -1

if (!cleanCode.slice(openPos, closePos).includes('(')) return closePos

let remainingParensisCount = 0
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
const cleanCodeLen = cleanCode.length
for (let pos = openPos; pos < cleanCodeLen; pos++) {
switch (cleanCode[pos]) {
case '(': {
remainingParensisCount++
break
}
case ')': {
remainingParensisCount--
if (remainingParensisCount <= 0) {
return pos
}
}
}
}
return -1
}

const importPrefix = '__vite_glob_'

const { basename, dirname, relative, join } = posix
Expand Down
12 changes: 12 additions & 0 deletions patches/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/package.json b/package.json
index 1b8dc76afc3cf5890cc3693c2975577fd3117dd6..9ac3a4d813fda1be476bd896a8f6168b3a459e41 100644
--- a/package.json
+++ b/package.json
@@ -46,5 +46,6 @@
},
"bin": {
"acorn": "./bin/acorn"
- }
+ },
+ "sideEffects": false
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved
Loading