This is an automated email from the ASF dual-hosted git repository. randall pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-annotator.git
commit 1320153ef3192d20f193cc5c9386f8bd6ee06a75 Author: Randall Leeds <[email protected]> AuthorDate: Wed Jul 22 23:13:24 2020 -0700 Remove JSON module resolution in TypeScript Until JSON modules are not experimental in Node, avoid using them to avoid creating broken ESM packages. --- .eslintrc.js | 5 +---- test/data-model.test.ts | 42 ++++++++++++++++++++++++------------------ tsconfig.json | 1 - 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 8e628eb..f8e4f1e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -127,10 +127,7 @@ module.exports = { 'import/no-internal-modules': [ 'error', { - allow: [ - 'ajv/lib/refs/json-schema-draft-04.json', - path.resolve(__dirname, './packages/*/src/**'), - ], + allow: [path.resolve(__dirname, './packages/*/src/**')], }, ], 'import/no-relative-parent-imports': 'off', diff --git a/test/data-model.test.ts b/test/data-model.test.ts index c52f4e1..b5661a5 100644 --- a/test/data-model.test.ts +++ b/test/data-model.test.ts @@ -24,7 +24,6 @@ import fs from 'fs'; import { URL } from 'url'; import Ajv from 'ajv'; -import META_SCHEMA from 'ajv/lib/refs/json-schema-draft-04.json'; import { assert } from 'chai'; import fetch from 'node-fetch'; import resolve from 'resolve'; @@ -45,11 +44,10 @@ process.argv.forEach((val, index) => { } }); -function readSchema(schemaPath: string, base: string = 'web-annotation-tests/'): any { - const resolverOptions = { extensions: ['.json', '.test'] }; - const resolvedPath = resolve.sync(`${base}${schemaPath}`, resolverOptions); - const schemaUnparsed = fs.readFileSync(resolvedPath); - return JSON.parse(schemaUnparsed.toString()); +function requireJSON(name: string): Record<string, unknown> { + const resolvedPath = resolve.sync(name); + const data = fs.readFileSync(resolvedPath).toString(); + return JSON.parse(data) as Record<string, unknown>; } const DEFINITIONS = [ @@ -60,40 +58,48 @@ const DEFINITIONS = [ 'id', 'otherProperties', 'specificResource', -].map(name => readSchema(`definitions/${name}`)); +].map(name => requireJSON(`web-annotation-tests/definitions/${name}.json`)); -const MUSTS = readSchema('annotations/annotationMusts'); +const MUSTS = requireJSON( + 'web-annotation-tests/annotations/annotationMusts.test', +); -const ajv = new Ajv({ schemaId: 'auto' }); +const META_SCHEMA = requireJSON('ajv/lib/refs/json-schema-draft-04.json'); + +const ajv = new Ajv({ schemaId: 'auto', meta: false }); ajv.addMetaSchema(META_SCHEMA); DEFINITIONS.forEach(schema => ajv.addSchema(schema)); describe('Test JSON against Schemas', () => { - let data = ''; + let data: Record<string, unknown>; before(async function() { if (!found_url) { this.skip(); } else { // load the data from the file or URL - let url_parsed = new URL(url); + const url_parsed = new URL(url); if (url_parsed.pathname !== url_parsed.href) { const data_response = await fetch(url_parsed.href); - data = await data_response.json(); + data = (await data_response.json()) as Record<string, unknown>; } else { // assume we have a local file and use that - data = JSON.parse(fs.readFileSync(url_parsed.pathname, 'utf8')); + data = JSON.parse( + fs.readFileSync(url_parsed.pathname, 'utf8'), + ) as Record<string, unknown>; } - if (data === '') { + + if (!data) { this.skip(); } } }); - MUSTS.assertions.forEach((schemaPath: string) => { - const schema = readSchema(schemaPath); - it(schema.title, () => { - let valid = ajv.validate(schema, data); + const assertions = MUSTS.assertions as [string]; + assertions.forEach((schemaPath: string) => { + const schema = requireJSON(`web-annotation-tests/${schemaPath}`); + it(schema.title as string, () => { + const valid = ajv.validate(schema, data); assert.isOk(valid, ajv.errorsText()); }); }); diff --git a/tsconfig.json b/tsconfig.json index cdd2af5..f37c6c5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,6 @@ ], "moduleResolution": "node", "noEmit": true, - "resolveJsonModule": true, "paths": { "@annotator/*": ["packages/*/src"] },
