From 6b39501af79036c76148e4062c21cad406511e08 Mon Sep 17 00:00:00 2001 From: Titus Date: Tue, 1 Jun 2021 10:37:25 +0200 Subject: [PATCH 01/10] Use `pull_request_target` in bb --- .github/workflows/bb.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/bb.yml b/.github/workflows/bb.yml index 291ab09..0198fc3 100644 --- a/.github/workflows/bb.yml +++ b/.github/workflows/bb.yml @@ -2,7 +2,7 @@ name: bb on: issues: types: [opened, reopened, edited, closed, labeled, unlabeled] - pull_request: + pull_request_target: types: [opened, reopened, edited, closed, labeled, unlabeled] jobs: main: From 9e7e52e3a2cb868414611599f0e294f08d83f249 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 18 Jul 2021 22:11:30 +0200 Subject: [PATCH 02/10] Use `@ts-expect-error` --- index.js | 6 +++--- test.js | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 2d408a0..19cdd28 100644 --- a/index.js +++ b/index.js @@ -21,19 +21,19 @@ export function stringifyPosition(value) { // Node. if (own.call(value, 'position') || own.call(value, 'type')) { - // @ts-ignore looks like a node. + // @ts-expect-error looks like a node. return position(value.position) } // Position. if (own.call(value, 'start') || own.call(value, 'end')) { - // @ts-ignore looks like a position. + // @ts-expect-error looks like a position. return position(value) } // Point. if (own.call(value, 'line') || own.call(value, 'column')) { - // @ts-ignore looks like a point. + // @ts-expect-error looks like a point. return point(value) } diff --git a/test.js b/test.js index 531fcf2..0e6d42b 100644 --- a/test.js +++ b/test.js @@ -13,19 +13,19 @@ test('stringifyPosition', function (t) { 'should return empty `string` with `null`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition('foo'), '', 'should return empty `string` with `string`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition(5), '', 'should return empty `string` with `number`' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({}), '', 'should return empty `string` with `{}`' @@ -38,7 +38,7 @@ test('stringifyPosition', function (t) { ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({type: 'text', position: 3}), '1:1-1:1', 'should return a range for `node` with invalid `position` #1' @@ -47,7 +47,7 @@ test('stringifyPosition', function (t) { t.equal( stringifyPosition({ type: 'text', - // @ts-ignore runtime. + // @ts-expect-error runtime. position: {start: {}, end: {}} }), '1:1-1:1', @@ -85,14 +85,14 @@ test('stringifyPosition', function (t) { ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({start: 3, end: 6}), '1:1-1:1', 'should return a range for `position` with invalid `point`s #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({start: {}, end: {}}), '1:1-1:1', 'should return range for `position` with invalid `point`s #1' @@ -123,21 +123,21 @@ test('stringifyPosition', function (t) { ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({line: 'foo', column: 'bar'}), '1:1', 'should return a point for a `point` with invalid indices #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({line: 4}), '4:1', 'should return a point for a partially valid `point` #1' ) t.equal( - // @ts-ignore runtime. + // @ts-expect-error runtime. stringifyPosition({column: 12}), '1:12', 'should return a point for a partially valid `point` #1' From 1d62acf552069ebc1718ea9ec33a40e48f5023dc Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sun, 18 Jul 2021 22:12:43 +0200 Subject: [PATCH 03/10] Update `xo` --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3ecb5b5..03c2276 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "tape": "^5.0.0", "type-coverage": "^2.0.0", "typescript": "^4.0.0", - "xo": "^0.38.0" + "xo": "^0.42.0" }, "scripts": { "prepack": "npm run build && npm run format", From a10182a865a3db65f903909e79fcc7ee348996f6 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:14:05 +0100 Subject: [PATCH 04/10] Update dev-dependencies --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 03c2276..c887697 100644 --- a/package.json +++ b/package.json @@ -40,13 +40,13 @@ "@types/tape": "^4.0.0", "c8": "^7.0.0", "prettier": "^2.0.0", - "remark-cli": "^9.0.0", - "remark-preset-wooorm": "^8.0.0", + "remark-cli": "^10.0.0", + "remark-preset-wooorm": "^9.0.0", "rimraf": "^3.0.0", "tape": "^5.0.0", "type-coverage": "^2.0.0", "typescript": "^4.0.0", - "xo": "^0.42.0" + "xo": "^0.48.0" }, "scripts": { "prepack": "npm run build && npm run format", From b9b6cc5d19cc562fb8b9477a869a9dee1f84690b Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:17:30 +0100 Subject: [PATCH 05/10] Fix types to support any unist node --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 19cdd28..054f9db 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ var own = {}.hasOwnProperty /** - * @typedef {import('unist').Node} Node + * @typedef {Record & {type: string, position?: Position|undefined}} NodeLike * @typedef {import('unist').Position} Position * @typedef {import('unist').Point} Point */ @@ -10,7 +10,7 @@ var own = {}.hasOwnProperty * Stringify one point, a position (start and end points), or a node’s * positional information. * - * @param {Node|Position|Point} [value] + * @param {NodeLike|Position|Point} [value] * @returns {string} */ export function stringifyPosition(value) { From e98f8fdd32c0b0e4254c89c7e996509408da3393 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:19:51 +0100 Subject: [PATCH 06/10] Refactor internals so typescript can understand things --- index.js | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 054f9db..a73d621 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,7 @@ -var own = {}.hasOwnProperty - /** - * @typedef {Record & {type: string, position?: Position|undefined}} NodeLike - * @typedef {import('unist').Position} Position * @typedef {import('unist').Point} Point + * @typedef {import('unist').Position} Position + * @typedef {Record & {type: string, position?: Position|undefined}} NodeLike */ /** @@ -20,20 +18,17 @@ export function stringifyPosition(value) { } // Node. - if (own.call(value, 'position') || own.call(value, 'type')) { - // @ts-expect-error looks like a node. + if ('position' in value || 'type' in value) { return position(value.position) } // Position. - if (own.call(value, 'start') || own.call(value, 'end')) { - // @ts-expect-error looks like a position. + if ('start' in value || 'end' in value) { return position(value) } // Point. - if (own.call(value, 'line') || own.call(value, 'column')) { - // @ts-expect-error looks like a point. + if ('line' in value || 'column' in value) { return point(value) } From 199949d809f48fbc699cbead51192f86238bbc8a Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:24:22 +0100 Subject: [PATCH 07/10] Add `strict` to `tsconfig.json` --- index.js | 8 ++++---- test.js | 5 +++++ tsconfig.json | 3 ++- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index a73d621..1b4a320 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ * Stringify one point, a position (start and end points), or a node’s * positional information. * - * @param {NodeLike|Position|Point} [value] + * @param {NodeLike|Position|Point|null} [value] * @returns {string} */ export function stringifyPosition(value) { @@ -37,7 +37,7 @@ export function stringifyPosition(value) { } /** - * @param {Point} point + * @param {Point|undefined} point * @returns {string} */ function point(point) { @@ -45,7 +45,7 @@ function point(point) { } /** - * @param {Position} pos + * @param {Position|undefined} pos * @returns {string} */ function position(pos) { @@ -53,7 +53,7 @@ function position(pos) { } /** - * @param {number} value + * @param {number|undefined} value * @returns {number} */ function index(value) { diff --git a/test.js b/test.js index 0e6d42b..5ae7b4d 100644 --- a/test.js +++ b/test.js @@ -58,7 +58,9 @@ test('stringifyPosition', function (t) { stringifyPosition({ type: 'text', position: { + // @ts-expect-error runtime. start: {line: null, column: null}, + // @ts-expect-error runtime. end: {line: null, column: null} } }), @@ -79,6 +81,7 @@ test('stringifyPosition', function (t) { ) t.equal( + // @ts-expect-error runtime. stringifyPosition({start: null, end: null}), '1:1-1:1', 'should return a range for a `position` without `point`s' @@ -99,6 +102,7 @@ test('stringifyPosition', function (t) { ) t.equal( + // @ts-expect-error runtime. stringifyPosition({ start: {line: null, column: null}, end: {line: null, column: null} @@ -117,6 +121,7 @@ test('stringifyPosition', function (t) { ) t.equal( + // @ts-expect-error runtime. stringifyPosition({line: null, column: null}), '1:1', 'should return a point for a `point` without indices' diff --git a/tsconfig.json b/tsconfig.json index be08abe..e31adf8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ "declaration": true, "emitDeclarationOnly": true, "allowSyntheticDefaultImports": true, - "skipLibCheck": true + "skipLibCheck": true, + "strict": true } } From 62374fbd5a9c53c8ccd02a2bd6a81a034b5d29f8 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:25:15 +0100 Subject: [PATCH 08/10] Refactor --- package.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/package.json b/package.json index c887697..d774dc3 100644 --- a/package.json +++ b/package.json @@ -65,11 +65,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "rules": { - "no-var": "off", - "prefer-arrow-callback": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ From 6ed2510368ff1b2bc602894a2efb1a8767b38860 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:46:59 +0100 Subject: [PATCH 09/10] Add improved docs --- readme.md | 102 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 77 insertions(+), 25 deletions(-) diff --git a/readme.md b/readme.md index 7f6b656..5fe2e1d 100644 --- a/readme.md +++ b/readme.md @@ -8,31 +8,64 @@ [![Backers][backers-badge]][collective] [![Chat][chat-badge]][chat] -[**unist**][unist] utility to pretty print the positional information of a node. +**[unist][]** utility to pretty print the positional information of a node. -## Install +## Contents + +* [What is this?](#what-is-this) +* [When should I use this?](#when-should-i-use-this) +* [Install](#install) +* [Use](#use) +* [API](#api) + * [`stringifyPosition(node|position|point)`](#stringifypositionnodepositionpoint) +* [Types](#types) +* [Compatibility](#compatibility) +* [Security](#security) +* [Related](#related) +* [Contribute](#contribute) +* [License](#license) + +## What is this? + +This package is a utility that takes any [unist][] (whether mdast, hast, etc) +node, position, or point, and serializes its positional info. -This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): -Node 12+ is needed to use it and it must be `import`ed instead of `require`d. +## When should I use this? + +This utility is useful to display where something occurred in the original +document, in one standard way, for humans. +For example, when throwing errors or warning messages about something. + +## Install -[npm][]: +This package is [ESM only][esm]. +In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: ```sh npm install unist-util-stringify-position ``` -## Use +In Deno with [`esm.sh`][esmsh]: ```js -import {stringifyPosition} from 'unist-util-stringify-position' +import {stringifyPosition} from 'https://esm.sh/unist-util-stringify-position@3' +``` + +In browsers with [`esm.sh`][esmsh]: + +```html + +``` -// Point -stringifyPosition({line: 2, column: 3}) // => '2:3' +## Use -// Position -stringifyPosition({start: {line: 2}, end: {line: 3}}) // => '2:1-3:1' +```js +import {stringifyPosition} from 'unist-util-stringify-position' -// Node +stringifyPosition({line: 2, column: 3}) // => '2:3' (point) +stringifyPosition({start: {line: 2}, end: {line: 3}}) // => '2:1-3:1' (position) stringifyPosition({ type: 'text', value: '!', @@ -40,27 +73,26 @@ stringifyPosition({ start: {line: 5, column: 11}, end: {line: 5, column: 12} } -}) // => '5:11-5:12' +}) // => '5:11-5:12' (node) ``` ## API -This package exports the following identifiers: `stringifyPosition`. +This package exports the identifier `stringifyPosition`. There is no default export. ### `stringifyPosition(node|position|point)` -Stringify one [point][], a [position][] (start and end [point][]s), or a node’s -[positional information][positional-information]. +Stringify a [point][], [position][], or a [node][]. ###### Parameters * `node` ([`Node`][node]) - — Node whose `'position'` property to stringify + — node whose `'position'` property to stringify * `position` ([`Position`][position]) - — Position whose `'start'` and `'end'` points to stringify + — position whose `'start'` and `'end'` points to stringify * `point` ([`Point`][point]) - — Point whose `'line'` and `'column'` to stringify + — point whose `'line'` and `'column'` to stringify ###### Returns @@ -70,16 +102,32 @@ Stringify one [point][], a [position][] (start and end [point][]s), or a node’ An empty string (`''`) is returned if the given value is neither `node`, `position`, nor `point`. +## Types + +This package is fully typed with [TypeScript][]. +There are no additional types exported. + +## Compatibility + +Projects maintained by the unified collective are compatible with all maintained +versions of Node.js. +As of now, that is Node.js 12.20+, 14.14+, and 16.0+. +Our projects sometimes work with older versions, but this is not guaranteed. + +## Security + +This project is safe. + ## Related * [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated) - — Check if a node is generated + — check if a node is generated * [`unist-util-position`](https://github.com/syntax-tree/unist-util-position) - — Get positional info of nodes + — get positional info of nodes * [`unist-util-remove-position`](https://github.com/syntax-tree/unist-util-remove-position) - — Remove positional info from trees + — remove positional info from trees * [`unist-util-source`](https://github.com/syntax-tree/unist-util-source) - — Get the source of a value (node or position) in a file + — get the source of a value (node or position) in a file ## Contribute @@ -129,6 +177,12 @@ abide by its terms. [author]: https://wooorm.com +[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c + +[esmsh]: https://esm.sh + +[typescript]: https://www.typescriptlang.org + [contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md [support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md @@ -142,5 +196,3 @@ abide by its terms. [position]: https://github.com/syntax-tree/unist#position [point]: https://github.com/syntax-tree/unist#point - -[positional-information]: https://github.com/syntax-tree/unist#positional-information From fd941e9fcafa64a6e525560fa24699ba19fb883f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 2 Mar 2022 13:49:08 +0100 Subject: [PATCH 10/10] 3.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d774dc3..38ea1f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unist-util-stringify-position", - "version": "3.0.0", + "version": "3.0.1", "description": "unist utility to serialize a node, position, or point as a human readable location", "license": "MIT", "keywords": [