Skip to content

Commit

Permalink
Add Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
jitsedesmet committed Jan 22, 2025
1 parent ab1e0b6 commit 74348b7
Show file tree
Hide file tree
Showing 39 changed files with 759 additions and 364 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ concurrency:
cancel-in-progress: true

jobs:
tooling:
strategy:
fail-fast: false
matrix:
node-version:
- 18.x
- 20.x
- 22.x
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: yarn install --immutable
- run: yarn lint
- run: yarn depcheck

test:
strategy:
fail-fast: false
Expand All @@ -27,7 +46,6 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: yarn install --immutable
- run: yarn build
- run: yarn depcheck
- run: yarn test

spec:
Expand Down
2 changes: 2 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
yarn run build
yarn run lint
yarn run depcheck
yarn run test
yarn run spec:all
4 changes: 2 additions & 2 deletions engines/engine-sparql-1-1-adjust/spec/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {Parser} from '../lib'

import {SparqlContext} from "@traqula/rules-sparql-1-1";

export async function parse(query: string, context: Partial<SparqlContext> = {}) {
const parser = new Parser();
const parser = new Parser();
export function parse(query: string, context: Partial<SparqlContext> = {}) {
parser.parse(query, context);
}
export function query() {
Expand Down
4 changes: 2 additions & 2 deletions engines/engine-sparql-1-1-adjust/test/statics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {beforeEach, describe, it} from "vitest";
import {describe, it} from "vitest";
import {Parser} from "../lib";
import {positiveTest, importSparql11NoteTests} from "@traqula/test-utils";
import {importSparql11NoteTests, positiveTest} from "@traqula/test-utils";
import {DataFactory} from "rdf-data-factory";
import {BaseQuad} from "@rdfjs/types";

Expand Down
4 changes: 1 addition & 3 deletions engines/engine-sparql-1-1/lib/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ const queryOrUpdate: SparqlRuleDef<'queryOrUpdate', Query | Update | Pick<Update

ACTION(() => {
updateResult.base = prologueValues.base ?? updateResult.base;
updateResult.prefixes = prologueValues.prefixes ?
{ ...updateResult.prefixes, ...prologueValues.prefixes } :
updateResult.prefixes;
updateResult.prefixes = { ...updateResult.prefixes, ...prologueValues.prefixes };
});

parsedPrologue = true;
Expand Down
4 changes: 2 additions & 2 deletions engines/engine-sparql-1-1/lib/expressionParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Builder, type RuleNamesFromList} from '@traqula/core';
import {gram, SparqlContext} from '@traqula/rules-sparql-1-1';
import {Builder} from '@traqula/core';
import {gram} from '@traqula/rules-sparql-1-1';


const rulesNoBuiltIn = <const> [
Expand Down
4 changes: 2 additions & 2 deletions engines/engine-sparql-1-1/spec/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import {ErrorSkipped,} from 'rdf-test-suite';
import {Parser} from '../lib'
import {SparqlContext} from "@traqula/rules-sparql-1-1";

export async function parse(query: string, context: Partial<SparqlContext> = {}) {
const parser = new Parser();
const parser = new Parser();
export function parse(query: string, context: Partial<SparqlContext> = {}) {
parser.parse(query, context);
}
export function query() {
Expand Down
1 change: 0 additions & 1 deletion engines/engine-sparql-1-1/test/query.bench.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable test/consistent-test-it */
import { Parser as SparqlJSparser } from 'sparqljs';
import { describe, bench } from 'vitest';
import { Parser as TraqulaParqer } from '../lib/';
Expand Down
4 changes: 2 additions & 2 deletions engines/engine-sparql-1-1/test/statics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {beforeEach, describe, it} from "vitest";
import {describe, it} from "vitest";
import {Parser} from "../lib";
import {positiveTest, importSparql11NoteTests} from "@traqula/test-utils";
import {importSparql11NoteTests, positiveTest} from "@traqula/test-utils";
import {DataFactory} from "rdf-data-factory";
import {BaseQuad} from "@rdfjs/types";

Expand Down
1 change: 1 addition & 0 deletions engines/engine-sparql-1-2/lib/Parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Builder} from '@traqula/core';
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import {gram as g11, SparqlParser, SparqlQuery} from '@traqula/rules-sparql-1-1';
import {gram as S12, lex as l12} from '@traqula/rules-sparql-1-2';
import {sparql11ParserBuilder} from '@traqula/engine-sparql-1-1';
Expand Down
9 changes: 4 additions & 5 deletions engines/engine-sparql-1-2/spec/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { ErrorSkipped, } from 'rdf-test-suite';
import { Parser } from '../lib'
import {ImplArgs} from "@traqula/core";
import {ErrorSkipped,} from 'rdf-test-suite';
import {Parser} from '../lib'
import {SparqlContext} from "@traqula/rules-sparql-1-1";

export async function parse(query: string, context: Partial<SparqlContext> = {}) {
const parser = new Parser();
const parser = new Parser();
export function parse(query: string, context: Partial<SparqlContext> = {}) {
parser.parse(query, context);
}
export function query() {
Expand Down
8 changes: 4 additions & 4 deletions engines/engine-sparql-1-2/test/statics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {beforeEach, describe, it} from "vitest";
import { Parser } from "../lib";
import {positiveTest, importSparql11NoteTests, negativeTest} from "@traqula/test-utils";
import {describe, it} from "vitest";
import {Parser} from "../lib";
import {importSparql11NoteTests, negativeTest, positiveTest} from "@traqula/test-utils";
import {DataFactory} from "rdf-data-factory";
import {BaseQuad} from "@rdfjs/types";

Expand Down Expand Up @@ -38,7 +38,7 @@ describe('a SPARQL 1.2 parser', () => {
}
});

it(`should NOT parse $only thing}`, async ({expect}) => {
it(`should NOT parse $only thing}`, ({expect}) => {
const query = `
PREFIX : <http://example.com/ns#>
Expand Down
19 changes: 19 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import eslint from "@eslint/js";
import tseslint from "typescript-eslint";


export default tseslint.config(
{ ignores: ['**/*.{js,d.ts,cjs}'] },
eslint.configs.recommended,
tseslint.configs.strictTypeChecked,
{
languageOptions: {
parserOptions: {
projectService: {
allowDefaultProject: [ '*.ts' ]
},
tsconfigRootDir: import.meta.dirname,
},
},
},
);
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"build:transpile-spec": "lerna run build:transpile-spec",
"clean": "git clean -dfx",
"depcheck": "lerna exec --concurrency 1 -- depcheck --ignores=\"vitest\"",
"lint": "eslint",
"postinstall": "yarn husky && yarn run build",
"publish": "yarn run build",
"spec:all": "lerna run spec:all --concurrency 1",
Expand All @@ -24,15 +25,18 @@
"version": "manual-git-changelog onversion"
},
"devDependencies": {
"@eslint/js": "^9.18.0",
"@rdfjs/types": "^2.0.0",
"@vitest/coverage-v8": "2.1.8",
"depcheck": "^1.4.7",
"esbuild": "^0.24.0",
"eslint": "^9.18.0",
"husky": "^9.1.7",
"lerna": "8.1.4",
"manual-git-changelog": "^1.0.2",
"rdf-test-suite": "^2.0.0",
"typescript": "^5.7.3",
"vitest": "^2.1.8"
"typescript-eslint": "^8.21.0",
"vitest": "^3.0.3"
}
}
10 changes: 5 additions & 5 deletions packages/core/lib/Wildcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ export class Wildcard {
public value = <const> '*';
public termType = <const> 'Wildcard';
public constructor() {
// eslint-disable-next-line no-constructor-return
return WILDCARD ?? this;
return singleton ?? this;
}

public equals(other: { termType: unknown } | undefined | null): boolean {
return Boolean(other && (this.termType === other.termType));
}

public toJSON(): object {
// eslint-disable-next-line unused-imports/no-unused-vars
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { value, ...rest } = this;
return rest;
}
}

let WILDCARD: Wildcard | undefined;
WILDCARD = new Wildcard();
let singleton: Wildcard | undefined = undefined;
singleton = new Wildcard();
1 change: 1 addition & 0 deletions packages/core/lib/grammar-builder/builderTypes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { ParserMethod } from 'chevrotain';
import type {RuleDef} from './ruleDefTypes';

Expand Down
102 changes: 53 additions & 49 deletions packages/core/lib/grammar-builder/parserBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* eslint-disable
@typescript-eslint/no-explicit-any,
@typescript-eslint/no-unsafe-assignment,
@typescript-eslint/no-unsafe-return,
@typescript-eslint/no-unsafe-argument */
import {ILexerConfig, IParserConfig} from '@chevrotain/types';
import type {TokenType, TokenVocabulary} from 'chevrotain';
import {EmbeddedActionsParser, Lexer} from 'chevrotain';
Expand Down Expand Up @@ -59,6 +64,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
Builder<Context, Names, {[Key in Names]: Key extends U ? RuleDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends RuleDef<Context, Key> ? RuleDefs[Key] : never) }> {
const self = <Builder<Context, Names, {[Key in Names]: Key extends U ? RuleDef<Context, Key, RET, ARGS> : (RuleDefs[Key] extends RuleDef<Context, Key> ? RuleDefs[Key] : never) }>>
<unknown> this;

self.rules[patch.name] = <any> patch;
return self;
}
Expand All @@ -70,7 +76,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
Builder<Context, Names | U, {[K in Names | U]: K extends U ? RuleDef<Context, K, RET, ARGS> : ( K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never ) : never) }> {
const self = <Builder<Context, Names | U, {[K in Names | U]: K extends U ? RuleDef<Context, K, RET, ARGS> : ( K extends Names ? (RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never ) : never) }>>
<unknown> this;
const rules = <Record<string, RuleDef<Context>>> self.rules;
const rules = <Record<string, RuleDef<Context> | undefined>> self.rules;
if (rules[rule.name] !== undefined && rules[rule.name] !== rule) {
throw new Error(`Rule ${rule.name} already exists in the builder`);
}
Expand Down Expand Up @@ -108,6 +114,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
*/
public deleteRule<U extends Names>(ruleName: U):
Builder<Context, Exclude<Names, U>, { [K in Exclude<Names, U>]: RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never }> {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
delete this.rules[ruleName];
return <Builder<Context, Exclude<Names, U>, { [K in Exclude<Names, U>]: RuleDefs[K] extends RuleDef<Context, K> ? RuleDefs[K] : never }>>
<unknown> this;
Expand Down Expand Up @@ -136,7 +143,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
: K extends OtherNames ? (OtherRules[K] extends RuleDef<Context, K> ? OtherRules[K] : never) : never
)}> {
// Assume the other grammar is bigger than yours. So start from that one and add this one
const otherRules: Record<string, RuleDef<Context>> = { ...builder.rules };
const otherRules: Record<string, RuleDef<Context> | undefined> = { ...builder.rules };
const myRules: Record<string, RuleDef<Context>> = this.rules;

for (const rule of Object.values(myRules)) {
Expand Down Expand Up @@ -179,6 +186,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
// Start building a parser that does not pass input using a state, but instead gets it as a function argument.
const selfSufficientParser: Partial<ParserFromRules<Context, Names, RuleDefs>> = {};
// To do that, we need to create a wrapper for each parser rule.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
for (const rule of <RuleDef<Context, Names>[]> Object.values(this.rules)) {
selfSufficientParser[rule.name] = <any> ((input: string, context: Context, arg: unknown) => {
// Transform input in accordance to 19.2
Expand Down Expand Up @@ -211,7 +219,7 @@ export class Builder<Context, Names extends string, RuleDefs extends RuleDefMap<
if (parser.errors.length > 0) {
// Console.log(lexResult.tokens);
throw new Error(`Parse error on line ${parser.errors.map(x => x.token.startLine).join(', ')}
${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`);
${parser.errors.map(x => `${String(x.token.startLine ?? '?')}: ${x.message}`).join('\n')}`);
}
return result;
});
Expand Down Expand Up @@ -261,7 +269,7 @@ ${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`);
private getSelfRef(): CstDef {
const subRuleImpl = (chevrotainSubrule: typeof this.SUBRULE): CstDef['SUBRULE'] => {
return ((cstDef, arg) => {
return chevrotainSubrule(<any> this[<keyof (typeof this)> cstDef.name], <any> { ARGS: [this.context, arg] });
return chevrotainSubrule( <any> this[<keyof (typeof this)> cstDef.name], <any> { ARGS: [this.context, arg] });
}) satisfies CstDef['SUBRULE'];
}
return {
Expand Down Expand Up @@ -295,53 +303,49 @@ ${parser.errors.map(x => `${x.token.startLine}: ${x.message}`).join('\n')}`);
OR7: altsOrOpts => this.OR7(altsOrOpts),
OR8: altsOrOpts => this.OR8(altsOrOpts),
OR9: altsOrOpts => this.OR9(altsOrOpts),
MANY: actionORMethodDef => this.MANY(actionORMethodDef),
MANY1: actionORMethodDef => this.MANY1(actionORMethodDef),
MANY2: actionORMethodDef => this.MANY2(actionORMethodDef),
MANY3: actionORMethodDef => this.MANY3(actionORMethodDef),
MANY4: actionORMethodDef => this.MANY4(actionORMethodDef),
MANY5: actionORMethodDef => this.MANY5(actionORMethodDef),
MANY6: actionORMethodDef => this.MANY6(actionORMethodDef),
MANY7: actionORMethodDef => this.MANY7(actionORMethodDef),
MANY8: actionORMethodDef => this.MANY8(actionORMethodDef),
MANY9: actionORMethodDef => this.MANY9(actionORMethodDef),
MANY_SEP: options => this.MANY_SEP(options),
MANY_SEP1: options => this.MANY_SEP1(options),
MANY_SEP2: options => this.MANY_SEP2(options),
MANY_SEP3: options => this.MANY_SEP3(options),
MANY_SEP4: options => this.MANY_SEP4(options),
MANY_SEP5: options => this.MANY_SEP5(options),
MANY_SEP6: options => this.MANY_SEP6(options),
MANY_SEP7: options => this.MANY_SEP7(options),
MANY_SEP8: options => this.MANY_SEP8(options),
MANY_SEP9: options => this.MANY_SEP9(options),
AT_LEAST_ONE: actionORMethodDef => this.AT_LEAST_ONE(actionORMethodDef),
AT_LEAST_ONE1: actionORMethodDef => this.AT_LEAST_ONE1(actionORMethodDef),
AT_LEAST_ONE2: actionORMethodDef => this.AT_LEAST_ONE2(actionORMethodDef),
AT_LEAST_ONE3: actionORMethodDef => this.AT_LEAST_ONE3(actionORMethodDef),
AT_LEAST_ONE4: actionORMethodDef => this.AT_LEAST_ONE4(actionORMethodDef),
AT_LEAST_ONE5: actionORMethodDef => this.AT_LEAST_ONE5(actionORMethodDef),
AT_LEAST_ONE6: actionORMethodDef => this.AT_LEAST_ONE6(actionORMethodDef),
AT_LEAST_ONE7: actionORMethodDef => this.AT_LEAST_ONE7(actionORMethodDef),
AT_LEAST_ONE8: actionORMethodDef => this.AT_LEAST_ONE8(actionORMethodDef),
AT_LEAST_ONE9: actionORMethodDef => this.AT_LEAST_ONE9(actionORMethodDef),
AT_LEAST_ONE_SEP: options => this.AT_LEAST_ONE_SEP(options),
AT_LEAST_ONE_SEP1: options => this.AT_LEAST_ONE_SEP1(options),
AT_LEAST_ONE_SEP2: options => this.AT_LEAST_ONE_SEP2(options),
AT_LEAST_ONE_SEP3: options => this.AT_LEAST_ONE_SEP3(options),
AT_LEAST_ONE_SEP4: options => this.AT_LEAST_ONE_SEP4(options),
AT_LEAST_ONE_SEP5: options => this.AT_LEAST_ONE_SEP5(options),
AT_LEAST_ONE_SEP6: options => this.AT_LEAST_ONE_SEP6(options),
AT_LEAST_ONE_SEP7: options => this.AT_LEAST_ONE_SEP7(options),
AT_LEAST_ONE_SEP8: options => this.AT_LEAST_ONE_SEP8(options),
AT_LEAST_ONE_SEP9: options => this.AT_LEAST_ONE_SEP9(options),
MANY: actionORMethodDef => { this.MANY(actionORMethodDef); },
MANY1: actionORMethodDef => { this.MANY1(actionORMethodDef); },
MANY2: actionORMethodDef => { this.MANY2(actionORMethodDef); },
MANY3: actionORMethodDef => { this.MANY3(actionORMethodDef); },
MANY4: actionORMethodDef => { this.MANY4(actionORMethodDef); },
MANY5: actionORMethodDef => { this.MANY5(actionORMethodDef); },
MANY6: actionORMethodDef => { this.MANY6(actionORMethodDef); },
MANY7: actionORMethodDef => { this.MANY7(actionORMethodDef); },
MANY8: actionORMethodDef => { this.MANY8(actionORMethodDef); },
MANY9: actionORMethodDef => { this.MANY9(actionORMethodDef); },
MANY_SEP: options => { this.MANY_SEP(options); },
MANY_SEP1: options => { this.MANY_SEP1(options); },
MANY_SEP2: options => { this.MANY_SEP2(options); },
MANY_SEP3: options => { this.MANY_SEP3(options); },
MANY_SEP4: options => { this.MANY_SEP4(options); },
MANY_SEP5: options => { this.MANY_SEP5(options); },
MANY_SEP6: options => { this.MANY_SEP6(options); },
MANY_SEP7: options => { this.MANY_SEP7(options); },
MANY_SEP8: options => { this.MANY_SEP8(options); },
MANY_SEP9: options => { this.MANY_SEP9(options); },
AT_LEAST_ONE: actionORMethodDef => { this.AT_LEAST_ONE(actionORMethodDef); },
AT_LEAST_ONE1: actionORMethodDef => { this.AT_LEAST_ONE1(actionORMethodDef); },
AT_LEAST_ONE2: actionORMethodDef => { this.AT_LEAST_ONE2(actionORMethodDef); },
AT_LEAST_ONE3: actionORMethodDef => { this.AT_LEAST_ONE3(actionORMethodDef); },
AT_LEAST_ONE4: actionORMethodDef => { this.AT_LEAST_ONE4(actionORMethodDef); },
AT_LEAST_ONE5: actionORMethodDef => { this.AT_LEAST_ONE5(actionORMethodDef); },
AT_LEAST_ONE6: actionORMethodDef => { this.AT_LEAST_ONE6(actionORMethodDef); },
AT_LEAST_ONE7: actionORMethodDef => { this.AT_LEAST_ONE7(actionORMethodDef); },
AT_LEAST_ONE8: actionORMethodDef => { this.AT_LEAST_ONE8(actionORMethodDef); },
AT_LEAST_ONE9: actionORMethodDef => { this.AT_LEAST_ONE9(actionORMethodDef); },
AT_LEAST_ONE_SEP: options => { this.AT_LEAST_ONE_SEP(options); },
AT_LEAST_ONE_SEP1: options => { this.AT_LEAST_ONE_SEP1(options); },
AT_LEAST_ONE_SEP2: options => { this.AT_LEAST_ONE_SEP2(options); },
AT_LEAST_ONE_SEP3: options => { this.AT_LEAST_ONE_SEP3(options); },
AT_LEAST_ONE_SEP4: options => { this.AT_LEAST_ONE_SEP4(options); },
AT_LEAST_ONE_SEP5: options => { this.AT_LEAST_ONE_SEP5(options); },
AT_LEAST_ONE_SEP6: options => { this.AT_LEAST_ONE_SEP6(options); },
AT_LEAST_ONE_SEP7: options => { this.AT_LEAST_ONE_SEP7(options); },
AT_LEAST_ONE_SEP8: options => { this.AT_LEAST_ONE_SEP8(options); },
AT_LEAST_ONE_SEP9: options => { this.AT_LEAST_ONE_SEP9(options); },
ACTION: func => this.ACTION(func),
BACKTRACK: (cstDef, ...args) => {
try {
return this.BACKTRACK(<any> this[<keyof (typeof this)> cstDef.name], <any> { ARGS: args });
} catch (error: unknown) {
throw error;
}
return this.BACKTRACK(<any> this[<keyof (typeof this)> cstDef.name], <any> { ARGS: args });
},
SUBRULE: subRuleImpl((rule, args) => this.SUBRULE(rule, args)),
SUBRULE1: subRuleImpl((rule, args) => this.SUBRULE1(rule, args)),
Expand Down
2 changes: 2 additions & 0 deletions packages/core/lib/grammar-builder/ruleDefTypes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type {
AtLeastOneSepMethodOpts,
DSLMethodOpts,
DSLMethodOptsWithErr,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
EmbeddedActionsParser,
GrammarAction,
IOrAlt,
Expand Down
Loading

0 comments on commit 74348b7

Please sign in to comment.