Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/thin-needles-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"yaml-eslint-parser": minor
---

Replace `lodash` to reduce the package installation size
1 change: 0 additions & 1 deletion explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"format": "prettier --write src/"
},
"dependencies": {
"lodash-es": "^4.17.21",
"vue": "^3.5.12"
},
"devDependencies": {
Expand Down
3 changes: 0 additions & 3 deletions explorer/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ export default defineConfig({
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
lodash: fileURLToPath(
new URL("./node_modules/lodash-es", import.meta.url),
),
},
},
});
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"homepage": "https://github.com/ota-meshi/yaml-eslint-parser#readme",
"dependencies": {
"eslint-visitor-keys": "^3.0.0",
"lodash": "^4.17.21",
"yaml": "^2.0.0"
},
"devDependencies": {
Expand All @@ -56,7 +55,6 @@
"@types/benchmark": "^2.1.1",
"@types/eslint": "^9.0.0",
"@types/eslint-visitor-keys": "^3.0.0",
"@types/lodash": "^4.14.167",
"@types/mocha": "^10.0.0",
"@types/node": "^22.0.0",
"@types/semver": "^7.3.10",
Expand Down
5 changes: 3 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Comment, Locations, Position, Range, Token } from "./ast";
import lodash from "lodash";
import type { CST, DocumentOptions } from "yaml";
import { ParseError } from ".";
import { parserOptionsToYAMLOption } from "./options";
import { sortedLastIndex } from "./utils";

export class Context {
public readonly code: string;
Expand Down Expand Up @@ -121,7 +121,8 @@ class LinesAndColumns {
}

public getLocFromIndex(index: number) {
const lineNumber = lodash.sortedLastIndex(this.lineStartIndices, index);
const lineNumber = sortedLastIndex(this.lineStartIndices, index);

return {
line: lineNumber,
column: index - this.lineStartIndices[lineNumber - 1],
Expand Down
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,44 @@ function getTaggedValue(
${tagText} ${text}`).toJSON();
return value;
}

/**
* Find the insertion position (index) of an item in an array with items sorted
* in ascending order; so that `splice(sortedIndex, 0, item)` would result in
* maintaining the array's sort-ness. The array can contain duplicates.
* If the item already exists in the array the index would be of the *last*
* occurrence of the item.
*
* Runs in O(logN) time.
*
* MIT License | Copyright (c) 2018 remeda | https://remedajs.com/
*
* The implementation is copied from remeda package:
* https://github.com/remeda/remeda/blob/878206eb3e8ec1c7f1300b1909b7aa629810c8bb/src/sortedLastIndex.ts
* https://github.com/remeda/remeda/blob/878206eb3e8ec1c7f1300b1909b7aa629810c8bb/src/internal/binarySearchCutoffIndex.ts#L1
*
* @param data - The (ascending) sorted array.
* @param item - The item to insert.
* @returns Insertion index (In the range 0..data.length).
* @signature
* sortedLastIndex(data, item)
* @example
* sortedLastIndex(['a','a','b','c','c'], 'c') // => 5
*/
export function sortedLastIndex<T>(array: readonly T[], item: T): number {
let lowIndex = 0;
let highIndex = array.length;

while (lowIndex < highIndex) {
const pivotIndex = (lowIndex + highIndex) >>> 1;
const pivot = array[pivotIndex];

if (pivot <= item) {
lowIndex = pivotIndex + 1;
} else {
highIndex = pivotIndex;
}
}

return highIndex;
}
Loading