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

chore: bump dependencies - yaml v2 #291

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .eslintrc.json
Expand Up @@ -15,7 +15,6 @@
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-constant-condition": ["error", { "checkLoops": false }],
"import/extensions": ["error", "always"]
"no-constant-condition": ["error", { "checkLoops": false }]
}
}
17 changes: 8 additions & 9 deletions jest.config.js
Expand Up @@ -2,34 +2,33 @@ export default {
testEnvironment: "node",
moduleFileExtensions: ["ts", "js", "json"],
testMatch: ["**/tests/**/*.ts", "**/*.test.ts"],
extensionsToTreatAsEsm: ['.ts'],
extensionsToTreatAsEsm: [".ts"],
transform: {
'\\.ts$': [
'ts-jest',
"\\.ts$": [
"ts-jest",
{
diagnostics: true,
useESM: true,
},
],
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
"^(\\.{1,2}/.*)\\.js$": "$1",
},
snapshotSerializers: ['jest-snapshot-serializer-raw'],
coverageReporters: ["lcov", "text-summary"],
collectCoverage: true,
collectCoverageFrom: ["src/**/*.ts"],
coveragePathIgnorePatterns: [
"src/index.ts",
"src/helpers.ts",
"src/types.ts"
"src/types.ts",
],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100
}
}
statements: 100,
},
},
};
35 changes: 17 additions & 18 deletions package.json
Expand Up @@ -34,26 +34,25 @@
"release": "yarn build && standard-version"
},
"dependencies": {
"lines-and-columns": "^2.0.3",
"tslib": "^2.4.1",
"yaml": "^1.10.0"
"lines-and-columns": "^2.0.4",
"tslib": "^2.6.2",
"yaml": "^2.3.4"
},
"devDependencies": {
"@types/jest": "29.2.5",
"@types/node": "18.11.18",
"@typescript-eslint/eslint-plugin": "5.48.0",
"@typescript-eslint/parser": "5.48.0",
"eslint": "8.31.0",
"eslint-config-prettier": "8.6.0",
"eslint-plugin-import": "2.26.0",
"jest": "29.3.1",
"jest-snapshot-serializer-raw": "1.2.0",
"npm-run-all": "4.1.5",
"patch-package": "6.5.1",
"prettier": "2.8.2",
"standard-version": "9.5.0",
"ts-jest": "29.0.3",
"typescript": "4.9.4"
"@types/jest": "^29.5.11",
"@types/node": "^20.11.5",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.16.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"jest": "^29.7.0",
"npm-run-all2": "^6.1.1",
"patch-package": "^8.0.0",
"prettier": "^3.2.4",
"standard-version": "^9.5.0",
"ts-jest": "^29.1.2",
"typescript": "^5.3.3"
},
"engines": {
"node": ">= 14"
Expand Down
65 changes: 0 additions & 65 deletions patches/yaml+1.10.0.patch

This file was deleted.

2 changes: 1 addition & 1 deletion prettier.config.cjs
Expand Up @@ -2,5 +2,5 @@

module.exports = {
trailingComma: "all",
arrowParens: "avoid"
arrowParens: "avoid",
};
13 changes: 0 additions & 13 deletions src/factories/error.ts

This file was deleted.

60 changes: 30 additions & 30 deletions src/helpers.ts
@@ -1,15 +1,17 @@
import { wrap } from "jest-snapshot-serializer-raw";
import YAML from "yaml";

import { parse } from "./parse.js";
import {
Anchor,
Comment,
LinePos,
Node,
Position,
Root,
Tag,
YamlUnistNode,
} from "./types.js";
import * as YAML from "./yaml.js";

export type Arrayable<T> = T | T[];

Expand Down Expand Up @@ -265,38 +267,43 @@ function indent(text: string) {

function codeFrameColumns(
text: string,
position: Position,
position: Position | [LinePos, LinePos],
codeFrameMaxHeight = Infinity,
) {
const isLinePos = Array.isArray(position);

const startLine = isLinePos ? position[0].line : position.start.line;
const startColumn = isLinePos ? position[0].col : position.start.column;
const endLine = isLinePos ? position[1].line : position.end.line;
const endColumn = isLinePos ? position[1].col : position.end.column;

const lines = text.split("\n").map(line => `${line}¶`);
const markerLines = lines.map((line, index) => {
if (index < position.start.line - 1 || index > position.end.line - 1) {
if (!position || index < startLine - 1 || index > endLine - 1) {
return "";
}
if (index === position.start.line - 1) {

if (index === startLine - 1) {
return (
" ".repeat(position.start.column - 1) +
(index === position.end.line - 1 &&
position.start.column === position.end.column
" ".repeat(startColumn - 1) +
(index === endLine - 1 && startColumn === endColumn
? "~"
: "^".repeat(
(index === position.end.line - 1
? position.end.column - 1
: line.length) -
(position.start.column - 1),
(index === endLine - 1 ? endColumn - 1 : line.length) -
(startColumn - 1),
))
);
} else if (index === position.end.line - 1) {
return position.end.column === 1 && line === "¶"
? "^"
: "^".repeat(position.end.column - 1);
} else {
return "^".repeat(line.length);
}

if (index === endLine - 1) {
return endColumn === 1 && line === "¶" ? "^" : "^".repeat(endColumn - 1);
}

return "^".repeat(line.length);
});

const start = Math.max(0, position.start.line - 1 - codeFrameMaxHeight);
const end = Math.min(lines.length, position.end.line + codeFrameMaxHeight);
const start = Math.max(0, startLine - 1 - codeFrameMaxHeight);
const end = Math.min(lines.length, endLine + codeFrameMaxHeight);

const gutterWidth = Math.floor(Math.log10(lines.length)) + 1;

Expand All @@ -323,22 +330,15 @@ export function testSyntaxError(text: string, message?: string) {
parse(text);
throw new Error("SyntaxError not found");
} catch (error: any) {
if (!isYAMLError(error)) {
if (!(error instanceof YAML.YAMLError)) {
throw error;
}
test(message || error.message, () => {
expect(
error.message + "\n" + codeFrameColumns(error.source, error.position),
error.message +
"\n" +
codeFrameColumns(error.code, error.linePos as [LinePos, LinePos]),
).toMatchSnapshot();
});
}
}

function isYAMLError(
e: any,
): e is YAML.YAMLSyntaxError | YAML.YAMLSemanticError {
return (
e instanceof Error &&
(e.name === "YAMLSyntaxError" || e.name === "YAMLSemanticError")
);
}
32 changes: 13 additions & 19 deletions src/parse.ts
@@ -1,29 +1,22 @@
import { LinesAndColumns } from "lines-and-columns";
import YAML from "yaml";

import { attachComments } from "./attach.js";
import { createRoot } from "./factories/root.js";
import { removeCstBlankLine } from "./preprocess.js";
import { Context, transformNode } from "./transform.js";
import { transformContent } from "./transforms/content.js";
import { transformError } from "./transforms/error.js";
import { transformOffset } from "./transforms/offset.js";
import { transformRange } from "./transforms/range.js";
import { Comment, Root } from "./types.js";
import { addOrigRange } from "./utils/add-orig-range.js";
import { Comment, Document, Root } from "./types.js";
import { removeFakeNodes } from "./utils/remove-fake-nodes.js";
import { updatePositions } from "./utils/update-positions.js";
import * as YAML from "./yaml.js";

export function parse(text: string): Root {
const cst = YAML.parseCST(text);

addOrigRange(cst);

const documents = cst.map(cstDocument =>
new YAML.Document({
merge: false,
keepCstNodes: true,
}).parse(cstDocument),
);
const documents = YAML.parseAllDocuments(text, {
strict: false,
uniqueKeys: false,
});

const locator = new LinesAndColumns(text);
const comments: Comment[] = [];
Expand All @@ -41,20 +34,21 @@ export function parse(text: string): Root {
for (const document of documents) {
for (const error of document.errors) {
if (
error instanceof YAML.YAMLSemanticError &&
error.message === 'Map keys must be unique; "<<" is repeated'
error instanceof YAML.YAMLParseError &&
error.code === "DUPLICATE_KEY"
) {
continue;
8;
}
throw transformError(error, context);
throw error;
}
}

documents.forEach(document => removeCstBlankLine(document.cstNode!));
documents.forEach(removeCstBlankLine);

const root = createRoot(
context.transformRange({ origStart: 0, origEnd: context.text.length }),
documents.map(context.transformNode),
documents.map(context.transformNode).filter(Boolean) as Document[],
comments,
);

Expand Down