vfile
Advanced tools
Comparing version 5.0.2 to 5.1.0
@@ -18,3 +18,3 @@ export class VFile { | ||
*/ | ||
constructor(value?: VFileCompatible) | ||
constructor(value?: VFileCompatible | undefined) | ||
/** | ||
@@ -65,4 +65,13 @@ * Place to store custom information. | ||
/** | ||
* Sometimes files have a source map associated with them. | ||
* This can be stored in the `map` field. | ||
* This should be a `RawSourceMap` type from the `source-map` module. | ||
* @type {unknown} | ||
*/ | ||
map: unknown | ||
/** | ||
* Set full path (`~/index.min.js`). | ||
* Cannot be nullified. | ||
* | ||
* @param {string|URL} path | ||
*/ | ||
@@ -72,2 +81,4 @@ set path(arg: string) | ||
* Access full path (`~/index.min.js`). | ||
* | ||
* @returns {string} | ||
*/ | ||
@@ -79,7 +90,7 @@ get path(): string | ||
*/ | ||
set dirname(arg: string) | ||
set dirname(arg: string | undefined) | ||
/** | ||
* Access parent path (`~`). | ||
*/ | ||
get dirname(): string | ||
get dirname(): string | undefined | ||
/** | ||
@@ -90,7 +101,7 @@ * Set basename (`index.min.js`). | ||
*/ | ||
set basename(arg: string) | ||
set basename(arg: string | undefined) | ||
/** | ||
* Access basename (including extname) (`index.min.js`). | ||
*/ | ||
get basename(): string | ||
get basename(): string | undefined | ||
/** | ||
@@ -100,7 +111,7 @@ * Set extname (including dot) (`.js`). | ||
*/ | ||
set extname(arg: string) | ||
set extname(arg: string | undefined) | ||
/** | ||
* Access extname (including dot) (`.js`). | ||
*/ | ||
get extname(): string | ||
get extname(): string | undefined | ||
/** | ||
@@ -110,7 +121,7 @@ * Set stem (w/o extname) (`index.min`). | ||
*/ | ||
set stem(arg: string) | ||
set stem(arg: string | undefined) | ||
/** | ||
* Access stem (w/o extname) (`index.min`). | ||
*/ | ||
get stem(): string | ||
get stem(): string | undefined | ||
/** | ||
@@ -122,3 +133,3 @@ * Serialize the file. | ||
*/ | ||
toString(encoding?: BufferEncoding): string | ||
toString(encoding?: BufferEncoding | undefined): string | ||
/** | ||
@@ -134,4 +145,8 @@ * Create a message and associates it w/ the file. | ||
reason: string | Error, | ||
place?: Node | Position | Point, | ||
origin?: string | ||
place?: | ||
| import('unist').Node<import('unist').Data> | ||
| import('unist').Position | ||
| import('unist').Point | ||
| undefined, | ||
origin?: string | undefined | ||
): VFileMessage | ||
@@ -150,4 +165,8 @@ /** | ||
reason: string | Error, | ||
place?: Node | Position | Point, | ||
origin?: string | ||
place?: | ||
| import('unist').Node<import('unist').Data> | ||
| import('unist').Position | ||
| import('unist').Point | ||
| undefined, | ||
origin?: string | undefined | ||
): VFileMessage | ||
@@ -167,4 +186,8 @@ /** | ||
reason: string | Error, | ||
place?: Node | Position | Point, | ||
origin?: string | ||
place?: | ||
| import('unist').Node<import('unist').Data> | ||
| import('unist').Position | ||
| import('unist').Point | ||
| undefined, | ||
origin?: string | undefined | ||
): never | ||
@@ -175,2 +198,3 @@ } | ||
export type Point = import('unist').Point | ||
export type URL = import('./minurl.shared.js').URL | ||
/** | ||
@@ -203,18 +227,19 @@ * Encodings supported by the buffer class. | ||
/** | ||
* Things that can be | ||
* passed to the constructor. | ||
* Things that can be passed to the constructor. | ||
*/ | ||
export type VFileCompatible = VFileValue | VFileOptions | VFile | ||
export type VFileCompatible = VFileValue | VFileOptions | VFile | URL | ||
export type VFileCoreOptions = { | ||
value?: VFileValue | ||
cwd?: string | ||
history?: Array<string> | ||
path?: string | ||
basename?: string | ||
stem?: string | ||
extname?: string | ||
dirname?: string | ||
data?: { | ||
[x: string]: unknown | ||
} | ||
value?: VFileValue | undefined | ||
cwd?: string | undefined | ||
history?: string[] | undefined | ||
path?: string | import('./minurl.shared.js').URL | undefined | ||
basename?: string | undefined | ||
stem?: string | undefined | ||
extname?: string | undefined | ||
dirname?: string | undefined | ||
data?: | ||
| { | ||
[x: string]: unknown | ||
} | ||
| undefined | ||
} | ||
@@ -221,0 +246,0 @@ /** |
@@ -5,2 +5,3 @@ /** | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('./minurl.shared.js').URL} URL | ||
* | ||
@@ -13,3 +14,4 @@ * @typedef {'ascii'|'utf8'|'utf-8'|'utf16le'|'ucs2'|'ucs-2'|'base64'|'latin1'|'binary'|'hex'} BufferEncoding | ||
* | ||
* @typedef {string|Uint8Array} VFileValue Contents of the file. | ||
* @typedef {string|Uint8Array} VFileValue | ||
* Contents of the file. | ||
* Can either be text, or a Buffer like structure. | ||
@@ -21,4 +23,4 @@ * This does not directly use type `Buffer`, because it can also be used in a | ||
* | ||
* @typedef {VFileValue|VFileOptions|VFile} VFileCompatible Things that can be | ||
* passed to the constructor. | ||
* @typedef {VFileValue|VFileOptions|VFile|URL} VFileCompatible | ||
* Things that can be passed to the constructor. | ||
* | ||
@@ -29,3 +31,3 @@ * @typedef VFileCoreOptions | ||
* @property {Array.<string>} [history] | ||
* @property {string} [path] | ||
* @property {string|URL} [path] | ||
* @property {string} [basename] | ||
@@ -46,5 +48,6 @@ * @property {string} [stem] | ||
import buffer from 'is-buffer' | ||
import {VFileMessage} from 'vfile-message' | ||
import {path} from './minpath.js' | ||
import {proc} from './minproc.js' | ||
import {VFileMessage} from 'vfile-message' | ||
import {urlToPath, isUrl} from './minurl.js' | ||
@@ -54,3 +57,3 @@ // Order of setting (least specific to most), we need this because otherwise | ||
// stem can be set. | ||
var order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname'] | ||
const order = ['history', 'path', 'basename', 'stem', 'extname', 'dirname'] | ||
@@ -75,7 +78,4 @@ export class VFile { | ||
constructor(value) { | ||
var index = -1 | ||
/** @type {VFileOptions} */ | ||
var options | ||
/** @type {string} */ | ||
var prop | ||
let options | ||
@@ -85,6 +85,8 @@ if (!value) { | ||
} else if (typeof value === 'string' || buffer(value)) { | ||
// @ts-ignore Looks like a buffer. | ||
// @ts-expect-error Looks like a buffer. | ||
options = {value} | ||
} else if (isUrl(value)) { | ||
options = {path: value} | ||
} else { | ||
// @ts-ignore Looks like file or options. | ||
// @ts-expect-error Looks like file or options. | ||
options = value | ||
@@ -145,7 +147,17 @@ } | ||
this.result | ||
/** | ||
* Sometimes files have a source map associated with them. | ||
* This can be stored in the `map` field. | ||
* This should be a `RawSourceMap` type from the `source-map` module. | ||
* @type {unknown} | ||
*/ | ||
this.map | ||
/* eslint-enable no-unused-expressions */ | ||
// Set path related properties in the correct order. | ||
let index = -1 | ||
while (++index < order.length) { | ||
prop = order[index] | ||
const prop = order[index] | ||
@@ -155,11 +167,14 @@ // Note: we specifically use `in` instead of `hasOwnProperty` to accept | ||
if (prop in options && options[prop] !== undefined) { | ||
this[prop] = prop === 'history' ? options[prop].concat() : options[prop] | ||
// @ts-expect-error: TS is confused by the different types for `history`. | ||
this[prop] = prop === 'history' ? [...options[prop]] : options[prop] | ||
} | ||
} | ||
/** @type {string} */ | ||
let prop | ||
// Set non-path related properties. | ||
for (prop in options) { | ||
if (!order.includes(prop)) { | ||
this[prop] = options[prop] | ||
} | ||
// @ts-expect-error: fine to set other things. | ||
if (!order.includes(prop)) this[prop] = options[prop] | ||
} | ||
@@ -170,2 +185,4 @@ } | ||
* Access full path (`~/index.min.js`). | ||
* | ||
* @returns {string} | ||
*/ | ||
@@ -179,4 +196,10 @@ get path() { | ||
* Cannot be nullified. | ||
* | ||
* @param {string|URL} path | ||
*/ | ||
set path(path) { | ||
if (isUrl(path)) { | ||
path = urlToPath(path) | ||
} | ||
assertNonEmpty(path, 'path') | ||
@@ -201,3 +224,3 @@ | ||
set dirname(dirname) { | ||
assertPath(this.path, 'dirname') | ||
assertPath(this.basename, 'dirname') | ||
this.path = path.join(dirname || '', this.basename) | ||
@@ -237,3 +260,3 @@ } | ||
assertPart(extname, 'extname') | ||
assertPath(this.path, 'extname') | ||
assertPath(this.dirname, 'extname') | ||
@@ -279,3 +302,3 @@ if (extname) { | ||
toString(encoding) { | ||
// @ts-ignore string’s don’t accept the parameter, but buffers do. | ||
// @ts-expect-error string’s don’t accept the parameter, but buffers do. | ||
return (this.value || '').toString(encoding) | ||
@@ -293,3 +316,3 @@ } | ||
message(reason, place, origin) { | ||
var message = new VFileMessage(reason, place, origin) | ||
const message = new VFileMessage(reason, place, origin) | ||
@@ -319,3 +342,3 @@ if (this.path) { | ||
info(reason, place, origin) { | ||
var message = this.message(reason, place, origin) | ||
const message = this.message(reason, place, origin) | ||
@@ -339,3 +362,3 @@ message.fatal = null | ||
fail(reason, place, origin) { | ||
var message = this.message(reason, place, origin) | ||
const message = this.message(reason, place, origin) | ||
@@ -351,3 +374,3 @@ message.fatal = true | ||
* | ||
* @param {string} part | ||
* @param {string|undefined} part | ||
* @param {string} name | ||
@@ -367,5 +390,5 @@ * @returns {void} | ||
* | ||
* @param {string} part | ||
* @param {string|undefined} part | ||
* @param {string} name | ||
* @returns {void} | ||
* @returns {asserts part is string} | ||
*/ | ||
@@ -381,5 +404,5 @@ function assertNonEmpty(part, name) { | ||
* | ||
* @param {string} path | ||
* @param {string|undefined} path | ||
* @param {string} name | ||
* @returns {void} | ||
* @returns {asserts path is string} | ||
*/ | ||
@@ -386,0 +409,0 @@ function assertPath(path, name) { |
@@ -13,3 +13,3 @@ export namespace path { | ||
*/ | ||
declare function basename(path: string, ext?: string): string | ||
declare function basename(path: string, ext?: string | undefined): string | ||
/** | ||
@@ -16,0 +16,0 @@ * @param {string} path |
@@ -54,2 +54,4 @@ // A derivative work based on: | ||
/* eslint-disable max-depth, complexity */ | ||
/** | ||
@@ -61,13 +63,2 @@ * @param {string} path | ||
function basename(path, ext) { | ||
var start = 0 | ||
var end = -1 | ||
/** @type {number} */ | ||
var index | ||
/** @type {number} */ | ||
var firstNonSlashEnd | ||
/** @type {boolean} */ | ||
var seenNonSlash | ||
/** @type {number} */ | ||
var extIndex | ||
if (ext !== undefined && typeof ext !== 'string') { | ||
@@ -78,3 +69,7 @@ throw new TypeError('"ext" argument must be a string') | ||
assertPath(path) | ||
index = path.length | ||
let start = 0 | ||
let end = -1 | ||
let index = path.length | ||
/** @type {boolean|undefined} */ | ||
let seenNonSlash | ||
@@ -105,4 +100,4 @@ if (ext === undefined || ext.length === 0 || ext.length > path.length) { | ||
firstNonSlashEnd = -1 | ||
extIndex = ext.length - 1 | ||
let firstNonSlashEnd = -1 | ||
let extIndex = ext.length - 1 | ||
@@ -157,9 +152,2 @@ while (index--) { | ||
function dirname(path) { | ||
/** @type {number} */ | ||
var end | ||
/** @type {boolean} */ | ||
var unmatchedSlash | ||
/** @type {number} */ | ||
var index | ||
assertPath(path) | ||
@@ -171,4 +159,6 @@ | ||
end = -1 | ||
index = path.length | ||
let end = -1 | ||
let index = path.length | ||
/** @type {boolean|undefined} */ | ||
let unmatchedSlash | ||
@@ -202,21 +192,17 @@ // Prefix `--` is important to not run on `0`. | ||
function extname(path) { | ||
var startDot = -1 | ||
var startPart = 0 | ||
var end = -1 | ||
assertPath(path) | ||
let index = path.length | ||
let end = -1 | ||
let startPart = 0 | ||
let startDot = -1 | ||
// Track the state of characters (if any) we see before our first dot and | ||
// after any path separator we find. | ||
var preDotState = 0 | ||
/** @type {boolean} */ | ||
var unmatchedSlash | ||
/** @type {number} */ | ||
var code | ||
/** @type {number} */ | ||
var index | ||
let preDotState = 0 | ||
/** @type {boolean|undefined} */ | ||
let unmatchedSlash | ||
assertPath(path) | ||
index = path.length | ||
while (index--) { | ||
code = path.charCodeAt(index) | ||
const code = path.charCodeAt(index) | ||
@@ -274,5 +260,5 @@ if (code === 47 /* `/` */) { | ||
function join(...segments) { | ||
var index = -1 | ||
/** @type {string} */ | ||
var joined | ||
let index = -1 | ||
/** @type {string|undefined} */ | ||
let joined | ||
@@ -299,13 +285,8 @@ while (++index < segments.length) { | ||
function normalize(path) { | ||
/** @type {boolean} */ | ||
var absolute | ||
/** @type {string} */ | ||
var value | ||
assertPath(path) | ||
absolute = path.charCodeAt(0) === 47 /* `/` */ | ||
const absolute = path.charCodeAt(0) === 47 /* `/` */ | ||
// Normalize the path according to POSIX rules. | ||
value = normalizeString(path, !absolute) | ||
let value = normalizeString(path, !absolute) | ||
@@ -331,11 +312,11 @@ if (value.length === 0 && !absolute) { | ||
function normalizeString(path, allowAboveRoot) { | ||
var result = '' | ||
var lastSegmentLength = 0 | ||
var lastSlash = -1 | ||
var dots = 0 | ||
var index = -1 | ||
let result = '' | ||
let lastSegmentLength = 0 | ||
let lastSlash = -1 | ||
let dots = 0 | ||
let index = -1 | ||
/** @type {number|undefined} */ | ||
let code | ||
/** @type {number} */ | ||
var code | ||
/** @type {number} */ | ||
var lastSlashIndex | ||
let lastSlashIndex | ||
@@ -422,1 +403,3 @@ while (++index <= path.length) { | ||
} | ||
/* eslint-enable max-depth, complexity */ |
@@ -0,1 +1,3 @@ | ||
import process from 'process' | ||
export const proc = process |
{ | ||
"name": "vfile", | ||
"version": "5.0.2", | ||
"version": "5.1.0", | ||
"description": "Virtual file format for text processing", | ||
@@ -40,7 +40,9 @@ "license": "MIT", | ||
"./lib/minpath.js": "./lib/minpath.browser.js", | ||
"./lib/minproc.js": "./lib/minproc.browser.js" | ||
"./lib/minproc.js": "./lib/minproc.browser.js", | ||
"./lib/minurl.js": "./lib/minurl.browser.js" | ||
}, | ||
"react-native": { | ||
"./lib/minpath.js": "./lib/minpath.browser.js", | ||
"./lib/minproc.js": "./lib/minproc.browser.js" | ||
"./lib/minproc.js": "./lib/minproc.browser.js", | ||
"./lib/minurl.js": "./lib/minurl.browser.js" | ||
}, | ||
@@ -62,4 +64,4 @@ "files": [ | ||
"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", | ||
@@ -69,3 +71,3 @@ "tape": "^5.0.0", | ||
"typescript": "^4.0.0", | ||
"xo": "^0.39.0" | ||
"xo": "^0.44.0" | ||
}, | ||
@@ -91,7 +93,4 @@ "scripts": { | ||
"rules": { | ||
"unicorn/no-array-for-each": "off", | ||
"max-depth": "off", | ||
"complexity": "off", | ||
"no-var": "off", | ||
"prefer-arrow-callback": "off" | ||
"unicorn/prefer-node-protocol": "off", | ||
"#unicorn/no-array-for-each": "off" | ||
} | ||
@@ -112,4 +111,8 @@ }, | ||
"strict": true, | ||
"ignoreCatch": true | ||
"ignoreCatch": true, | ||
"#": "needed `any`s", | ||
"ignoreFiles": [ | ||
"lib/minurl.shared.d.ts" | ||
] | ||
} | ||
} |
@@ -151,2 +151,4 @@ <h1> | ||
Cannot be nullified. | ||
You can set a file URL (a `URL` object with a `file:` protocol) | ||
which will be turned into a path with [`url.fileURLToPath`][file-url-to-path]. | ||
@@ -430,1 +432,3 @@ ### `vfile.basename` | ||
[buffer]: https://nodejs.org/api/buffer.html | ||
[file-url-to-path]: https://nodejs.org/api/url.html#url_url_fileurltopath_url |
53346
21
1149
433