-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(evasive-transform): elideComments option
- Loading branch information
Showing
6 changed files
with
251 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
User-visible changes in `@endo/evasive-transform`: | ||
|
||
# Next release | ||
|
||
- Adds an `elideComments` option to replace the interior of comments with | ||
minimal blank space with identical cursor advancement behavior. |
This file contains 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 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 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 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,168 @@ | ||
import { test } from './prepare-test-env-ava-fixture.js'; | ||
import { elideComment } from '../src/transform-comment.js'; | ||
import { evadeCensorSync } from '../src/index.js'; | ||
|
||
test('elideComment preserves the column width of the last and only line of a block comment', t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: ' hello world ', | ||
}); | ||
elideComment(comment); | ||
t.is(comment.value, ' '); | ||
}); | ||
|
||
test('elideComment erases non-final lines but preserves all newlines in a block comment', t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: ' * some\n * unnecessary information \n * the end', | ||
}); | ||
elideComment(comment); | ||
t.is(comment.value, '\n\n '); | ||
}); | ||
|
||
test('elideComment unconditionally elides line comments', t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentLine', | ||
value: ' hello', | ||
}); | ||
elideComment(comment); | ||
t.is(comment.value, ''); | ||
}); | ||
|
||
test('evadeCensor with elideComments erases the interior of block comments', t => { | ||
const object = evadeCensorSync( | ||
`/** | ||
* Comment | ||
* @param {type} name | ||
*/`, | ||
{ elideComments: true }, | ||
); | ||
t.is( | ||
object.code, | ||
`/* | ||
*/`, | ||
); | ||
}); | ||
|
||
test('evadeCensor with elideComments elides line comments', t => { | ||
const object = evadeCensorSync(`// hello`, { elideComments: true }); | ||
t.is(object.code, `//`); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves bang comments', t => { | ||
const object = evadeCensorSync(`/*! kris wuz here */`, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, `/*! kris wuz here */`); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves jsdoc @preserve comments', t => { | ||
const comment = `/** | ||
* @preserve | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves initial jsdoc @preserve comments', t => { | ||
const comment = `/** @preserve | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves artless-but-valid jsdoc @preserve comments', t => { | ||
const comment = `/** | ||
@preserve | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves jsdoc @copyright comments', t => { | ||
const comment = `/** | ||
* @copyright | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves jsdoc @license comments', t => { | ||
const comment = `/** | ||
* @license | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments preserves jsdoc @cc_on comments', t => { | ||
const comment = `/** | ||
* @cc_on | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is(object.code, comment); | ||
}); | ||
|
||
test('evadeCensor with elideComments does not preserve jsdoc @copyrighteous comments', t => { | ||
const comment = `/** | ||
* @copyrighteous | ||
*/`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is( | ||
object.code, | ||
`/* | ||
*/`, | ||
); | ||
}); | ||
|
||
/* eslint-disable no-eval */ | ||
|
||
test('evadeCensor with elideComments preserves automatically-inserted-semicolon (ASI)', t => { | ||
const comment = ` | ||
(() => { | ||
return /* | ||
*/ 42; | ||
})(); | ||
`; | ||
const object = evadeCensorSync(comment, { | ||
elideComments: true, | ||
}); | ||
t.is((0, eval)(comment), undefined); | ||
t.is((0, eval)(object.code), undefined); | ||
}); | ||
|
||
test('evadeCensor with stripComments preserves automatically-inserted-semicolon (ASI)', t => { | ||
t.log( | ||
'There is no stripComments option. This is a trip-fall in case this is attempted.', | ||
); | ||
const comment = ` | ||
(() => { | ||
return /* | ||
*/ 42; | ||
})(); | ||
`; | ||
const object = evadeCensorSync(comment, { | ||
stripComments: true, | ||
}); | ||
t.is((0, eval)(comment), undefined); | ||
t.is((0, eval)(object.code), undefined); | ||
}); | ||
|
||
/* eslint-enable no-eval */ |
22 changes: 11 additions & 11 deletions
22
packages/evasive-transform/test/transform-comment.test.js
This file contains 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,51 +1,51 @@ | ||
import { test } from './prepare-test-env-ava-fixture.js'; | ||
import { transformComment } from '../src/transform-comment.js'; | ||
import { evadeComment } from '../src/transform-comment.js'; | ||
|
||
test('transformComment() - Node type becomes CommentBlock', async t => { | ||
test('evadeComment() - Node type becomes CommentBlock', async t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
value: 'hello world', | ||
}); | ||
transformComment(comment); | ||
evadeComment(comment); | ||
t.is(comment.type, 'CommentBlock'); | ||
}); | ||
|
||
test('transformComment() - strip extraneous leading whitespace', async t => { | ||
test('evadeComment() - strip extraneous leading whitespace', async t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: ' hello world ', | ||
}); | ||
transformComment(comment); | ||
evadeComment(comment); | ||
t.is(comment.value, ' hello world '); | ||
}); | ||
|
||
test('transformComment() - defang HTML comment', async t => { | ||
test('evadeComment() - defang HTML comment', async t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: '<!-- evil code -->', | ||
}); | ||
transformComment(comment); | ||
evadeComment(comment); | ||
t.is(comment.value, '<!=- evil code -=>'); | ||
}); | ||
|
||
test('transformComment() - rewrite suspicious import(...)', async t => { | ||
test('evadeComment() - rewrite suspicious import(...)', async t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: `/** | ||
* @type {import('c:\\My Documents\\user.js')} | ||
*/`, | ||
}); | ||
transformComment(comment); | ||
evadeComment(comment); | ||
t.regex( | ||
comment.value, | ||
new RegExp("\\* @type \\{IMPORT\\('c:\\\\My Documents\\\\user\\.js'\\)"), | ||
); | ||
}); | ||
|
||
test('transformComment() - rewrite end-of-comment marker', async t => { | ||
test('evadeComment() - rewrite end-of-comment marker', async t => { | ||
const comment = /** @type {import('@babel/types').Comment} */ ({ | ||
type: 'CommentBlock', | ||
value: '/** I like turtles */', | ||
}); | ||
transformComment(comment); | ||
evadeComment(comment); | ||
t.is(comment.value, '/** I like turtles *X/'); | ||
}); |