-
-
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
672b668
commit 5eb4d69
Showing
8 changed files
with
151 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 |
---|---|---|
|
@@ -88,4 +88,5 @@ ignore: | |
- '**/index.ts' | ||
|
||
profiling: | ||
critical_files_paths: [] | ||
critical_files_paths: | ||
- src/utils/compare.ts |
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 |
---|---|---|
|
@@ -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", | ||
|
@@ -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 | ||
} |
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,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) | ||
}) | ||
}) | ||
}) |
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,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 |
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