From 402f540b7d6f18046f400006641116fc14f33923 Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Sun, 26 Apr 2020 19:08:26 +0200 Subject: [PATCH] Fix incorrect constructor options in sparqljs (#44106) * Fix incorrect constructor options in sparqljs * Include old version of sparqljs typings --- types/sparqljs/index.d.ts | 7 +- types/sparqljs/sparqljs-tests.ts | 8 +- types/sparqljs/v2/index.d.ts | 272 ++++++++++++++++++++++++++++ types/sparqljs/v2/sparqljs-tests.ts | 169 +++++++++++++++++ types/sparqljs/v2/tsconfig.json | 31 ++++ types/sparqljs/v2/tslint.json | 1 + types/yog-ral/index.d.ts | 14 +- types/yog-ral/tsconfig.json | 48 ++--- types/yog-ral/tslint.json | 6 +- types/yog-ral/yog-ral-tests.ts | 146 +++++++-------- 10 files changed, 588 insertions(+), 114 deletions(-) create mode 100644 types/sparqljs/v2/index.d.ts create mode 100644 types/sparqljs/v2/sparqljs-tests.ts create mode 100644 types/sparqljs/v2/tsconfig.json create mode 100644 types/sparqljs/v2/tslint.json diff --git a/types/sparqljs/index.d.ts b/types/sparqljs/index.d.ts index dfc04ccfcd..536dd1547f 100644 --- a/types/sparqljs/index.d.ts +++ b/types/sparqljs/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for sparqljs 2.1 +// Type definitions for sparqljs 3.0 // Project: https://github.com/RubenVerborgh/SPARQL.js // Definitions by: Alexey Morozov +// Ruben Taelman // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.1 export const Parser: { - new ( + new (options?: { prefixes?: { [prefix: string]: string }, baseIRI?: string, - ): SparqlParser; + }): SparqlParser; }; export const Generator: { diff --git a/types/sparqljs/sparqljs-tests.ts b/types/sparqljs/sparqljs-tests.ts index 2fe29b40c8..2d07c1db46 100644 --- a/types/sparqljs/sparqljs-tests.ts +++ b/types/sparqljs/sparqljs-tests.ts @@ -24,10 +24,10 @@ function officialExamples() { } function advancedOptions() { - const parser = new SparqlJs.Parser( - {rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}, - 'http://example.com' - ); + const parser = new SparqlJs.Parser({ + prefixes: {rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}, + baseIRI: 'http://example.com' + }); const generator = new SparqlJs.Generator({allPrefixes: false}); } diff --git a/types/sparqljs/v2/index.d.ts b/types/sparqljs/v2/index.d.ts new file mode 100644 index 0000000000..dfc04ccfcd --- /dev/null +++ b/types/sparqljs/v2/index.d.ts @@ -0,0 +1,272 @@ +// Type definitions for sparqljs 2.1 +// Project: https://github.com/RubenVerborgh/SPARQL.js +// Definitions by: Alexey Morozov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +export const Parser: { + new ( + prefixes?: { [prefix: string]: string }, + baseIRI?: string, + ): SparqlParser; +}; + +export const Generator: { + new (options?: GeneratorOptions): SparqlGenerator; +}; + +export interface GeneratorOptions { + allPrefixes?: boolean; +} + +export interface SparqlParser { + parse(query: string): SparqlQuery; +} + +export interface SparqlGenerator { + stringify(query: SparqlQuery): string; +} + +export type SparqlQuery = Query | Update; + +export type Query = SelectQuery | ConstructQuery | AskQuery | DescribeQuery; + +export interface BaseQuery { + type: 'query'; + base?: string; + prefixes: { [prefix: string]: string; }; + where?: Pattern[]; + values?: ValuePatternRow[]; +} + +export interface SelectQuery extends BaseQuery { + queryType: 'SELECT'; + variables: Variable[] | ['*']; + distinct?: boolean; + from?: { + default: string[]; + named: string[]; + }; + reduced?: boolean; + group?: Grouping[]; + having?: Expression[]; + order?: Ordering[]; + limit?: number; + offset?: number; +} + +export interface Grouping { + expression: Expression; +} + +export interface Ordering { + expression: Expression; + descending?: boolean; +} + +export interface ConstructQuery extends BaseQuery { + queryType: 'CONSTRUCT'; + template?: Triple[]; +} + +export interface AskQuery extends BaseQuery { + queryType: 'ASK'; +} + +export interface DescribeQuery extends BaseQuery { + queryType: 'DESCRIBE'; + variables: Variable[] | ['*']; +} + +export interface Update { + type: 'update'; + prefixes: { [prefix: string]: string; }; + updates: UpdateOperation[]; +} + +export type UpdateOperation = InsertDeleteOperation | ManagementOperation; + +export interface InsertDeleteOperation { + updateType: 'insert' | 'delete' | 'deletewhere' | 'insertdelete'; + graph?: string; + insert?: Quads[]; + delete?: Quads[]; + where?: Pattern[]; +} + +export type Quads = BgpPattern | GraphQuads; + +export type ManagementOperation = + | CopyMoveAddOperation + | LoadOperation + | CreateOperation + | ClearDropOperation; + +export interface CopyMoveAddOperation { + type: 'copy' | 'move' | 'add'; + silent: boolean; + source: GraphOrDefault; + destination: GraphOrDefault; +} + +export interface LoadOperation { + type: 'load'; + silent: boolean; + source: string; + destination: string | false; +} + +export interface CreateOperation { + type: 'create'; + silent: boolean; + graph: string; +} + +export interface ClearDropOperation { + type: 'clear' | 'drop'; + silent: boolean; + graph: GraphReference; +} + +export interface GraphOrDefault { + type: 'graph'; + name?: string; + default?: boolean; +} + +export interface GraphReference extends GraphOrDefault { + named?: boolean; + all?: boolean; +} + +/** + * Examples: '?var', '*', + * SELECT (?a as ?b) ... ==> { expression: '?a', variable: '?b' } + */ +export type Variable = VariableExpression | Term; + +export interface VariableExpression { + expression: Expression; + variable: Term; +} + +export type Pattern = + | BgpPattern + | BlockPattern + | GraphPattern + | ServicePattern + | FilterPattern + | BindPattern + | ValuesPattern + | SelectQuery; + +/** + * Basic Graph Pattern + */ +export interface BgpPattern { + type: 'bgp'; + triples: Triple[]; +} + +export interface GraphQuads { + type: 'graph'; + name: Term; + triples: Triple[]; +} + +export interface BlockPattern { + type: 'optional' | 'union' | 'group' | 'minus' | 'graph' | 'service'; + patterns: Pattern[]; +} + +export interface GroupPattern extends BlockPattern { + type: 'group'; +} + +export interface GraphPattern extends BlockPattern { + type: 'graph'; + name: Term; +} + +export interface ServicePattern extends BlockPattern { + type: 'service'; + name: Term; + silent: boolean; +} + +export interface FilterPattern { + type: 'filter'; + expression: Expression; +} + +export interface BindPattern { + type: 'bind'; + expression: Expression; + variable: Term; +} + +export interface ValuesPattern { + type: 'values'; + values: ValuePatternRow[]; +} + +export interface ValuePatternRow { + [variable: string]: Term; +} + +/** + * Either '?var', 'schema:iri', '_:blankNode', + * '"literal"^^' or '{undefined}'. + * + * Term is a nominal type based on string. + */ +export type Term = string & { __termBrand: string; }; + +export interface Triple { + subject: Term; + predicate: PropertyPath | Term; + object: Term; +} + +export interface PropertyPath { + type: 'path'; + pathType: '|' | '/' | '^' | '+' | '*' | '!'; + items: Array; +} + +export type Expression = + | OperationExpression + | FunctionCallExpression + | AggregateExpression + | BgpPattern + | GroupPattern + | Tuple + | Term; + +// allow Expression circularly reference itself +// tslint:disable-next-line no-empty-interface +export interface Tuple extends Array {} + +export interface BaseExpression { + type: string; + distinct?: boolean; +} + +export interface OperationExpression extends BaseExpression { + type: 'operation'; + operator: string; + args: Expression[]; +} + +export interface FunctionCallExpression extends BaseExpression { + type: 'functionCall'; + function: string; + args: Expression[]; +} + +export interface AggregateExpression extends BaseExpression { + type: 'aggregate'; + expression: Expression; + aggregation: string; + separator?: string; +} diff --git a/types/sparqljs/v2/sparqljs-tests.ts b/types/sparqljs/v2/sparqljs-tests.ts new file mode 100644 index 0000000000..2fe29b40c8 --- /dev/null +++ b/types/sparqljs/v2/sparqljs-tests.ts @@ -0,0 +1,169 @@ +import * as SparqlJs from 'sparqljs'; + +/** + * Examples from the project's README + */ +function officialExamples() { + // Parse a SPARQL query to a JSON object + const SparqlParser = SparqlJs.Parser; + const parser = new SparqlParser(); + const parsedQuery = parser.parse( + 'PREFIX foaf: ' + + 'SELECT * { ?mickey foaf:name "Mickey Mouse"@en; foaf:knows ?other. }', + ); + + // Regenerate a SPARQL query from a JSON object + const SparqlGenerator = SparqlJs.Generator; + const generator = new SparqlGenerator(); + if (parsedQuery.type === 'query' && parsedQuery.queryType === 'SELECT') { + parsedQuery.variables = ['?mickey' as SparqlJs.Term]; + } + + // $ExpectType string + generator.stringify(parsedQuery); +} + +function advancedOptions() { + const parser = new SparqlJs.Parser( + {rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}, + 'http://example.com' + ); + const generator = new SparqlJs.Generator({allPrefixes: false}); +} + +/** + * Basic query structure + */ +function basicQueries() { + const foo = 'example:foo' as SparqlJs.Term; + const bar = 'example:bar' as SparqlJs.Term; + const qux = 'example:qux' as SparqlJs.Term; + + const prefixes = {rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}; + + const bgpPattern: SparqlJs.BgpPattern = { + type: 'bgp', + triples: [ + {subject: foo, predicate: qux, object: bar}, + { + subject: foo, + predicate: { + type: 'path', + pathType: '|', + items: [qux, bar], + }, + object: bar, + } + ], + }; + + const select: SparqlJs.SelectQuery = { + type: 'query', + queryType: 'SELECT', + prefixes, + variables: ['*'], + distinct: true, + from: { + default: ['http://example.com/'], + named: ['http://example.com/foo', 'http://example.com/bar'], + }, + reduced: false, + group: [ + {expression: foo}, + {expression: bar}, + ], + having: [{ + type: 'functionCall', + function: 'isIRI', + args: [foo], + }], + order: [{ + expression: bar, + descending: true, + }], + limit: 100, + offset: 10, + where: [bgpPattern], + values: [ + {x: foo, y: bar}, + {x: foo}, + ] + }; + + const construct: SparqlJs.ConstructQuery = { + type: 'query', + queryType: 'CONSTRUCT', + base: 'http://example.com', + prefixes, + template: bgpPattern.triples, + }; + + const ask: SparqlJs.AskQuery = { + type: 'query', + queryType: 'ASK', + prefixes, + }; + + const describe: SparqlJs.DescribeQuery = { + type: 'query', + queryType: 'DESCRIBE', + prefixes, + variables: [ + foo, + { + variable: bar, + expression: { + type: 'operation', + operator: '+', + args: [foo, bar], + } + } + ], + }; +} + +/** + * Update query structure + */ +function updateQueries() { + const bgp: SparqlJs.BgpPattern = { + type: 'bgp', + triples: [], + }; + + const update: SparqlJs.Update = { + type: 'update', + prefixes: { + rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', + }, + updates: [ + { + updateType: 'insertdelete', + graph: 'http://example.com/foo', + insert: [bgp], + delete: [bgp], + where: [], + }, + { + type: 'copy', + silent: true, + source: { + type: 'graph', + name: 'http://example.com/foo', + }, + destination: { + type: 'graph', + default: true, + }, + }, + { + type: 'clear', + silent: false, + graph: { + type: 'graph', + all: true, + }, + }, + ], + }; +} diff --git a/types/sparqljs/v2/tsconfig.json b/types/sparqljs/v2/tsconfig.json new file mode 100644 index 0000000000..b79be9bafd --- /dev/null +++ b/types/sparqljs/v2/tsconfig.json @@ -0,0 +1,31 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../../", + "typeRoots": [ + "../../" + ], + "types": [], + "paths": { + "sparqljs": [ + "sparqljs/v2" + ], + "sparqljs/*": [ + "sparqljs/v2/*" + ] + }, + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sparqljs-tests.ts" + ] +} \ No newline at end of file diff --git a/types/sparqljs/v2/tslint.json b/types/sparqljs/v2/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sparqljs/v2/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/yog-ral/index.d.ts b/types/yog-ral/index.d.ts index c14e72743e..705410d22b 100644 --- a/types/yog-ral/index.d.ts +++ b/types/yog-ral/index.d.ts @@ -1,7 +1,7 @@ -// Type definitions for yog-ral 0.20 -// Project: https://gitlab.baidu.com/fex/node-ral -// Definitions by: ssddi456 -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.3 - -export * from 'node-ral'; +// Type definitions for yog-ral 0.20 +// Project: https://gitlab.baidu.com/fex/node-ral +// Definitions by: ssddi456 +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +export * from 'node-ral'; diff --git a/types/yog-ral/tsconfig.json b/types/yog-ral/tsconfig.json index dc44644a91..ee4975fa07 100644 --- a/types/yog-ral/tsconfig.json +++ b/types/yog-ral/tsconfig.json @@ -1,24 +1,24 @@ -{ - "compilerOptions": { - "target": "es6", - "module": "commonjs", - "lib": [ - "es6" - ], - "noImplicitAny": true, - "noImplicitThis": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "baseUrl": "../", - "typeRoots": [ - "../" - ], - "types": [], - "noEmit": true, - "forceConsistentCasingInFileNames": true - }, - "files": [ - "index.d.ts", - "yog-ral-tests.ts" - ] -} +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "yog-ral-tests.ts" + ] +} diff --git a/types/yog-ral/tslint.json b/types/yog-ral/tslint.json index 8643075d49..f93cf8562a 100644 --- a/types/yog-ral/tslint.json +++ b/types/yog-ral/tslint.json @@ -1,3 +1,3 @@ -{ - "extends": "dtslint/dt.json" -} +{ + "extends": "dtslint/dt.json" +} diff --git a/types/yog-ral/yog-ral-tests.ts b/types/yog-ral/yog-ral-tests.ts index f41a8e9607..2f5dd9edcd 100644 --- a/types/yog-ral/yog-ral-tests.ts +++ b/types/yog-ral/yog-ral-tests.ts @@ -1,73 +1,73 @@ -import * as yogRal from "yog-ral"; - -class FormConverter extends yogRal.Converter { - pack(config: yogRal.Service, data: {}) { - return new Buffer('123'); - } - unpack(config: yogRal.Service, data: {}) { - return {}; - } - getName() { - return 'form'; - } -} - -class HashringBalance extends yogRal.Balance { - getName() { - return 'hashring'; - } - fetchServer(balanceContext: yogRal.Balance.BalanceContextClass, conf: {}, prevBackend: yogRal.Server): yogRal.Server { - return { - host: '127.0.0.1', - port: 8888, - }; - } -} - -class HttpProtocol extends yogRal.Protocol { - getName() { - return 'http'; - } - _request(config: any, callback: (err: any, data: any) => any) { - callback(new Error(), '123'); - } -} - -class DefaultConfigNormalizer extends yogRal.ConfigNormalizer { - getName() { - return 'default'; - } - needUpdate() { - return false; - } - normalizeConfig(config: any) { - return config; - } -} - -const runner = yogRal.RAL('test', {}); -runner.on('data', () => { - // yeap -}); -runner.doRequest(); - -yogRal.RAL.init(); - -yogRal.RALPromise('test', {}).then; - -yogRal.Config.loadRawConf; -yogRal.Config.load; -yogRal.Config.normalizerManager; -yogRal.Config.normalize; -yogRal.Config.getContext; -yogRal.Config.getConf; -yogRal.Config.clearConf; -yogRal.Config.getConfNames; -yogRal.Config.getRawConf; -yogRal.Config.getUpdateNeededRawConf; -yogRal.Config.enableUpdate; -yogRal.Config.disableUpdate; -yogRal.Config.isAutoUpdateEnabled; - -const logger = yogRal.Logger('some'); -logger.debug('test'); +import * as yogRal from "yog-ral"; + +class FormConverter extends yogRal.Converter { + pack(config: yogRal.Service, data: {}) { + return new Buffer('123'); + } + unpack(config: yogRal.Service, data: {}) { + return {}; + } + getName() { + return 'form'; + } +} + +class HashringBalance extends yogRal.Balance { + getName() { + return 'hashring'; + } + fetchServer(balanceContext: yogRal.Balance.BalanceContextClass, conf: {}, prevBackend: yogRal.Server): yogRal.Server { + return { + host: '127.0.0.1', + port: 8888, + }; + } +} + +class HttpProtocol extends yogRal.Protocol { + getName() { + return 'http'; + } + _request(config: any, callback: (err: any, data: any) => any) { + callback(new Error(), '123'); + } +} + +class DefaultConfigNormalizer extends yogRal.ConfigNormalizer { + getName() { + return 'default'; + } + needUpdate() { + return false; + } + normalizeConfig(config: any) { + return config; + } +} + +const runner = yogRal.RAL('test', {}); +runner.on('data', () => { + // yeap +}); +runner.doRequest(); + +yogRal.RAL.init(); + +yogRal.RALPromise('test', {}).then; + +yogRal.Config.loadRawConf; +yogRal.Config.load; +yogRal.Config.normalizerManager; +yogRal.Config.normalize; +yogRal.Config.getContext; +yogRal.Config.getConf; +yogRal.Config.clearConf; +yogRal.Config.getConfNames; +yogRal.Config.getRawConf; +yogRal.Config.getUpdateNeededRawConf; +yogRal.Config.enableUpdate; +yogRal.Config.disableUpdate; +yogRal.Config.isAutoUpdateEnabled; + +const logger = yogRal.Logger('some'); +logger.debug('test');