From f18ba003c3f6590be6bee9726b3fd3874cd7a210 Mon Sep 17 00:00:00 2001 From: TokugawaT_YD <41653501+TokugawaTakesi@users.noreply.github.com> Date: Thu, 15 Aug 2019 06:35:43 +0900 Subject: [PATCH] Types for Html-validator (#37607) * draft * clear to tests * Clear for tests --- types/html-validator/html-validator-tests.ts | 37 ++++++++ types/html-validator/index.d.ts | 91 ++++++++++++++++++++ types/html-validator/tsconfig.json | 23 +++++ types/html-validator/tslint.json | 3 + 4 files changed, 154 insertions(+) create mode 100644 types/html-validator/html-validator-tests.ts create mode 100644 types/html-validator/index.d.ts create mode 100644 types/html-validator/tsconfig.json create mode 100644 types/html-validator/tslint.json diff --git a/types/html-validator/html-validator-tests.ts b/types/html-validator/html-validator-tests.ts new file mode 100644 index 0000000000..28c52e3f24 --- /dev/null +++ b/types/html-validator/html-validator-tests.ts @@ -0,0 +1,37 @@ +import validateHtml = require('html-validator'); + +const testHtml = ` + + + + + Invalid + + +

I'm baaaaaaaaaaaad code

+ + +`; + +validateHtml({ + data: testHtml, + format: 'json' +}).then((validationResults: validateHtml.ParsedJsonAsValidationResults) => { + if (validationResults.messages.length === 0) { + console.warn(`File "test" contains W3C standard violations or guidelines neglects.`); + return; + } + + validationResults.messages.forEach((violation: validateHtml.ValidationMessageObject) => { + if (violation.type === 'error') { + console.log(`W3C standard violation: ${violation.message}`); + } + + if (violation.type === 'info') { + console.warn(`W3C guidelines neglect: ${violation.message}`); + } + + console.log(violation.extract); + console.log(`line: ${violation.lastLine}, column: ${violation.firstColumn}-${violation.lastColumn}\n`); + }); +}); diff --git a/types/html-validator/index.d.ts b/types/html-validator/index.d.ts new file mode 100644 index 0000000000..d180c4db52 --- /dev/null +++ b/types/html-validator/index.d.ts @@ -0,0 +1,91 @@ +// Type definitions for html-validator 4.1 +// Project: https://github.com/zrrrzzt/html-validator +// Definitions by: Takesi Tokugawa +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +/// + +declare function HtmlValidator( + options: + HtmlValidator.OptionsForHtmlFileAsValidationTargetAndObjectAsResult | + HtmlValidator.OptionsForExternalUrlAsValidationTargetAndObjectAsResult +): Promise; + +declare function HtmlValidator( + options: + HtmlValidator.OptionsForHtmlFileAsValidationTargetAndTextAsResults | + HtmlValidator.OptionsForExternalUrlAsValidationTargetAndTextAsResults +): Promise; + +declare namespace HtmlValidator { + interface BasicOptions { + validator?: object; + ignore?: string | string[]; + isLocal?: boolean; + isFragment?: boolean; + } + + interface OptionsForHtmlFileAsValidationTarget extends BasicOptions { + data: string; + } + + interface OptionsForExternalUrlAsValidationTarget extends BasicOptions { + url: string; + } + + // Could be used to avoid string literals + enum ValidationResultsOutputFormats { + json = 'json', + html = 'html', + xhtml = 'xhtml', + xml = 'xml', + gnu = 'gnu', + text = 'text' + } + + interface OptionsForHtmlFileAsValidationTargetAndObjectAsResult extends OptionsForHtmlFileAsValidationTarget { + format?: 'json'; + } + + interface OptionsForHtmlFileAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget { + format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text'; + } + + interface OptionsForExternalUrlAsValidationTargetAndObjectAsResult extends OptionsForExternalUrlAsValidationTarget { + format?: 'json'; + } + + interface OptionsForExternalUrlAsValidationTargetAndTextAsResults extends OptionsForHtmlFileAsValidationTarget { + format: 'html' | 'xhtml' | 'xml' | 'gnu' | 'text'; + } + + interface ParsedJsonAsValidationResults { + messages: ValidationMessageObject[]; + } + + // Could be used to avoid string literals + enum ValidationMessageTypes { + error = 'error', + info = 'info' + } + + // Could be used to avoid string literals + enum ValidationMessageSubTypes { + warning = 'warning' + } + + interface ValidationMessageObject { + type: 'error' | 'info'; + subType?: 'warning'; + lastLine: number; + firstColumn: number; + lastColumn: number; + hiliteStart: number; + hiliteLength: number; + message: string; + extract: string; + } +} + +export = HtmlValidator; diff --git a/types/html-validator/tsconfig.json b/types/html-validator/tsconfig.json new file mode 100644 index 0000000000..e8daf0dd86 --- /dev/null +++ b/types/html-validator/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "html-validator-tests.ts" + ] +} diff --git a/types/html-validator/tslint.json b/types/html-validator/tslint.json new file mode 100644 index 0000000000..30a1bdde2e --- /dev/null +++ b/types/html-validator/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} \ No newline at end of file