Skip to content

fix: preserve non-enumerable properties in cached results#185

Open
mixelburg wants to merge 1 commit into
jonschlinkert:masterfrom
mixelburg:fix/cached-non-enumerable-props
Open

fix: preserve non-enumerable properties in cached results#185
mixelburg wants to merge 1 commit into
jonschlinkert:masterfrom
mixelburg:fix/cached-non-enumerable-props

Conversation

@mixelburg

Copy link
Copy Markdown

Summary

Fixes two caching bugs:

1. .matter and .language lost on cached calls (#174)

The matter and language properties are defined as non-enumerable via Reflect.defineProperty in toFile.js. When the cache returns a shallow copy via Object.assign({}, cached), non-enumerable properties are skipped. This means on the second (cached) call, file.matter and file.language are undefined.

const matter = require('gray-matter');
matter('---\ntitle: Title\n---').matter // '\ntitle: Title'
matter('---\ntitle: Title\n---').matter // undefined (BUG)

Fix: Explicitly copy matter and language after Object.assign, following the same pattern already used for orig.

2. Invalid front-matter was cached (#166)

The file object was cached before parseMatter ran, so if parsing threw an error (e.g. invalid YAML), the raw input stayed in the cache. The second call returned a wrong result instead of throwing again.

Fix: Moved matter.cache[file.content] = file to after parseMatter succeeds.

Fixes #174, fixes #166

Fix two caching bugs:

1. .matter and .language were lost on cached calls (jonschlinkert#174)
   These properties are defined as non-enumerable via Reflect.defineProperty
   in toFile.js. Object.assign({}, cached) skips non-enumerable properties,
   so file.matter and file.language were undefined on the second call.

2. Invalid front-matter was cached (jonschlinkert#166)
   The file object was cached before parseMatter ran, so if parsing threw
   an error, the raw input was cached and the second call returned a wrong
   result. Moved cache write to after parseMatter succeeds.

Fixes jonschlinkert#174, fixes jonschlinkert#166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.matter is not cached properly Invalid front-matter should not be cached

1 participant