Skip to content

Commit

Permalink
assure consistent source referencing/clonning
Browse files Browse the repository at this point in the history
  • Loading branch information
syavorsky committed Jan 6, 2021
1 parent e970379 commit 2cca78f
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ examples

## Stringifier

The stringifier is an important piece used by other tools updating the source code. It goes over `Block.source[].tokens` and assembles them back to a string. It might be used with various transforms applied to the parsed data before strngifying.
The stringifier is an important piece used by other tools updating the source code. It goes over `Block.source[].tokens` and assembles them back to a string. It might be used with various transforms applied to the parsed data before stringifying.

```js
const { parse, stringify, transforms: {flow, align, indent} } = require('./lib/');
Expand Down Expand Up @@ -201,7 +201,7 @@ console.log(stringify(transform(parsed[0])));
```

examples
- [using standard formats](https://syavorsky.github.io/comment-parser/#stringify-formatting)
- [format comments](https://syavorsky.github.io/comment-parser/#stringify-formatting)

[suggest more examples](https://github.com/syavorsky/comment-parser/issues/new?title=example+suggestion%3A+...&labels=example,stringifier)

Expand Down
3 changes: 2 additions & 1 deletion src/transforms/align.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transform } from './index';
import { Markers, Tokens, Block, Line } from '../primitives';
import { rewireSource } from '../util';

interface Width {
start: number;
Expand Down Expand Up @@ -76,6 +77,6 @@ export default function align(): Transform {

return ({ source, ...fields }: Block): Block => {
w = source.reduce(getWidth, { ...zeroWidth });
return { ...fields, source: source.map(update) };
return rewireSource({ ...fields, source: source.map(update) });
};
}
17 changes: 7 additions & 10 deletions src/transforms/indent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Transform } from './index';
import { Block, Line } from '../primitives';
import { rewireSource } from '../util';

const pull = (offset: number) => (str) => str.slice(offset);
const push = (offset: number) => {
Expand All @@ -17,15 +18,11 @@ export default function indent(pos: number): Transform {
return shift(start);
};

const update = (line: Line): Line => {
line.tokens.start = pad(line.tokens.start);
return line;
};

return ({ description, tags, source, problems }: Block): Block => ({
description,
problems,
tags,
source: source.map(update),
const update = (line: Line): Line => ({
...line,
tokens: { ...line.tokens, start: pad(line.tokens.start) },
});

return ({ source, ...fields }: Block): Block =>
rewireSource({ ...fields, source: source.map(update) });
}
13 changes: 12 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Block, Tokens, Spec } from './primitives';
import { Block, Tokens, Spec, Line } from './primitives';

export function isSpace(source: string): boolean {
return /^\s+$/.test(source);
Expand Down Expand Up @@ -54,3 +54,14 @@ export function seedTokens(tokens: Partial<Tokens> = {}): Tokens {
...tokens,
};
}

export function rewireSource(block: Block): Block {
const source = block.source.reduce(
(acc, line) => acc.set(line.number, line),
new Map<number, Line>()
);
for (const spec of block.tags) {
spec.source = spec.source.map((line) => source.get(line.number));
}
return block;
}
13 changes: 13 additions & 0 deletions tests/unit/transforms-align.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ test('same line close', () => {

expect(out).toBe(source);
});

test('spec source referencing', () => {
const parsed = getParser()(`/** @tag {type} name Description */`);
const block = align()(parsed[0]);
expect(block.tags[0].source[0] === block.source[0]).toBe(true);
});

test('block source clonning', () => {
const parsed = getParser()(`/** @tag {type} name Description */`);
const block = align()(parsed[0]);
parsed[0].source[0].tokens.description = 'test';
expect(block.source[0].tokens.description).toBe('Description ');
});
20 changes: 10 additions & 10 deletions tests/unit/transforms-indent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ test('force pull', () => {
expect(out).toBe(expected.slice(1));
});

test('no source clonning', () => {
const parsed = getParser()(`
/**
* Description may go
* over few lines followed by @tags
* @param {string} name name parameter
*
* @param {any} value value of any type
*/`);
test('spec source referencing', () => {
const parsed = getParser()(`/** @tag {type} name Description */`);
const block = indent(0)(parsed[0]);
expect(block.tags[0].source[0] === block.source[0]).toBe(true);
});

test('block source clonning', () => {
const parsed = getParser()(`/** @tag {type} name Description */`);
const block = indent(0)(parsed[0]);
expect(block.tags[0].source[0] === block.source[3]).toBe(true);
parsed[0].source[0].tokens.description = 'test';
expect(block.source[0].tokens.description).toBe('Description ');
});
21 changes: 21 additions & 0 deletions tests/unit/transforms.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { flow } from '../../src/transforms/index';
import { seedBlock } from '../../src/util';
import { Block } from '../../src/primitives';

const t0 = (b: Block): Block => ({ ...b, description: b.description + ' t0' });
const t1 = (b: Block): Block => ({ ...b, description: b.description + ' t1' });

test('multiple', () => {
const block = seedBlock({ description: 'test' });
expect(flow(t0, t1)(block).description).toBe('test t0 t1');
});

test('one', () => {
const block = seedBlock({ description: 'test' });
expect(flow(t0)(block).description).toBe('test t0');
});

test('none', () => {
const block = seedBlock({ description: 'test' });
expect(flow()(block).description).toBe('test');
});
48 changes: 48 additions & 0 deletions tests/unit/util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
splitLines,
splitSpace,
seedSpec,
rewireSource,
} from '../../src/util';

test.each([
Expand Down Expand Up @@ -116,3 +117,50 @@ test('seedSpec overrides', () => {
source: [],
});
});

test('rewireSource', () => {
const source = () => [
{
number: 42,
source: '/** @my-tag */',
tokens: {
start: '',
delimiter: '/**',
postDelimiter: ' ',
tag: '@my-tag',
postTag: ' ',
name: '',
postName: '',
type: '',
postType: '',
description: '',
end: '*/',
},
},
];

const parsed = {
description: '',
tags: [
{
tag: 'my-tag',
name: '',
type: '',
optional: false,
description: '',
problems: [],
source: source(),
},
],
source: source(),
problems: [],
};

expect(parsed.source[0] === parsed.tags[0].source[0]).toBe(false);

rewireSource(parsed);

expect(parsed.source[0] === parsed.tags[0].source[0]).toBe(true);
parsed.source[0].tokens.name = 'test';
expect(parsed.tags[0].source[0].tokens.name).toBe('test');
});

0 comments on commit 2cca78f

Please sign in to comment.