Skip to content

Commit

Permalink
Add augmentation of types in file.data.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jun 12, 2022
1 parent 2401ff1 commit 6618d18
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
coverage/
node_modules/
.DS_Store
*.d.ts
test/*.d.ts
index.d.ts
*.log
yarn.lock
29 changes: 29 additions & 0 deletions complex-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Need a random export to turn this into a module?
export type Whatever = unknown

declare module 'vfile' {
interface DataMap {
meta: {
/**
* Date when this content was first published.
*
* Populated by `unified-infer-git-meta` from Git history.
*/
published?: Date

/**
* Date when this content was last modified.
*
* Populated by `unified-infer-git-meta` from Git history.
*/
modified?: Date

/**
* Authors who contributed to this content.
*
* Populated by `unified-infer-git-meta` from Git history.
*/
author?: string
}
}
}
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/**
* @typedef {import('./complex-types.js')} DoNotTouchThisAsItIncludesAugmentation
*
* @callback Format
* Format function.
* @param {Array<string>} authors
Expand Down Expand Up @@ -111,9 +113,8 @@ export default function unifiedInferGitMeta(options = {}) {
const matter = /** @type {Record<string, unknown>} */ (
file.data.matter || {}
)
const meta = /** @type {Record<string, unknown>} */ (
file.data.meta || (file.data.meta = {})
)
const meta = file.data.meta || (file.data.meta = {})

if (!matter.published && !meta.published) meta.published = published
if (!matter.modified && !meta.modified) meta.modified = modified
if (author && !matter.author && !meta.author) meta.author = author
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"complex-types.d.ts",
"index.d.ts",
"index.js"
],
Expand Down Expand Up @@ -58,7 +59,7 @@
"xo": "^0.50.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" \"test/**/*.d.ts\" && tsc && type-coverage",
"build": "rimraf \"{index,test}.d.ts\" \"test/**/*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test/index.js",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"include": ["*.js", "test/**/*.js"],
"include": ["test/**/*.js", "*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
Expand Down

0 comments on commit 6618d18

Please sign in to comment.