Skip to content

Commit

Permalink
add an example for producing Babel AST from the source, issue #99
Browse files Browse the repository at this point in the history
  • Loading branch information
syavorsky committed Jan 6, 2021
1 parent 2cca78f commit ccb42c2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ examples
- [line numbers control](https://syavorsky.github.io/comment-parser/#parse-line-numbering)
- [description spacing](https://syavorsky.github.io/comment-parser/#parse-spacing)
- [escaping](https://syavorsky.github.io/comment-parser/#parse-escaping)
- [explore the origin source](https://syavorsky.github.io/comment-parser/#parse-source-exploration)

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

Expand Down
47 changes: 47 additions & 0 deletions tests/e2e/examples.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,57 @@ function stringify_formatting(source, parse, stringify, transforms) {
const stringified = stringify(transform(parsed[0]));
}

function parse_source_exploration(source, parse, stringify, transforms) {
// parse() produces Block[].source keeping accurate track of origin source

/**
* Description may go
* over multiple lines followed by @tags
* @param {string} name the name parameter
* @param {any} value the value parameter
*/

const parsed = parse(source);

const summary = ({ source }) => ({
source: source
.map(
({ tokens }) =>
tokens.start +
tokens.delimiter +
tokens.postDelimiter +
tokens.tag +
tokens.postTag +
tokens.type +
tokens.postType +
tokens.name +
tokens.postName +
tokens.description +
tokens.end
)
.join('\n'),
start: {
line: source[0].number + 1,
column: source[0].tokens.start.length,
},
end: {
line: source[source.length - 1].number + 1,
column: source[source.length - 1].source.length,
},
});

const pos = (p) => p.line + ':' + p.column;

const stringified = parsed[0].tags
.map(summary)
.map((s) => `${pos(s.start)} - ${pos(s.end)}\n${s.source}`);
}

(typeof window === 'undefined' ? module.exports : window).examples = [
parse_defaults,
parse_line_numbering,
parse_escaping,
parse_spacing,
parse_source_exploration,
stringify_formatting,
];

0 comments on commit ccb42c2

Please sign in to comment.