Skip to content

refactor: drop lodash #268

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

Merged
merged 6 commits into from
Jun 26, 2025
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
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"eslint-visitor-keys": "^4.2.0",
"espree": "^10.3.0",
"esquery": "^1.6.0",
"lodash": "^4.17.21",
"semver": "^7.6.3"
},
"devDependencies": {
Expand All @@ -31,7 +30,6 @@
"@eslint/js": "^9.19.0",
"@types/debug": "^4.1.7",
"@types/estree": "^1.0.0",
"@types/lodash": "^4.14.186",
"@types/mocha": "^9.0.0",
"@types/node": "^18.8.4",
"@types/semver": "^7.3.12",
Expand Down Expand Up @@ -84,7 +82,7 @@
"watch:tsc": "tsc --module es2015 --watch",
"watch:rollup": "wait-on .temp/index.js && rollup -c -o index.js --watch",
"watch:test": "wait-on index.js && warun index.js \"test/*.js\" \"test/fixtures/ast/*/*.json\" \"test/fixtures/*\" --debounce 1000 --no-initial -- nyc mocha \"test/*.js\" --reporter dot --timeout 10000",
"watch:update-ast": "wait-on index.js && warun index.js \"test/fixtures/ast/*/*.vue\" -- node scripts/update-fixtures-ast.js",
"watch:update-ast": "wait-on index.js && warun index.js \"test/fixtures/ast/*/*.vue\" -- ts-node scripts/update-fixtures-ast.js",
"watch:coverage-report": "wait-on coverage/lcov-report/index.html && opener coverage/lcov-report/index.html"
},
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import replace from "rollup-plugin-replace"

const pkg = require("./package.json")
const deps = new Set(
["assert", "events", "path"].concat(Object.keys(pkg.dependencies))
["assert", "events", "path"].concat(Object.keys(pkg.dependencies)),
)

export default {
Expand All @@ -31,5 +31,5 @@ export default {
"process.env.PACKAGE_VERSION": `"${pkg.version}"`,
}),
],
external: id => deps.has(id) || id.startsWith("lodash"),
external: (id) => deps.has(id),
}
2 changes: 1 addition & 1 deletion src/common/error-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ParseError, VDocumentFragment } from "../ast/index"
import sortedIndexBy from "lodash/sortedIndexBy"
import { sortedIndexBy } from "../utils/utils"
/**
* Insert the given error.
* @param document The document that the node is belonging to.
Expand Down
2 changes: 1 addition & 1 deletion src/common/lines-and-columns.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sortedLastIndex from "lodash/sortedLastIndex"
import { sortedLastIndex } from "../utils/utils"
import type { Location } from "../ast/index"
import type { LocationCalculator } from "./location-calculator"
/**
Expand Down
2 changes: 1 addition & 1 deletion src/common/location-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import sortedLastIndex from "lodash/sortedLastIndex"
import { sortedLastIndex } from "../utils/utils"
import type { Location } from "../ast/index"
import { LinesAndColumns } from "./lines-and-columns"

Expand Down
3 changes: 1 addition & 2 deletions src/common/token-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sortedIndexBy from "lodash/sortedIndexBy"
import sortedLastIndexBy from "lodash/sortedLastIndexBy"
import { sortedIndexBy, sortedLastIndexBy } from "../utils/utils"
import type { LocationRange, Token, VDocumentFragment } from "../ast/index"
import type { LinesAndColumns } from "./lines-and-columns"

Expand Down
5 changes: 2 additions & 3 deletions src/external/node-event-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import type EventEmitter from "events"
import type { ESQueryOptions, Selector } from "esquery"
import esquery from "esquery"
import union from "lodash/union"
import intersection from "lodash/intersection"
import memoize from "lodash/memoize"
import { memoize } from "../utils/memoize"
import { union, intersection } from "../utils/utils"
import type { Node } from "../ast/index"

interface NodeSelector {
Expand Down
2 changes: 1 addition & 1 deletion src/external/token-store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @fileoverview Define utilify functions for token store.
* @author Toru Nagashima
*/
import sortedIndexBy from "lodash/sortedIndexBy"
import { sortedIndexBy } from "../../utils/utils"
import type { HasLocation } from "../../ast/index"

