Skip to content

Commit

Permalink
feat(utils): slice
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Mar 4, 2024
1 parent aa3c242 commit 5a71bd4
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/types/__tests__/state.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module esast-util-attach-comments/types/tests/unit-d/State
*/

import type { Optional } from '@flex-development/tutils'
import type { Comment } from 'estree'
import type TestSubject from '../state'

Expand All @@ -16,4 +17,10 @@ describe('unit-d:types/State', () => {
it('should match [index: number]', () => {
expectTypeOf<TestSubject>().toHaveProperty('index').toEqualTypeOf<number>()
})

it('should match [leave?: boolean]', () => {
expectTypeOf<TestSubject>()
.toHaveProperty('leave')
.toEqualTypeOf<Optional<boolean>>()
})
})
5 changes: 5 additions & 0 deletions src/types/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type State = {
* Index of current comment.
*/
index: number

/**
* Visiting nodes on exit?
*/
leave?: boolean
}

export type { State as default }
10 changes: 9 additions & 1 deletion src/utils/__tests__/compare.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import testSubject from '../compare'

describe('unit:utils/compare', () => {
it('should return Number.NaN if comparison cannot be made', () => {
expect(testSubject({}, {}, true)).to.be.NaN
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[],
[{}, null],
[null, {}, true]
]

// Act + Expect
cases.forEach(params => expect(testSubject(...params)).to.be.NaN)
})

describe('position', () => {
Expand Down
61 changes: 61 additions & 0 deletions src/utils/__tests__/slice.spec.ts
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]])
})
})
11 changes: 6 additions & 5 deletions src/utils/compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module esast-util-attach-comments/utils/compare
*/

import type { Nilable } from '@flex-development/tutils'
import type { Comment, Node } from 'estree'

declare module 'estree' {
Expand All @@ -28,15 +29,15 @@ type Field = 'end' | 'start'
*
* @this {void}
*
* @param {Pick<Comment, 'position'>} comment - Comment node
* @param {Pick<Comment | Node, 'position'>} node - Node to check
* @param {Nilable<Pick<Comment, 'position'>>?} [comment] - Comment node
* @param {Nilable<Pick<Comment | Node, 'position'>>?} [node] - Node to check
* @param {boolean?} [end] - Use `end` position of `node` in comparsion?
* @return {number} Comparison result
*/
function compare(
this: void,
comment: Pick<Comment, 'position'>,
node: Pick<Comment | Node, 'position'>,
comment?: Nilable<Pick<Comment, 'position'>>,
node?: Nilable<Pick<Comment | Node, 'position'>>,
end?: boolean
): number {
/**
Expand All @@ -47,7 +48,7 @@ function compare(
const field: Field = end ? 'end' : 'start'

// compare positions
if (comment.position?.start && node.position?.[field]) {
if (comment?.position?.start && node?.position?.[field]) {
return (
comment.position.start.line - node.position[field].line ||
comment.position.start.column - node.position[field].column
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export { default as compare } from './compare'
export { default as keycheck } from './keycheck'
export { default as slice } from './slice'
42 changes: 42 additions & 0 deletions src/utils/slice.ts
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

0 comments on commit 5a71bd4

Please sign in to comment.