Skip to content

Commit

Permalink
fix: processor needs to be called as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
lottamus committed Jan 28, 2019
1 parent a0c9986 commit 9454184
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 33 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,10 @@ describe('parse', () => {
it('should parse simple', () => {
expect(parse('**simple**')).toMatchSnapshot();
});

it('should work when called multiple times in a row', () => {
// This tests to make sure the processor isn't frozen: https://github.com/unifiedjs/unified/blob/7ee2c8f563f0ebe330cd76496be9ba405a1cd023/readme.md#processorfreeze

expect(parse('**simple**')).toEqual(parse('**simple**'));
});
});
8 changes: 8 additions & 0 deletions src/__tests__/stringify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ describe('stringify', () => {
stringify(JSON.parse(fs.readFileSync(path.resolve(__dirname, './fixtures/simple.json'), 'utf-8')))
).toMatchSnapshot();
});

it('should work when called multiple times in a row', () => {
// This tests to make sure the processor isn't frozen: https://github.com/unifiedjs/unified/blob/7ee2c8f563f0ebe330cd76496be9ba405a1cd023/readme.md#processorfreeze

const json = JSON.parse(fs.readFileSync(path.resolve(__dirname, './fixtures/simple.json'), 'utf-8'));

expect(stringify(json)).toBe(stringify(json));
});
});
4 changes: 3 additions & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ const defaultProcessor = unified().use(remarkParse);

export const parse = (input: string, opts: IParseOpts = defaultOpts, processor: IProcessor = defaultProcessor) => {
// return the parsed remark ast
return processor.data('settings', opts).parse(input);
return processor()
.data('settings', opts)
.parse(input);
};
9 changes: 4 additions & 5 deletions src/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import remarkParse from 'remark-parse';
import remarkStringify, { IStringifyOpts } from 'remark-stringify';
import unified, { IProcessor } from 'unified';
import { INode as Node } from './ast-types/unist';
Expand All @@ -8,10 +7,10 @@ const defaultOpts: IStringifyOpts = {
gfm: true,
};

const defaultProcessor = unified()
.use(remarkParse)
.use(remarkStringify);
const defaultProcessor = unified().use(remarkStringify);

export const stringify = (tree: Node, opts: IStringifyOpts = defaultOpts, processor: IProcessor = defaultProcessor) => {
return processor.data('settings', defaultOpts).stringify(tree);
return processor()
.data('settings', defaultOpts)
.stringify(tree);
};
32 changes: 5 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4020,7 +4020,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -6018,7 +6018,7 @@ import-local@^1.0.0:
pkg-dir "^2.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -7500,11 +7500,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseisequal@^3.0.0:
version "3.0.7"
resolved "https://registry.yarnpkg.com/lodash._baseisequal/-/lodash._baseisequal-3.0.7.tgz#d8025f76339d29342767dcc887ce5cb95a5b51f1"
Expand All @@ -7522,29 +7517,17 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*, lodash._bindcallback@^3.0.0:
lodash._bindcallback@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
Expand Down Expand Up @@ -7681,11 +7664,6 @@ [email protected], lodash.pick@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down Expand Up @@ -10219,7 +10197,7 @@ readable-stream@~1.1.10:
isarray "0.0.1"
string_decoder "~0.10.x"

readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=
Expand Down

0 comments on commit 9454184

Please sign in to comment.