/**
Expand Down
7 changes: 3 additions & 4 deletions src/html/intermediate-tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* See LICENSE file in root directory for full license.
*/
import assert from "assert"
import last from "lodash/last"
import type {
ErrorCode,
HasLocation,
Expand Down Expand Up @@ -175,7 +174,7 @@ export class IntermediateTokenizer {
// VExpressionEnd was not found.
// Concatenate the deferred tokens to the committed token.
const start = this.expressionStartToken
const end = last(this.expressionTokens) || start
const end = this.expressionTokens.at(-1) || start
const value = this.expressionTokens.reduce(concat, start.value)
this.expressionStartToken = null
this.expressionTokens = []
Expand Down Expand Up @@ -240,7 +239,7 @@ export class IntermediateTokenizer {
if (this.expressionStartToken != null) {
// Defer this token until a VExpressionEnd token or a non-text token appear.
const lastToken =
last(this.expressionTokens) || this.expressionStartToken
this.expressionTokens.at(-1) || this.expressionStartToken
if (lastToken.range[1] === token.range[0]) {
this.expressionTokens.push(token)
return null
Expand Down Expand Up @@ -552,7 +551,7 @@ export class IntermediateTokenizer {
}

const start = this.expressionStartToken
const end = last(this.expressionTokens) || start
const end = this.expressionTokens.at(-1) || start

// If it's '{{}}', it's handled as a text.
if (token.range[0] === start.range[1]) {
Expand Down
12 changes: 4 additions & 8 deletions src/html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
* See LICENSE file in root directory for full license.
*/
import assert from "assert"
import last from "lodash/last"
import findLastIndex from "lodash/findLastIndex"
import type {
ErrorCode,
HasLocation,
Expand Down Expand Up @@ -52,8 +50,7 @@ import {
getScriptParser,
getParserLangFromSFC,
} from "../common/parser-options"
import sortedIndexBy from "lodash/sortedIndexBy"
import sortedLastIndexBy from "lodash/sortedLastIndexBy"
import { sortedIndexBy, sortedLastIndexBy } from "../utils/utils"
import type {
CustomTemplateTokenizer,
CustomTemplateTokenizerConstructor,
Expand Down Expand Up @@ -160,7 +157,7 @@ function adjustAttributeName(name: string, namespace: Namespace): string {
*/
function propagateEndLocation(node: VDocumentFragment | VElement): void {
const lastChild =
(node.type === "VElement" ? node.endTag : null) || last(node.children)
(node.type === "VElement" ? node.endTag : null) || node.children.at(-1)
if (lastChild != null) {
node.range[1] = lastChild.range[1]
node.loc.end = lastChild.loc.end
Expand Down Expand Up @@ -236,7 +233,7 @@ export class Parser {
* Get the current node.
*/
private get currentNode(): VDocumentFragment | VElement {
return last(this.elementStack) || this.document
return this.elementStack.at(-1) || this.document
}

/**
Expand Down Expand Up @@ -701,8 +698,7 @@ export class Parser {
protected EndTag(token: EndTag): void {
debug("[html] EndTag %j", token)

const i = findLastIndex(
this.elementStack,
const i = this.elementStack.findLastIndex(
(el) => el.name.toLowerCase() === token.name,
)
if (i === -1) {
Expand Down
26 changes: 12 additions & 14 deletions src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* @copyright 2017 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
import first from "lodash/first"
import last from "lodash/last"
import sortedIndexBy from "lodash/sortedIndexBy"
import { sortedIndexBy } from "../utils/utils"
import type {
ESLintArrayExpression,
ESLintArrayPattern,
Expand Down Expand Up @@ -521,7 +519,7 @@ function parseFilter(
}
}

const token = last(ast.tokens)!
const token = ast.tokens!.at(-1)!
return throwUnexpectedTokenError(token.value, token)
}

Expand All @@ -536,7 +534,7 @@ function parseFilter(

// Update range.
const firstToken = tokens[0]
const lastToken = last(tokens)!
const lastToken = tokens.at(-1)!
expression.range = [firstToken.range[0], lastToken.range[1]]
expression.loc = { start: firstToken.loc.start, end: lastToken.loc.end }

Expand Down Expand Up @@ -778,7 +776,7 @@ export function parseExpression(
}

// Update range.
const lastToken = last(ret.tokens)!
const lastToken = ret.tokens.at(-1)!
ret.expression.range[1] = lastToken.range[1]
ret.expression.loc.end = lastToken.loc.end

Expand Down Expand Up @@ -933,7 +931,7 @@ function parseVForExpressionForEcmaVersion5(
if (open != null) {
open.value = "("
}
const close = last(parsedAliases.tokens)
const close = parsedAliases.tokens.at(-1)
if (close != null) {
close.value = ")"
}
Expand Down Expand Up @@ -977,7 +975,7 @@ function parseVForExpressionForEcmaVersion5(
comments.push(...parsedIterator.comments)
const { right, references } = parsedIterator
const firstToken = tokens[0]
const lastToken = last(tokens) || firstToken
const lastToken = tokens.at(-1) || firstToken
const expression: VForExpression = {
type: "VForExpression",
range: [firstToken.range[0], lastToken.range[1]],
Expand Down Expand Up @@ -1136,8 +1134,8 @@ function parseVOnExpressionBody(
).argument as ESLintFunctionExpression
const block = functionDecl.body
const body = block.body
const firstStatement = first(body)
const lastStatement = last(body)
const firstStatement = body[0]
const lastStatement = body.at(-1)
const expression: VOnExpression = {
type: "VOnExpression",
range: [
Expand Down Expand Up @@ -1231,8 +1229,8 @@ export function parseSlotScopeExpression(
)
const references = scope.references
const variables = scope.variables
const firstParam = first(params)!
const lastParam = last(params)!
const firstParam = params[0]
const lastParam = params.at(-1)!
const expression: VSlotScopeExpression = {
type: "VSlotScopeExpression",
range: [firstParam.range[0], lastParam.range[1]],
Expand Down Expand Up @@ -1330,8 +1328,8 @@ export function parseGenericExpression(
)
const references = scope.references
const variables = scope.variables
const firstParam = first(params)!
const lastParam = last(params)!
const firstParam = params[0]
const lastParam = params.at(-1)!
const expression: VGenericExpression = {
type: "VGenericExpression",
range: [firstParam.range[0], lastParam.range[1]],
Expand Down
Loading