-
-
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 <unicornware@flexdevelopment.llc>
- Loading branch information
1 parent
ff3aa94
commit 9ab9994
Showing
6 changed files
with
190 additions
and
2 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 |
---|---|---|
|
@@ -90,3 +90,4 @@ ignore: | |
profiling: | ||
critical_files_paths: | ||
- src/utils/get-identifiers.ts | ||
- src/utils/has-module-id.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* @file Unit Tests - hasModuleId | ||
* @module estree-util-unassert/utils/tests/unit/hasModuleId | ||
*/ | ||
|
||
import type { Assign } from '@flex-development/tutils' | ||
import { createFilter, type CreateFilter } from '@rollup/pluginutils' | ||
import type { | ||
AwaitExpression, | ||
CallExpression, | ||
Identifier, | ||
ImportDeclaration, | ||
Literal | ||
} from 'estree' | ||
import testSubject from '../has-module-id' | ||
|
||
describe('unit:utils/hasModuleId', () => { | ||
let devlopIdentifier: Identifier | ||
let devlopLiteral: Assign<Literal, { raw: string; value: string }> | ||
let filter: ReturnType<CreateFilter> | ||
|
||
beforeAll(() => { | ||
devlopLiteral = { raw: '\'devlop\'', type: 'Literal', value: 'devlop' } | ||
devlopIdentifier = { name: devlopLiteral.value, type: 'Identifier' } | ||
filter = createFilter(/^devlop$/, [], { resolve: false }) | ||
}) | ||
|
||
it('should return false if node.type is ignored', () => { | ||
expect(testSubject(devlopIdentifier, filter)).to.be.false | ||
}) | ||
|
||
describe('AwaitExpression', () => { | ||
it('should return false if node is not dynamic assertion import', () => { | ||
// Arrange | ||
const node: AwaitExpression = { | ||
argument: { source: devlopIdentifier, type: 'ImportExpression' }, | ||
type: 'AwaitExpression' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.false | ||
}) | ||
|
||
it('should return true node is dynamic assertion import', () => { | ||
// Arrange | ||
const node: AwaitExpression = { | ||
argument: { source: devlopLiteral, type: 'ImportExpression' }, | ||
type: 'AwaitExpression' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.true | ||
}) | ||
}) | ||
|
||
describe('CallExpression', () => { | ||
it('should return false if node is not assertion `require` call', () => { | ||
// Arrange | ||
const node: CallExpression = { | ||
arguments: [devlopIdentifier], | ||
callee: { name: 'require', type: 'Identifier' }, | ||
optional: false, | ||
type: 'CallExpression' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.false | ||
}) | ||
|
||
it('should return true node is assertion `require` call', () => { | ||
// Arrange | ||
const node: CallExpression = { | ||
arguments: [devlopLiteral], | ||
callee: { name: 'require', type: 'Identifier' }, | ||
optional: false, | ||
type: 'CallExpression' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.true | ||
}) | ||
}) | ||
|
||
describe('ImportDeclaration', () => { | ||
let invariant: Assign<Literal, { raw: string; value: string }> | ||
|
||
beforeAll(() => { | ||
invariant = { raw: '\'invariant\'', type: 'Literal', value: 'invariant' } | ||
}) | ||
|
||
it('should return false if node is not assertion import', () => { | ||
// Arrange | ||
const node: ImportDeclaration = { | ||
source: invariant, | ||
specifiers: [ | ||
{ | ||
local: { name: invariant.value, type: 'Identifier' }, | ||
type: 'ImportDefaultSpecifier' | ||
} | ||
], | ||
type: 'ImportDeclaration' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.false | ||
}) | ||
|
||
it('should return true node is assertion import', () => { | ||
// Arrange | ||
const node: ImportDeclaration = { | ||
source: devlopLiteral, | ||
specifiers: [ | ||
{ | ||
imported: { name: 'ok', type: 'Identifier' }, | ||
local: { name: 'ok', type: 'Identifier' }, | ||
type: 'ImportSpecifier' | ||
} | ||
], | ||
type: 'ImportDeclaration' | ||
} | ||
|
||
// Act + Expect | ||
expect(testSubject(node, filter)).to.be.true | ||
}) | ||
}) | ||
}) |
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,58 @@ | ||
/** | ||
* @file Utilities - hasModuleId | ||
* @module estree-util-unassert/utils/hasModuleId | ||
*/ | ||
|
||
import type { Nilable } from '@flex-development/tutils' | ||
import type { | ||
AwaitExpression, | ||
CallExpression, | ||
ImportDeclaration, | ||
Node | ||
} from 'estree' | ||
import { is } from 'unist-util-is' | ||
|
||
/** | ||
* Nodes that can contain assertion module ids. | ||
* | ||
* @internal | ||
*/ | ||
type HasModuleId = AwaitExpression | CallExpression | ImportDeclaration | ||
|
||
/** | ||
* Check if `node` contains an assertion module id. | ||
* | ||
* @see {@linkcode HasModuleId} | ||
* @see {@linkcode Node} | ||
* | ||
* @param {Nilable<Node>} node - Node to check | ||
* @param {(id: unknown) => boolean} filter - Module id filter | ||
* @return {node is HasModuleId} `true` if `node` contains assertion module id | ||
*/ | ||
const hasModuleId = ( | ||
node: Nilable<Node>, | ||
filter: (id: unknown) => boolean | ||
): node is HasModuleId => { | ||
switch (true) { | ||
case is(node, 'AwaitExpression'): | ||
return ( | ||
is(node.argument, 'ImportExpression') && | ||
is(node.argument.source, 'Literal') && | ||
filter(node.argument.source.value) | ||
) | ||
case is(node, 'CallExpression'): | ||
return ( | ||
is(node.callee, 'Identifier') && | ||
node.callee.name === 'require' && | ||
!!node.arguments.length && | ||
is(node.arguments[0], 'Literal') && | ||
filter(node.arguments[0].value) | ||
) | ||
case is(node, 'ImportDeclaration'): | ||
return is(node.source, 'Literal') && filter(node.source.value) | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
export default hasModuleId |
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