Skip to content

Commit e1ce46a

Browse files
committed
refactor: drop first, last
1 parent 73dcb3e commit e1ce46a

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

src/html/intermediate-tokenizer.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* See LICENSE file in root directory for full license.
55
*/
66
import assert from "assert"
7-
import last from "lodash/last"
87
import type {
98
ErrorCode,
109
HasLocation,
@@ -175,7 +174,7 @@ export class IntermediateTokenizer {
175174
// VExpressionEnd was not found.
176175
// Concatenate the deferred tokens to the committed token.
177176
const start = this.expressionStartToken
178-
const end = last(this.expressionTokens) || start
177+
const end = this.expressionTokens.at(-1) || start
179178
const value = this.expressionTokens.reduce(concat, start.value)
180179
this.expressionStartToken = null
181180
this.expressionTokens = []
@@ -240,7 +239,7 @@ export class IntermediateTokenizer {
240239
if (this.expressionStartToken != null) {
241240
// Defer this token until a VExpressionEnd token or a non-text token appear.
242241
const lastToken =
243-
last(this.expressionTokens) || this.expressionStartToken
242+
this.expressionTokens.at(-1) || this.expressionStartToken
244243
if (lastToken.range[1] === token.range[0]) {
245244
this.expressionTokens.push(token)
246245
return null
@@ -552,7 +551,7 @@ export class IntermediateTokenizer {
552551
}
553552

554553
const start = this.expressionStartToken
555-
const end = last(this.expressionTokens) || start
554+
const end = this.expressionTokens.at(-1) || start
556555

557556
// If it's '{{}}', it's handled as a text.
558557
if (token.range[0] === start.range[1]) {

src/html/parser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* See LICENSE file in root directory for full license.
55
*/
66
import assert from "assert"
7-
import last from "lodash/last"
87
import findLastIndex from "lodash/findLastIndex"
98
import type {
109
ErrorCode,
@@ -160,7 +159,7 @@ function adjustAttributeName(name: string, namespace: Namespace): string {
160159
*/
161160
function propagateEndLocation(node: VDocumentFragment | VElement): void {
162161
const lastChild =
163-
(node.type === "VElement" ? node.endTag : null) || last(node.children)
162+
(node.type === "VElement" ? node.endTag : null) || node.children.at(-1)
164163
if (lastChild != null) {
165164
node.range[1] = lastChild.range[1]
166165
node.loc.end = lastChild.loc.end
@@ -236,7 +235,7 @@ export class Parser {
236235
* Get the current node.
237236
*/
238237
private get currentNode(): VDocumentFragment | VElement {
239-
return last(this.elementStack) || this.document
238+
return this.elementStack.at(-1) || this.document
240239
}
241240

242241
/**

src/script/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* @copyright 2017 Toru Nagashima. All rights reserved.
44
* See LICENSE file in root directory for full license.
55
*/
6-
import first from "lodash/first"
7-
import last from "lodash/last"
86
import sortedIndexBy from "lodash/sortedIndexBy"
97
import type {
108
ESLintArrayExpression,
@@ -521,7 +519,7 @@ function parseFilter(
521519
}
522520
}
523521

524-
const token = last(ast.tokens)!
522+
const token = ast.tokens!.at(-1)!
525523
return throwUnexpectedTokenError(token.value, token)
526524
}
527525

@@ -536,7 +534,7 @@ function parseFilter(
536534

537535
// Update range.
538536
const firstToken = tokens[0]
539-
const lastToken = last(tokens)!
537+
const lastToken = tokens.at(-1)!
540538
expression.range = [firstToken.range[0], lastToken.range[1]]
541539
expression.loc = { start: firstToken.loc.start, end: lastToken.loc.end }
542540

@@ -778,7 +776,7 @@ export function parseExpression(
778776
}
779777

780778
// Update range.
781-
const lastToken = last(ret.tokens)!
779+
const lastToken = ret.tokens.at(-1)!
782780
ret.expression.range[1] = lastToken.range[1]
783781
ret.expression.loc.end = lastToken.loc.end
784782

@@ -933,7 +931,7 @@ function parseVForExpressionForEcmaVersion5(
933931
if (open != null) {
934932
open.value = "("
935933
}
936-
const close = last(parsedAliases.tokens)
934+
const close = parsedAliases.tokens.at(-1)
937935
if (close != null) {
938936
close.value = ")"
939937
}
@@ -977,7 +975,7 @@ function parseVForExpressionForEcmaVersion5(
977975
comments.push(...parsedIterator.comments)
978976
const { right, references } = parsedIterator
979977
const firstToken = tokens[0]
980-
const lastToken = last(tokens) || firstToken
978+
const lastToken = tokens.at(-1) || firstToken
981979
const expression: VForExpression = {
982980
type: "VForExpression",
983981
range: [firstToken.range[0], lastToken.range[1]],
@@ -1136,8 +1134,8 @@ function parseVOnExpressionBody(
11361134
).argument as ESLintFunctionExpression
11371135
const block = functionDecl.body
11381136
const body = block.body
1139-
const firstStatement = first(body)
1140-
const lastStatement = last(body)
1137+
const firstStatement = body[0]
1138+
const lastStatement = body.at(-1)
11411139
const expression: VOnExpression = {
11421140
type: "VOnExpression",
11431141
range: [
@@ -1231,8 +1229,8 @@ export function parseSlotScopeExpression(
12311229
)
12321230
const references = scope.references
12331231
const variables = scope.variables
1234-
const firstParam = first(params)!
1235-
const lastParam = last(params)!
1232+
const firstParam = params[0]
1233+
const lastParam = params.at(-1)!
12361234
const expression: VSlotScopeExpression = {
12371235
type: "VSlotScopeExpression",
12381236
range: [firstParam.range[0], lastParam.range[1]],
@@ -1330,8 +1328,8 @@ export function parseGenericExpression(
13301328
)
13311329
const references = scope.references
13321330
const variables = scope.variables
1333-
const firstParam = first(params)!
1334-
const lastParam = last(params)!
1331+
const firstParam = params[0]
1332+
const lastParam = params.at(-1)!
13351333
const expression: VGenericExpression = {
13361334
type: "VGenericExpression",
13371335
range: [firstParam.range[0], lastParam.range[1]],

test/ast.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
const assert = require("assert")
1313
const fs = require("fs")
1414
const path = require("path")
15-
const lodash = require("lodash")
1615
const parser = require("../src")
1716
const eslint = require("eslint")
1817
const semver = require("semver")
@@ -116,7 +115,7 @@ function validateParent(source, parserOptions) {
116115
ruleContext.sourceCode.parserServices.defineTemplateBodyVisitor({
117116
"*"(node) {
118117
if (stack.length >= 1) {
119-
const parent = lodash.last(stack)
118+
const parent = stack.at(-1)
120119
assert(
121120
node.parent === parent,
122121
`The parent of ${nodeToString(

0 commit comments

Comments
 (0)