Skip to content

Commit

Permalink
feat: use new @tree-sitter-grammars/tree-sitter-yaml grammar
Browse files Browse the repository at this point in the history
Refs #4033
  • Loading branch information
char0n committed Oct 30, 2024
1 parent 1f90787 commit 3aac09a
Show file tree
Hide file tree
Showing 10 changed files with 15,077 additions and 28,013 deletions.
43,006 changes: 15,033 additions & 27,973 deletions package-lock.json

Large diffs are not rendered by default.

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"
},
"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

0 comments on commit 3aac09a

Please sign in to comment.