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

feat(parser-adapter-yaml-1-2): use new @tree-sitter-grammars/tree-sitter-yaml grammar #4429

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
7,529 changes: 4,524 additions & 3,005 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
},
"scripts": {
"prebuild": "cd ./node_modules/tree-sitter && node-gyp rebuild && cd ../tree-sitter-json && tree-sitter generate --abi=13 ./grammar.js && tree-sitter build-wasm && node-gyp rebuild && cd ../tree-sitter-yaml && cross-env BABEL_ENV=cjs NODE_OPTIONS='-r core-js/stable @babel/register' tree-sitter generate --abi=13 ./grammar.js && tree-sitter build-wasm && node-gyp rebuild",
"prebuild": "cd ./node_modules/tree-sitter && node-gyp rebuild && cd ../tree-sitter-json && tree-sitter generate ./grammar.js && tree-sitter build --wasm && node-gyp rebuild && cd ../@tree-sitter-grammars/tree-sitter-yaml && tree-sitter generate ./grammar.js && tree-sitter build --wasm && node-gyp rebuild",
"build": "lerna run build",
"build:es": "lerna run build:es",
"build:cjs": "lerna run build:cjs",
Expand Down Expand Up @@ -50,7 +50,6 @@
"@babel/plugin-transform-runtime": "=7.25.9",
"@babel/preset-env": "=7.26.0",
"@babel/preset-typescript": "=7.26.0",
"@babel/register": "=7.25.9",
"@commitlint/cli": "=19.5.0",
"@commitlint/config-conventional": "=19.5.0",
"@types/benchmark": "^2.1.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ asyncapi: 2.4.0
info:
version: '1.0.0'
title: Something
channels: {}
31 changes: 29 additions & 2 deletions packages/apidom-ls/test/openapi-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ describe('apidom-ls-yaml', function () {
];
assert.deepEqual(result, expected as Diagnostic[]);
});

