From b6283cee18091628a01b3da60a4b4af01e7720eb Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 9 May 2020 14:56:56 +0200 Subject: [PATCH 1/5] Bump prettier to 2.x MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s now applied on the entire workspace. All HTML and markdown files are ignored. --- .prettierignore | 2 ++ package.json | 4 ++-- test/fixtures/attributes/index.json | 5 +---- test/index.js | 10 ++++------ 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/.prettierignore b/.prettierignore index 96b5e24..3666407 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,5 @@ coverage/ hast-util-from-parse5.js hast-util-from-parse5.min.js +*.html +*.md diff --git a/package.json b/package.json index ab6b756..812ee91 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "not": "^0.1.0", "nyc": "^15.0.0", "parse5": "^5.0.0", - "prettier": "^1.0.0", + "prettier": "^2.0.5", "remark-cli": "^7.0.0", "remark-preset-wooorm": "^6.0.0", "tape": "^4.0.0", @@ -49,7 +49,7 @@ "xo": "^0.27.0" }, "scripts": { - "format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", + "format": "remark . -qfo && prettier --write . && xo --fix", "build-bundle": "browserify index.js -s hastUtilFromParse5 > hast-util-from-parse5.js", "build-mangle": "browserify index.js -p tinyify -s hastUtilFromParse5 > hast-util-from-parse5.min.js", "build": "npm run build-bundle && npm run build-mangle", diff --git a/test/fixtures/attributes/index.json b/test/fixtures/attributes/index.json index 14ca186..77a13ef 100644 --- a/test/fixtures/attributes/index.json +++ b/test/fixtures/attributes/index.json @@ -22,10 +22,7 @@ "tagName": "p", "properties": { "id": "foo", - "className": [ - "bar", - "baz" - ], + "className": ["bar", "baz"], "dataQux": "quux" }, "children": [], diff --git a/test/index.js b/test/index.js index 68fb50e..4520d07 100644 --- a/test/index.js +++ b/test/index.js @@ -13,7 +13,7 @@ var fromParse5 = require('..') var join = path.join -test('hast-util-from-parse5', function(t) { +test('hast-util-from-parse5', function (t) { var file = vfile({contents: 'Hello!

World!'}) t.deepEqual( @@ -346,17 +346,15 @@ test('hast-util-from-parse5', function(t) { t.end() }) -test('fixtures', function(t) { +test('fixtures', function (t) { var base = join('test', 'fixtures') - fs.readdirSync(base) - .filter(not(hidden)) - .forEach(each) + fs.readdirSync(base).filter(not(hidden)).forEach(each) t.end() function each(fixture) { - t.test(fixture, function(st) { + t.test(fixture, function (st) { var opts = { file: vfile.readSync(join(base, fixture, 'index.html')), out: join(base, fixture, 'index.json') From fd1334fafa184c2e76ac1e383f4c2ba499a7d5d1 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 9 May 2020 15:21:44 +0200 Subject: [PATCH 2/5] Add TypeScript definitions --- package.json | 13 ++++++++--- types/index.d.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++ types/tests.ts | 11 +++++++++ types/tsconfig.json | 10 +++++++++ types/tslint.json | 8 +++++++ 5 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 types/index.d.ts create mode 100644 types/tests.ts create mode 100644 types/tsconfig.json create mode 100644 types/tslint.json diff --git a/package.json b/package.json index 812ee91..36a0e1c 100644 --- a/package.json +++ b/package.json @@ -24,16 +24,20 @@ "Titus Wormer (https://wooorm.com)" ], "files": [ - "index.js" + "index.js", + "types/index.d.ts" ], + "types": "types/index.d.ts", "dependencies": { "ccount": "^1.0.3", + "dtslint": "^3.5.2", "hastscript": "^5.0.0", "property-information": "^5.0.0", "web-namespaces": "^1.1.2", "xtend": "^4.0.1" }, "devDependencies": { + "@types/parse5": "^5.0.2", "browserify": "^16.0.0", "is-hidden": "^1.0.0", "not": "^0.1.0", @@ -46,6 +50,7 @@ "tinyify": "^2.0.0", "to-vfile": "^6.0.0", "unist-util-visit": "^2.0.0", + "vfile": "^4.1.0", "xo": "^0.27.0" }, "scripts": { @@ -55,7 +60,8 @@ "build": "npm run build-bundle && npm run build-mangle", "test-api": "node test", "test-coverage": "nyc --reporter lcov tape test", - "test": "npm run format && npm run build && npm run test-coverage" + "test-types": "dtslint types", + "test": "npm run format && npm run build && npm run test-coverage && npm run test-types" }, "prettier": { "tabWidth": 2, @@ -72,7 +78,8 @@ "guard-for-in": "off" }, "ignores": [ - "hast-util-from-parse5.js" + "hast-util-from-parse5.js", + "**/*.ts" ] }, "nyc": { diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..8d63192 --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,55 @@ +// TypeScript Version: 3.5 +import {Node} from 'unist' +import {Document} from 'parse5' +import {VFile} from 'vfile' + +declare namespace hastUtilFromParse5 { + interface HastUtilFromParse5Options { + /** + * Whether the [*root*](https://github.com/syntax-tree/unist#root) of the + * [*tree*](https://github.com/syntax-tree/unist#tree) is in the `'html'` or `'svg'` + * space. + * + * If an element in with the SVG namespace is found in `ast`, `fromParse5` + * automatically switches to the SVG space when entering the element, and switches + * back when leaving. + * + * @default 'html' + */ + space?: 'html' | 'svg' + + /** + * [`VFile`](https://github.com/vfile/vfile), used to add + * [positional information](https://github.com/syntax-tree/unist#positional-information) + * to [*nodes*](https://github.com/syntax-tree/hast#nodes). + * If given, the [*file*](https://github.com/syntax-tree/unist#file) should have the + * original HTML source as its contents. + */ + file?: VFile + /** + * + * Whether to add extra positional information about starting tags, closing tags, + * and attributes to elements. + * + * Note: not used without `file`. + * + * @default: false + */ + verbose?: boolean + } +} + +/** + * Transform [Parse5’s AST](https://github.com/inikulin/parse5/blob/master/packages/parse5/docs/tree-adapter/default/interface-list.md) + * to a [**hast**](https://github.com/syntax-tree/hast) + * [*tree*](https://github.com/syntax-tree/unist#tree). + * + * @param options If `options` is a [`VFile`](https://github.com/vfile/vfile), it’s treated + * as `{file: options}`. + */ +declare function hastUtilFromParse5( + ast: Document, + options?: hastUtilFromParse5.HastUtilFromParse5Options | VFile +): Node + +export = hastUtilFromParse5 diff --git a/types/tests.ts b/types/tests.ts new file mode 100644 index 0000000..d108bcf --- /dev/null +++ b/types/tests.ts @@ -0,0 +1,11 @@ +import vfile = require('vfile') +import parse5 = require('parse5') +import fromParse5 = require('hast-util-from-parse5') + +const file = vfile() +const ast = parse5.parse('', {sourceCodeLocationInfo: true}) +fromParse5(ast, file) +fromParse5(ast, {file}) +fromParse5(ast, {space: 'html'}) +fromParse5(ast, {space: 'svg'}) +fromParse5(ast, {verbose: true}) diff --git a/types/tsconfig.json b/types/tsconfig.json new file mode 100644 index 0000000..7c9fbd5 --- /dev/null +++ b/types/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "lib": ["es2015"], + "strict": true, + "baseUrl": ".", + "paths": { + "hast-util-from-parse5": ["index.d.ts"] + } + } +} diff --git a/types/tslint.json b/types/tslint.json new file mode 100644 index 0000000..759caa0 --- /dev/null +++ b/types/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "no-redundant-jsdoc": false, + "semicolon": false, + "whitespace": false + } +} From 34097cde849413db22b7f457c41f24c3ec697040 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 9 May 2020 20:15:45 +0200 Subject: [PATCH 3/5] Move dtslint to devDependencies --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 36a0e1c..871c804 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "types": "types/index.d.ts", "dependencies": { "ccount": "^1.0.3", - "dtslint": "^3.5.2", "hastscript": "^5.0.0", "property-information": "^5.0.0", "web-namespaces": "^1.1.2", @@ -39,6 +38,7 @@ "devDependencies": { "@types/parse5": "^5.0.2", "browserify": "^16.0.0", + "dtslint": "^3.5.2", "is-hidden": "^1.0.0", "not": "^0.1.0", "nyc": "^15.0.0", From b3c3c2b4ee52dc7050abcc0824f71d5eed27a598 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 18 May 2020 22:18:57 +0200 Subject: [PATCH 4/5] Add @types/parse5 and vfile to dependencies The type definitions depend on these types. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 871c804..17f1447 100644 --- a/package.json +++ b/package.json @@ -29,14 +29,15 @@ ], "types": "types/index.d.ts", "dependencies": { + "@types/parse5": "^5.0.2", "ccount": "^1.0.3", "hastscript": "^5.0.0", "property-information": "^5.0.0", + "vfile": "^4.1.0", "web-namespaces": "^1.1.2", "xtend": "^4.0.1" }, "devDependencies": { - "@types/parse5": "^5.0.2", "browserify": "^16.0.0", "dtslint": "^3.5.2", "is-hidden": "^1.0.0", @@ -50,7 +51,6 @@ "tinyify": "^2.0.0", "to-vfile": "^6.0.0", "unist-util-visit": "^2.0.0", - "vfile": "^4.1.0", "xo": "^0.27.0" }, "scripts": { From 906d8423a799dffa013841d24cbd7a89fd246f60 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Wed, 20 May 2020 17:38:46 +0200 Subject: [PATCH 5/5] Ignore JSON files for prettier and revert formatting --- .prettierignore | 1 + test/fixtures/attributes/index.json | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.prettierignore b/.prettierignore index 3666407..5b5b52e 100644 --- a/.prettierignore +++ b/.prettierignore @@ -2,4 +2,5 @@ coverage/ hast-util-from-parse5.js hast-util-from-parse5.min.js *.html +*.json *.md diff --git a/test/fixtures/attributes/index.json b/test/fixtures/attributes/index.json index 77a13ef..14ca186 100644 --- a/test/fixtures/attributes/index.json +++ b/test/fixtures/attributes/index.json @@ -22,7 +22,10 @@ "tagName": "p", "properties": { "id": "foo", - "className": ["bar", "baz"], + "className": [ + "bar", + "baz" + ], "dataQux": "quux" }, "children": [],