Skip to content

Commit

Permalink
feat(utils): compare
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 672b668 commit 5eb4d69
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ ignore:
- '**/index.ts'

profiling:
critical_files_paths: []
critical_files_paths:
- src/utils/compare.ts
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[![vitest](https://img.shields.io/badge/-vitest-6e9f18?style=flat&logo=vitest&logoColor=ffffff)](https://vitest.dev/)
[![yarn](https://img.shields.io/badge/-yarn-2c8ebb?style=flat&logo=yarn&logoColor=ffffff)](https://yarnpkg.com/)

[esast][esast](and [estree][estree]) utility to attach comments
[esast][esast] (and [estree][estree]) utility to attach comments

## Contents

Expand Down
6 changes: 1 addition & 5 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
import pkg from './package.json' assert { type: 'json' }
import tsconfig from './tsconfig.build.json' assert { type: 'json' }

/**
Expand All @@ -16,10 +15,7 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
const config: Config = defineBuildConfig({
charset: 'utf8',
entries: [{ dts: 'only' }, { dts: false, ignore: ['types'] }],
target: [
pkg.engines.node.replace(/^\D+/, 'node'),
tsconfig.compilerOptions.target
],
target: ['node18', tsconfig.compilerOptions.target],
tsconfig: 'tsconfig.build.json'
})

Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"@types/is-ci": "3.0.4",
"@types/node": "20.11.24",
"@types/node-notifier": "8.0.5",
"@types/unist": "3.0.2",
"@typescript-eslint/eslint-plugin": "7.1.0",
"@typescript-eslint/parser": "7.1.0",
"@vates/toggle-scripts": "1.0.0",
Expand Down Expand Up @@ -147,20 +148,22 @@
"yaml-eslint-parser": "1.2.2"
},
"peerDependencies": {
"@types/estree": ">=1.0.5"
"@types/estree": ">=1.0.5",
"@types/unist": ">=3.0.2"
},
"peerDependenciesMeta": {
"@types/estree": {
"optional": true
},
"@types/unist": {
"optional": true
}
},
"resolutions": {
"@types/estree": "1.0.5",
"@types/unist": "3.0.2",
"chai": "5.1.0"
},
"engines": {
"node": ">=18.18.2"
},
"packageManager": "[email protected]",
"sideEffects": false
}
74 changes: 74 additions & 0 deletions src/utils/__tests__/compare.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @file Unit Tests - compare
* @module esast-util-attach-comments/utils/tests/unit/compare
*/

import type { Point, Position } from 'unist'
import testSubject from '../compare'

describe('unit:utils/compare', () => {
it('should return Number.NaN if comparison cannot be made', () => {
expect(testSubject({}, {}, true)).to.be.NaN
})

describe('position', () => {
let comment: { position: Position }

beforeAll(() => {
comment = {
position: {
end: { column: 33, line: 11, offset: 411 },
start: { column: 18, line: 11, offset: 396 }
}
}
})

it('should return node.position.end.column comparison result', () => {
// Arrange
const end: Point = { column: 16, line: 11, offset: 394 }
const start: Point = { column: 5, line: 11, offset: 383 }

// Act
const result = testSubject(comment, { position: { end, start } }, true)

// Expect
expect(result).to.equal(comment.position.start.column - end.column)
})

it('should return node.position.end.line comparison result', () => {
// Arrange
const end: Point = { column: 21, line: 5, offset: 216 }
const start: Point = { column: 13, line: 5, offset: 208 }

// Act
const result = testSubject(comment, { position: { end, start } })

// Expect
expect(result).to.equal(comment.position.start.line - end.line)
})

it('should return node.position.start.column comparison result', () => {
// Arrange
const end: Point = { column: 43, line: 11, offset: 421 }
const start: Point = { column: 34, line: 11, offset: 412 }

// Act
const result = testSubject(comment, { position: { end, start } })

// Expect
expect(result).to.equal(comment.position.start.column - start.column)
})

it('should return node.position.start.line comparison result', () => {
// Arrange
const end: Point = { column: 50, line: 3, offset: 144 }
const start: Point = { column: 1, line: 3, offset: 95 }

// Act
const result = testSubject(comment, { position: { end, start } })

// Expect
expect(result).to.equal(comment.position.start.line - start.line)
})
})
})
60 changes: 60 additions & 0 deletions src/utils/compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @file Utilities - compare
* @module esast-util-attach-comments/utils/compare
*/

import type { Comment, Node } from 'estree'

declare module 'estree' {
interface BaseNodeWithoutComments {
position?: import('unist').Position | undefined
}
}

/**
* Field to compare on.
*
* @internal
*/
type Field = 'end' | 'start'

/**
* Compare `node` against the start position of `comment`.
*
* @see {@linkcode Comment}
* @see {@linkcode Node}
*
* @internal
*
* @this {void}
*
* @param {Pick<Comment, 'position'>} comment - Comment node
* @param {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'>,
end?: boolean
): number {
/**
* Field to compare on.
*
* @const {Field} field
*/
const field: Field = end ? 'end' : 'start'

// compare positions
if (comment.position?.start && node.position?.[field]) {
return (
comment.position.start.line - node.position[field].line ||
comment.position.start.column - node.position[field].column
)
}

return Number.NaN
}

export default compare
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* @module esast-util-attach-comments/utils
*/

export { default as compare } from './compare'
export { default as keycheck } from './keycheck'
13 changes: 5 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ __metadata:
"@types/is-ci": "npm:3.0.4"
"@types/node": "npm:20.11.24"
"@types/node-notifier": "npm:8.0.5"
"@types/unist": "npm:3.0.2"
"@typescript-eslint/eslint-plugin": "npm:7.1.0"
"@typescript-eslint/parser": "npm:7.1.0"
"@vates/toggle-scripts": "npm:1.0.0"
Expand Down Expand Up @@ -1506,9 +1507,12 @@ __metadata:
yaml-eslint-parser: "npm:1.2.2"
peerDependencies:
"@types/estree": ">=1.0.5"
"@types/unist": ">=3.0.2"
peerDependenciesMeta:
"@types/estree":
optional: true
"@types/unist":
optional: true
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -2483,20 +2487,13 @@ __metadata:
languageName: node
linkType: hard

"@types/unist@npm:*, @types/unist@npm:^3.0.0":
"@types/unist@npm:3.0.2":
version: 3.0.2
resolution: "@types/unist@npm:3.0.2"
checksum: 10/3d04d0be69316e5f14599a0d993a208606c12818cf631fd399243d1dc7a9bd8a3917d6066baa6abc290814afbd744621484756803c80cba892c39cd4b4a85616
languageName: node
linkType: hard

"@types/unist@npm:^2.0.0, @types/unist@npm:^2.0.2":
version: 2.0.10
resolution: "@types/unist@npm:2.0.10"
checksum: 10/e2924e18dedf45f68a5c6ccd6015cd62f1643b1b43baac1854efa21ae9e70505db94290434a23da1137d9e31eb58e54ca175982005698ac37300a1c889f6c4aa
languageName: node
linkType: hard

"@types/validator@npm:^13.7.10":
version: 13.11.8
resolution: "@types/validator@npm:13.11.8"
Expand Down

0 comments on commit 5eb4d69

Please sign in to comment.