it('test validate invalid YAML indent', async function () {
const validationContext: ValidationContext = {
comments: DiagnosticSeverity.Error,
Expand All @@ -871,8 +872,8 @@ describe('apidom-ls-yaml', function () {
{
range: {
start: {
line: 3,
character: 4,
line: 2,
character: 18,
},
end: {
line: 3,
Expand All @@ -884,6 +885,32 @@ describe('apidom-ls-yaml', function () {
code: 0,
source: 'syntax',
},
{
range: {
start: {
line: 1,
character: 0,
},
end: {
line: 1,
character: 4,
},
},
message: "should always have a 'title'",
severity: 1,
code: 70101,
source: 'apilint',
data: {
quickFix: [
{
message: "add 'title' field",
action: 'addChild',
snippetYaml: 'title: \n ',
snippetJson: '"title": "",\n ',
},
],
},
},
];
assert.deepEqual(result, expected as Diagnostic[]);
});
Expand Down
12 changes: 6 additions & 6 deletions packages/apidom-parser-adapter-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"build:es": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'",
"build:cjs": "npm run build:wasm && npm run build:wasm:copy && BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'",
"build:umd:browser": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress",
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/tree-sitter-json/tree-sitter-json.wasm && exit 0 || cd ../../node_modules/tree-sitter-json && tree-sitter generate --abi=13 ./grammar.js && tree-sitter build-wasm && node-gyp rebuild",
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/tree-sitter-json/tree-sitter-json.wasm && exit 0 || cd ../../node_modules/tree-sitter-json && tree-sitter generate ./grammar.js && tree-sitter build --wasm && node-gyp rebuild",
"build:wasm:copy": "copyfiles -u 4 ../../node_modules/tree-sitter-json/tree-sitter-json.wasm wasm",
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix",
"clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types",
"typescript:check-types": "tsc --noEmit",
"typescript:declaration": "tsc -p declaration.tsconfig.json && rollup -c config/rollup/types.dist.js",
"test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha",
"test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test NODE_NO_WARNINGS=1 mocha",
"perf": "cross-env BABEL_ENV=es babel ./test/perf/index.ts --out-file ./test/perf/index.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/index.mjs",
"perf:lexical-analysis": "cross-env BABEL_ENV=es babel ./test/perf/lexical-analysis.ts --out-file ./test/perf/lexical-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/lexical-analysis.mjs",
"perf:parse-syntactic-analysis-direct": "cross-env BABEL_ENV=es babel ./test/perf/parse-syntactic-analysis-direct.ts --out-file ./test/perf/parse-syntactic-analysis-direct.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/parse-syntactic-analysis-direct.mjs",
Expand All @@ -59,12 +59,12 @@
"@types/ramda": "~0.30.0",
"ramda": "~0.30.0",
"ramda-adjunct": "^5.0.0",
"tree-sitter": "=0.20.4",
"tree-sitter-json": "=0.20.2",
"web-tree-sitter": "=0.20.3"
"tree-sitter": "=0.21.1",
"tree-sitter-json": "=0.23.0",
"web-tree-sitter": "=0.24.3"
},
"devDependencies": {
"tree-sitter-cli": "=0.20.4"
"tree-sitter-cli": "^0.24.3"
},
"files": [
"src/**/*.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,12 @@ class TreeCursorSyntaxNode {
}

setFieldName(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentFieldName === 'function') {
this.fieldName = cursor.currentFieldName();
} else {
this.fieldName = cursor.currentFieldName;
}
this.fieldName = cursor.currentFieldName;
return this;
}

setHasError(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentNode === 'function') {
this.hasError = cursor.currentNode().hasError();
} else {
this.hasError = cursor.currentNode.hasError();
}
this.hasError = cursor.currentNode.hasError;
return this;
}

Expand Down
14 changes: 7 additions & 7 deletions packages/apidom-parser-adapter-yaml-1-2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
"build:es": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=es babel src --out-dir src --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward'",
"build:cjs": "npm run build:wasm && npm run build:wasm:copy && BABEL_ENV=cjs babel src --out-dir src --extensions '.ts' --out-file-extension '.cjs' --root-mode 'upward'",
"build:umd:browser": "npm run build:wasm && npm run build:wasm:copy && cross-env BABEL_ENV=browser webpack --config config/webpack/browser.config.js --progress",
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/tree-sitter-yaml/tree-sitter-yaml.wasm && exit 0 || cd ../../node_modules/tree-sitter-yaml && cross-env BABEL_ENV=cjs NODE_OPTIONS='-r core-js/stable @babel/register' tree-sitter generate --abi=13 ./grammar.js && tree-sitter build-wasm && node-gyp rebuild",
"build:wasm:copy": "copyfiles -u 4 ../../node_modules/tree-sitter-yaml/tree-sitter-yaml.wasm wasm",
"build:wasm": "node ../../scripts/file-exists.js ../../node_modules/@tree-sitter-grammars/tree-sitter-yaml/tree-sitter-yaml.wasm && exit 0 || cd ../../node_modules/@tree-sitter-grammars/tree-sitter-yaml && tree-sitter generate ./grammar.js && tree-sitter build --wasm && node-gyp rebuild",
"build:wasm:copy": "copyfiles -u 5 ../../node_modules/@tree-sitter-grammars/tree-sitter-yaml/tree-sitter-yaml.wasm wasm",
"lint": "eslint ./",
"lint:fix": "eslint ./ --fix",
"clean": "rimraf --glob 'src/**/*.mjs' 'src/**/*.cjs' 'test/**/*.mjs' ./dist ./types",
"typescript:check-types": "tsc --noEmit",
"typescript:declaration": "tsc -p declaration.tsconfig.json && rollup -c config/rollup/types.dist.js",
"test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test mocha",
"test": "npm run build:es && cross-env BABEL_ENV=es babel test --out-dir test --extensions '.ts' --out-file-extension '.mjs' --root-mode 'upward' && cross-env NODE_ENV=test NODE_NO_WARNINGS=1 mocha",
"perf": "cross-env BABEL_ENV=es babel ./test/perf/index.ts --out-file ./test/perf/index.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/index.mjs",
"perf:lexical-analysis": "cross-env BABEL_ENV=es babel ./test/perf/lexical-analysis.ts --out-file ./test/perf/lexical-analysis.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/lexical-analysis.mjs",
"perf:parse-syntactic-analysis-indirect": "cross-env BABEL_ENV=es babel ./test/perf/parse-syntactic-analysis-indirect.ts --out-file ./test/perf/parse-syntactic-analysis-indirect.mjs --root-mode 'upward' && cross-env NODE_ENV=test node ./test/perf/parse-syntactic-analysis-indirect.mjs",
Expand All @@ -58,12 +58,12 @@
"@types/ramda": "~0.30.0",
"ramda": "~0.30.0",
"ramda-adjunct": "^5.0.0",
"tree-sitter": "=0.20.4",
"tree-sitter-yaml": "=0.5.0",
"web-tree-sitter": "=0.20.3"
"tree-sitter": "=0.21.1",
"@tree-sitter-grammars/tree-sitter-yaml": "=0.6.1",
"web-tree-sitter": "=0.24.3"
},
"devDependencies": {
"tree-sitter-cli": "=0.20.4"
"tree-sitter-cli": "^0.24.3"
Copy link
Member Author

Choose a reason for hiding this comment

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

Let's pin the tree-sitter-cli to specific version to avoid surprises

},
"files": [
"src/**/*.mjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { lexicalAnalysis, syntacticAnalysis };
export const detect = async (source: string): Promise<boolean> => {
try {
const cst = await lexicalAnalysis(source);
return cst.rootNode.type !== 'ERROR';
return !cst.rootNode.isError;
} catch {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { lexicalAnalysis, syntacticAnalysis };
export const detect = async (source: string): Promise<boolean> => {
try {
const cst = await lexicalAnalysis(source);
return cst.rootNode.type !== 'ERROR';
return !cst.rootNode.isError;
} catch {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Parser, { Tree } from 'tree-sitter';
// @ts-ignore
import YAMLLanguage from 'tree-sitter-yaml';
import YAMLLanguage from '@tree-sitter-grammars/tree-sitter-yaml';

const parser = new Parser();
parser.setLanguage(YAMLLanguage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,12 @@ class TreeCursorSyntaxNode {
}

setFieldName(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentFieldName === 'function') {
this.fieldName = cursor.currentFieldName();
} else {
this.fieldName = cursor.currentFieldName;
}
this.fieldName = cursor.currentFieldName;
return this;
}

setHasError(cursor: NodeTreeCursor | WebTreeCursor) {
if (typeof cursor.currentNode === 'function') {
this.hasError = cursor.currentNode().hasError();
} else {
this.hasError = cursor.currentNode.hasError();
}
this.hasError = cursor.currentNode.hasError;
return this;
}

Expand Down
14 changes: 12 additions & 2 deletions packages/apidom-parser-adapter-yaml-1-2/test/adapter-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ describe('adapter-browser', function () {

context('given non YAML 1.2', function () {
specify('should detect proper media type', async function () {
assert.isFalse(await adapter.detect('test : test : test'));
assert.isFalse(
await adapter.detect(`
!!Invalid yaml:
"some: key" :
- "no quotes here: value"
- list without separator
another_key: "value" other_key: value
[no_key]
:another_invalid_struct!
`),
);
});
});

Expand Down Expand Up @@ -130,7 +140,7 @@ describe('adapter-browser', function () {
`;
const parseResult = await adapter.parse(syntaxErrorSpec, { sourceMap: true });

assert.isFalse(parseResult.isEmpty);
assert.isTrue(parseResult.isEmpty);
assert.strictEqual(toValue(parseResult.errors.get(0)), '(Error YAML syntax error)');
});
});
Expand Down
14 changes: 12 additions & 2 deletions packages/apidom-parser-adapter-yaml-1-2/test/adapter-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ describe('adapter-node', function () {

context('given non YAML 1.2', function () {
specify('should detect proper media type', async function () {
assert.isFalse(await adapter.detect('test : test : test'));
assert.isFalse(
await adapter.detect(`
!!Invalid yaml:
"some: key" :
- "no quotes here: value"
- list without separator
another_key: "value" other_key: value
[no_key]
:another_invalid_struct!
`),
);
});
});

Expand Down Expand Up @@ -115,7 +125,7 @@ describe('adapter-node', function () {
`;
const parseResult = await adapter.parse(syntaxErrorSpec, { sourceMap: true });

assert.isFalse(parseResult.isEmpty);
assert.isTrue(parseResult.isEmpty);
assert.strictEqual(toValue(parseResult.errors.get(0)), '(Error YAML syntax error)');
});
});
Expand Down