-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
aa3c242
commit 5a71bd4
Showing
7 changed files
with
131 additions
and
6 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
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,61 @@ | ||
/** | ||
* @file Unit Tests - slice | ||
* @module esast-util-attach-comments/utils/tests/unit/slice | ||
*/ | ||
|
||
import type { State } from '#src/types' | ||
import type { NewExpression } from 'estree' | ||
import testSubject from '../slice' | ||
|
||
describe('unit:utils/slice', () => { | ||
let node: NewExpression | ||
let state: State | ||
|
||
beforeAll(() => { | ||
state = { | ||
comments: [ | ||
{ | ||
position: { | ||
end: { column: 33, line: 11, offset: 411 }, | ||
start: { column: 18, line: 11, offset: 396 } | ||
}, | ||
type: 'Block', | ||
value: ' @__PURE__ ' | ||
}, | ||
{ | ||
position: { | ||
end: { column: 27, line: 14, offset: 535 }, | ||
start: { column: 12, line: 14, offset: 520 } | ||
}, | ||
type: 'Block', | ||
value: ' @__PURE__ ' | ||
} | ||
], | ||
index: 0 | ||
} | ||
|
||
node = { | ||
arguments: [], | ||
callee: { | ||
name: 'Set', | ||
position: { | ||
end: { column: 41, line: 11, offset: 419 }, | ||
start: { column: 38, line: 11, offset: 416 } | ||
}, | ||
type: 'Identifier' | ||
}, | ||
position: { | ||
end: { column: 43, line: 11, offset: 421 }, | ||
start: { column: 34, line: 11, offset: 412 } | ||
}, | ||
type: 'NewExpression' | ||
} | ||
}) | ||
|
||
it('should return slice from state.comments', () => { | ||
expect(testSubject(state, node)) | ||
.to.be.an('array') | ||
.of.length(1) | ||
.with.deep.ordered.members([state.comments[0]]) | ||
}) | ||
}) |
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,42 @@ | ||
/** | ||
* @file Utilities - slice | ||
* @module esast-util-attach-comments/utils/slice | ||
*/ | ||
|
||
import type { State } from '#src/types' | ||
import type { Comment, Node } from 'estree' | ||
import compare from './compare' | ||
|
||
/** | ||
* Get a comments slice. | ||
* | ||
* @see {@linkcode Comment} | ||
* @see {@linkcode Node} | ||
* @see {@linkcode State} | ||
* | ||
* @internal | ||
* | ||
* @this {void} | ||
* | ||
* @param {State} state - Visitor state | ||
* @param {Node} node - Node being visited | ||
* @return {Comment[]} Slice from `state.comments` | ||
*/ | ||
function slice(this: void, state: State, node: Node): Comment[] { | ||
/** | ||
* Slice result. | ||
* | ||
* @const {Comment[]} result | ||
*/ | ||
const result: Comment[] = [] | ||
|
||
// get slice | ||
while ( | ||
state.comments[state.index] && | ||
compare(state.comments[state.index], node, state.leave) < 1 | ||
) result.push(state.comments[state.index++]!) | ||
|
||
return result | ||
} | ||
|
||
export default slice |