-
Notifications
You must be signed in to change notification settings - Fork 16
feat: getLeadingComments/getTrailingComments
#90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3baa3bd
feat: add additionalComments option for programmatic comment insertion
manuel3108 de0ef35
fix casing
manuel3108 718afce
add type export
manuel3108 af12b38
fix multi-line comments
manuel3108 3784107
Merge branch 'main' of github.com:sveltejs/esrap into feat/additional…
jycouet 4214cd3
update `load` to `acornParse`
jycouet a04abea
Merge branch 'main' into feat/additional-comments
Rich-Harris 005e1c3
feat: `getLeadingComments`/`getTrailingComments`
Rich-Harris 867dce3
unused
Rich-Harris 7cd55e5
expose BaseComment
Rich-Harris e781fb9
oops
Rich-Harris ed52633
Update .changeset/pretty-spiders-hear.md
Rich-Harris 00c0e6c
update README
Rich-Harris 47caf1c
Merge branch 'feat/additional-comments-function' of github.com:svelte…
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'esrap': minor | ||
| --- | ||
|
|
||
| feat: add additionalComments option for programmatic comment insertion | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,12 @@ const { code, map } = print( | |
| quotes: 'single', | ||
|
|
||
| // an array of `{ type: 'Line' | 'Block', value: string, loc: { start, end } }` objects | ||
| comments: [] | ||
| comments: [], | ||
|
|
||
| // a WeakMap of AST nodes to additional comments to insert at specific nodes | ||
| // useful for programmatically adding comments during code transformation, | ||
| // especially for nodes that were added programmatically | ||
| additionalComments: new WeakMap() | ||
|
||
| }) | ||
| ); | ||
| ``` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| export * from './index'; | ||
| export { Comment } from '../types'; | ||
| export { BaseComment, Comment } from '../types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| export * from './index'; | ||
| export { Comment } from '../types'; | ||
| export { BaseComment, Comment } from '../types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // @ts-check | ||
| /** @import { TSESTree } from '@typescript-eslint/types' */ | ||
| /** @import { BaseComment } from '../src/languages/types.js' */ | ||
| import { expect, test } from 'vitest'; | ||
| import { print } from '../src/index.js'; | ||
| import { acornParse } from './common.js'; | ||
| import ts from '../src/languages/ts/index.js'; | ||
|
|
||
| /** | ||
| * @param {string} value | ||
| * @returns {BaseComment} | ||
| */ | ||
| function line(value) { | ||
| return { type: 'Line', value }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} value | ||
| * @returns {BaseComment} | ||
| */ | ||
| function block(value) { | ||
| return { type: 'Block', value }; | ||
| } | ||
|
|
||
| /** | ||
| * Helper to get return statement from a simple function | ||
| * @param {TSESTree.Program} ast - Parsed AST | ||
| * @returns {TSESTree.Node} The return statement | ||
| */ | ||
| function get_return_statement(ast) { | ||
| const functionDecl = ast.body[0]; | ||
| // @ts-expect-error accessing function body | ||
| const statements = functionDecl.body.body; | ||
| // Find the return statement (could be first or second depending on function structure) | ||
| return statements.find(/** @param {any} stmt */ (stmt) => stmt.type === 'ReturnStatement'); | ||
| } | ||
|
|
||
| test('additional comments are inserted correctly', () => { | ||
| const input = `function example() { | ||
| const x = 1; | ||
| return x; | ||
| }`; | ||
|
|
||
| const { ast } = acornParse(input); | ||
| const returnStatement = get_return_statement(ast); | ||
| expect(returnStatement.type).toBe('ReturnStatement'); | ||
|
|
||
| const { code } = print( | ||
| ast, | ||
| ts({ | ||
| getLeadingComments: (n) => | ||
| n === returnStatement ? [line(' This is a leading comment')] : undefined, | ||
| getTrailingComments: (n) => | ||
| n === returnStatement ? [block(' This is a trailing comment ')] : undefined | ||
| }) | ||
| ); | ||
|
|
||
| expect(code).toContain('// This is a leading comment'); | ||
| expect(code).toContain('/* This is a trailing comment */'); | ||
| }); | ||
|
|
||
| test('only leading comments are inserted when specified', () => { | ||
| const input = `function test() { return 42; }`; | ||
| const { ast } = acornParse(input); | ||
| const returnStatement = get_return_statement(ast); | ||
|
|
||
| const { code } = print( | ||
| ast, | ||
| ts({ | ||
| getLeadingComments: (n) => (n === returnStatement ? [line(' Leading only ')] : undefined) | ||
| }) | ||
| ); | ||
|
|
||
| expect(code).toContain('// Leading only'); | ||
| expect(code).not.toContain('trailing'); | ||
| }); | ||
|
|
||
| test('only trailing comments are inserted when specified', () => { | ||
| const input = `function test() { return 42; }`; | ||
| const { ast } = acornParse(input); | ||
| const returnStatement = get_return_statement(ast); | ||
|
|
||
| const { code } = print( | ||
| ast, | ||
| ts({ | ||
| getTrailingComments: (n) => (n === returnStatement ? [block(' Trailing only ')] : undefined) | ||
| }) | ||
| ); | ||
|
|
||
| expect(code).toContain('/* Trailing only */'); | ||
| expect(code).not.toContain('//'); | ||
| }); | ||
|
|
||
| test('additional comments multi-line comments have new line', () => { | ||
| const input = `function example() { | ||
| const x = 1; | ||
| return x; | ||
| }`; | ||
|
|
||
| const { ast } = acornParse(input); | ||
| const returnStatement = get_return_statement(ast); | ||
| expect(returnStatement.type).toBe('ReturnStatement'); | ||
|
|
||
| const { code } = print( | ||
| ast, | ||
| ts({ | ||
| getLeadingComments: (n) => | ||
| n === returnStatement ? [block('*\n * This is a leading comment\n ')] : undefined | ||
| }) | ||
| ); | ||
|
|
||
| expect(code).toMatchInlineSnapshot(` | ||
| "function example() { | ||
| const x = 1; | ||
|
|
||
| /** | ||
| * This is a leading comment | ||
| */ | ||
| return x; | ||
| }" | ||
| `); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.