From 5505d038a00e1647696451cf47eae5366c0a615c Mon Sep 17 00:00:00 2001 From: TaylorBeeston Date: Fri, 10 Jul 2020 18:04:07 -0700 Subject: [PATCH 1/8] Add types --- package.json | 7 ++++++- types/hastscript-tests.ts | 5 +++++ types/index.d.ts | 11 +++++++++++ types/tsconfig.json | 10 ++++++++++ types/tslint.json | 7 +++++++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 types/hastscript-tests.ts create mode 100644 types/index.d.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json diff --git a/package.json b/package.json index f2528b9..bc95fc0 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,9 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], + "types": "types/index.d.ts", "files": [ + "types/index.d.ts", "index.js", "factory.js", "html.js", @@ -41,7 +43,9 @@ "space-separated-tokens": "^1.0.0" }, "devDependencies": { + "@types/hast": "^2.3.1", "browserify": "^16.0.0", + "dtslint": "^3.6.12", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^8.0.0", @@ -59,7 +63,8 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test": "npm run generate && npm run format && npm run build && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run generate && npm run format && npm run build && npm run test-coverage && npm run test-types" }, "nyc": { "check-coverage": true, diff --git a/types/hastscript-tests.ts b/types/hastscript-tests.ts new file mode 100644 index 0000000..e29f479 --- /dev/null +++ b/types/hastscript-tests.ts @@ -0,0 +1,5 @@ +import h = require('hastscript') + +h() +h('.bar', {class: 'bar'}) +h('.foo', {class: 'bar'}, h('.baz')) diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..d101c37 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,11 @@ +// TypeScript Version: 3.5 + +import {Element, Properties, Node} from 'hast' + +declare function hastscript( + selector?: string, + properties?: Properties, + children?: string | Node | Array +): Element + +export = hastscript diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..80eb6ff --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "hastscript": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..70c4494 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "semicolon": false, + "whitespace": false + } +} From 10dae3bffd68693b272445e04e4876564fa4da73 Mon Sep 17 00:00:00 2001 From: Taylor Beeston <39720479+TaylorBeeston@users.noreply.github.com> Date: Sat, 11 Jul 2020 09:13:26 -0700 Subject: [PATCH 2/8] Add TSDoc annotations Co-authored-by: Christian Murphy --- types/index.d.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index d101c37..578ac4a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2,6 +2,13 @@ import {Element, Properties, Node} from 'hast' +/** + * DSL to create virtual hast trees for HTML or SVG + * + * @param selector Simple CSS selector + * @param properties Map of properties + * @param children (Lists of) child nodes + */ declare function hastscript( selector?: string, properties?: Properties, From 4311b4f76de33ea61ba68632b7fbca2c0e9ca7f5 Mon Sep 17 00:00:00 2001 From: Taylor Beeston <39720479+TaylorBeeston@users.noreply.github.com> Date: Sat, 11 Jul 2020 09:14:31 -0700 Subject: [PATCH 3/8] Add additional type test cases Co-authored-by: Christian Murphy --- types/hastscript-tests.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/types/hastscript-tests.ts b/types/hastscript-tests.ts index e29f479..1af8029 100644 --- a/types/hastscript-tests.ts +++ b/types/hastscript-tests.ts @@ -1,5 +1,11 @@ import h = require('hastscript') -h() -h('.bar', {class: 'bar'}) -h('.foo', {class: 'bar'}, h('.baz')) +h() // $ExpectType Element +h('.bar', {class: 'bar'}) // $ExpectType Element +h('.bar', 'child text') // $ExpectType Element +h('.bar', ['child text']) // $ExpectType Element +h('.foo', {class: 'bar'}, h('.baz')) // $ExpectType Element +h('.foo', {class: 'bar'}, [h('.baz')]) // $ExpectType Element +h('.bar',{class: 'bar'} , 'child text') // $ExpectType Element +h('.bar',{class: 'bar'}, ['child text']) // $ExpectType Element +h(false) // $ExpectError From 6a8818bb7cd73eafa056f6d947d2000e3cc6a600 Mon Sep 17 00:00:00 2001 From: TaylorBeeston Date: Sat, 11 Jul 2020 11:42:15 -0700 Subject: [PATCH 4/8] Add function overload --- types/index.d.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/index.d.ts b/types/index.d.ts index 578ac4a..372623d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -2,6 +2,17 @@ import {Element, Properties, Node} from 'hast' +/** + * DSL to create virtual hast trees for HTML or SVG + * + * @param selector Simple CSS selector + * @param children (Lists of) child nodes + */ +declare function hastscript( + selector?: string, + children?: string | Node | Array +): Element + /** * DSL to create virtual hast trees for HTML or SVG * From 43576aba874ff35f8d084bc082b9b4203cefcc4f Mon Sep 17 00:00:00 2001 From: TaylorBeeston Date: Sat, 11 Jul 2020 11:43:48 -0700 Subject: [PATCH 5/8] Proposal to handle s --- package.json | 3 ++- types/hastscript-tests.ts | 11 +++++++++-- types/svg.d.ts | 5 +++++ types/tsconfig.json | 3 ++- 4 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 types/svg.d.ts diff --git a/package.json b/package.json index bc95fc0..0098126 100644 --- a/package.json +++ b/package.json @@ -27,9 +27,10 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], - "types": "types/index.d.ts", + "types": "types", "files": [ "types/index.d.ts", + "types/svg.d.ts", "index.js", "factory.js", "html.js", diff --git a/types/hastscript-tests.ts b/types/hastscript-tests.ts index 1af8029..863a218 100644 --- a/types/hastscript-tests.ts +++ b/types/hastscript-tests.ts @@ -1,4 +1,5 @@ import h = require('hastscript') +import s = require('hastscript/svg') h() // $ExpectType Element h('.bar', {class: 'bar'}) // $ExpectType Element @@ -6,6 +7,12 @@ h('.bar', 'child text') // $ExpectType Element h('.bar', ['child text']) // $ExpectType Element h('.foo', {class: 'bar'}, h('.baz')) // $ExpectType Element h('.foo', {class: 'bar'}, [h('.baz')]) // $ExpectType Element -h('.bar',{class: 'bar'} , 'child text') // $ExpectType Element -h('.bar',{class: 'bar'}, ['child text']) // $ExpectType Element +h('.bar', {class: 'bar'}, 'child text') // $ExpectType Element +h('.bar', {class: 'bar'}, ['child text']) // $ExpectType Element h(false) // $ExpectError + +// $ExpectType +s('svg', {xmlns: 'http://www.w3.org/2000/svg', viewbox: '0 0 500 500'}, [ + s('title', 'SVG ` Date: Mon, 13 Jul 2020 10:23:35 -0700 Subject: [PATCH 6/8] Move @types/hast to dependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0098126..a985764 100644 --- a/package.json +++ b/package.json @@ -38,13 +38,13 @@ "svg-case-sensitive-tag-names.json" ], "dependencies": { + "@types/hast": "^2.3.1", "comma-separated-tokens": "^1.0.0", "hast-util-parse-selector": "^2.0.0", "property-information": "^5.0.0", "space-separated-tokens": "^1.0.0" }, "devDependencies": { - "@types/hast": "^2.3.1", "browserify": "^16.0.0", "dtslint": "^3.6.12", "nyc": "^15.0.0", From 518c0045e5db43c77bcc834cc090eb2031c71550 Mon Sep 17 00:00:00 2001 From: TaylorBeeston Date: Mon, 13 Jul 2020 10:24:23 -0700 Subject: [PATCH 7/8] Update semver of dtslint to ^3.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a985764..a6ed59b 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ }, "devDependencies": { "browserify": "^16.0.0", - "dtslint": "^3.6.12", + "dtslint": "^3.0.0", "nyc": "^15.0.0", "prettier": "^2.0.0", "remark-cli": "^8.0.0", From aa0b4a50afc4923620f0131f57d58b44c1f55b59 Mon Sep 17 00:00:00 2001 From: TaylorBeeston Date: Mon, 13 Jul 2020 10:31:14 -0700 Subject: [PATCH 8/8] Flatten type defintions --- types/hastscript-tests.ts => hastscript-tests.ts | 0 types/index.d.ts => index.d.ts | 0 package.json | 8 ++++---- types/svg.d.ts => svg.d.ts | 0 types/tsconfig.json => tsconfig.json | 0 types/tslint.json => tslint.json | 0 6 files changed, 4 insertions(+), 4 deletions(-) rename types/hastscript-tests.ts => hastscript-tests.ts (100%) rename types/index.d.ts => index.d.ts (100%) rename types/svg.d.ts => svg.d.ts (100%) rename types/tsconfig.json => tsconfig.json (100%) rename types/tslint.json => tslint.json (100%) diff --git a/types/hastscript-tests.ts b/hastscript-tests.ts similarity index 100% rename from types/hastscript-tests.ts rename to hastscript-tests.ts diff --git a/types/index.d.ts b/index.d.ts similarity index 100% rename from types/index.d.ts rename to index.d.ts diff --git a/package.json b/package.json index a6ed59b..af00b89 100644 --- a/package.json +++ b/package.json @@ -27,10 +27,10 @@ "contributors": [ "Titus Wormer (https://wooorm.com)" ], - "types": "types", + "types": "index.d.ts", "files": [ - "types/index.d.ts", - "types/svg.d.ts", + "index.d.ts", + "svg.d.ts", "index.js", "factory.js", "html.js", @@ -64,7 +64,7 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test.js", - "test-types": "dtslint types", + "test-types": "dtslint .", "test": "npm run generate && npm run format && npm run build && npm run test-coverage && npm run test-types" }, "nyc": { diff --git a/types/svg.d.ts b/svg.d.ts similarity index 100% rename from types/svg.d.ts rename to svg.d.ts diff --git a/types/tsconfig.json b/tsconfig.json similarity index 100% rename from types/tsconfig.json rename to tsconfig.json diff --git a/types/tslint.json b/tslint.json similarity index 100% rename from types/tslint.json rename to tslint.json