HAST Property Checker
hast-util-has-property is a focused utility within the unified (specifically HAST) ecosystem, designed to ascertain if a given HAST (Hypertext Abstract Syntax Tree) element node possesses a specified property. The current stable version is 3.0.0. This package is maintained by the syntax-tree collective, aligning its release cadence with Node.js LTS versions and the broader `unified` project's evolution, frequently releasing new major versions to adopt modern JavaScript features like ESM. Its primary differentiation lies in its tight integration with the HAST specification, providing a type-safe (TypeScript-enabled) and robust method for property checking, crucial for HTML processing pipelines. It is a highly specialized tool, typically used by developers working directly with HAST manipulation, offering a reliable primitive for conditional logic based on element properties in transform plugins. It strictly operates on valid HAST `Node` objects, particularly `Element` types, providing a precise `boolean` result.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module ... hast-util-has-property.js from ... not supported.
cause Attempting to use CommonJS `require()` to import an ESM-only package.fixChange `const hasProperty = require('hast-util-has-property')` to `import { hasProperty } from 'hast-util-has-property'`. Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`). -
TypeError: hasProperty is not a function
cause Incorrect import syntax, often trying to import a named export as a default, or attempting to `require()` an ESM module and access a non-existent default property.fixUse the correct named import: `import { hasProperty } from 'hast-util-has-property'`. Verify that no default import syntax is used. -
TypeError: node is not a valid HAST node
cause Passing an invalid `node` argument (e.g., `null`, `undefined`, or a non-HAST object) to `hasProperty` after the v3.0.0 update, which enforced stricter input validation.fixEnsure that the `node` argument supplied to `hasProperty` is always a well-formed HAST `Node` object, as expected by the utility.
Warnings
- breaking Version 3.0.0 of `hast-util-has-property` requires Node.js 16 or newer. Older Node.js versions are no longer supported.
- breaking This package is ESM-only since v2.0.0 and further refined with 'exports' in v3.0.0. CommonJS `require()` statements will no longer work and will result in errors.
- breaking Version 3.0.0 removes support for passing non-Node or non-name types as arguments to `hasProperty`. The function now expects valid HAST `Node` objects for the first argument and a string for the second.
- gotcha The package only exports a named export `hasProperty`. There is no default export. Attempting to import it as a default will fail.
Install
-
npm install hast-util-has-property -
yarn add hast-util-has-property -
pnpm add hast-util-has-property
Imports
- hasProperty
const hasProperty = require('hast-util-has-property')import { hasProperty } from 'hast-util-has-property' - hasProperty
import { hasProperty } from 'https://esm.sh/hast-util-has-property@3'
Quickstart
import {hasProperty} from 'hast-util-has-property'
// Example 1: Text node has no properties, so 'bravo' is false.
hasProperty({type: 'text', value: 'alpha'}, 'bravo') // => false
// Example 2: Element node without 'className' property.
hasProperty(
{
type: 'element',
tagName: 'div',
properties: {id: 'bravo'},
children: []
},
'className'
) // => false
// Example 3: Element node with 'id' property.
hasProperty(
{
type: 'element',
tagName: 'div',
properties: {id: 'charlie'},
children: []
},
'id'
) // => true
console.log('Checked properties successfully.')