2.0.1 • Published 2 months ago

@bojavou/acorn-import-attributes v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

@bojavou/acorn-import-attributes

Import attributes for acorn.

Parses to the stage3 Import Attributes ESTree extension.

Supports both the with and deprecated assert keywords. Both are parsed to the same tree structure. A property attributesKeyword is added to expose which keyword was present in the source.

Usage

npm install @bojavou/acorn-import-attributes
import * as acorn from 'acorn'
import importAttributes from '@bojavou/acorn-import-attributes'

const Parser = acorn.Parser.extend(
  importAttributes({ with: true, assert: true })
)

const source = `
import config from 'config.json' with { type: 'json' }
`.trim()
const tree = Parser.parse(source, {
  ecmaVersion: 'latest',
  sourceType: 'module'
})
{
  type: 'Program',
  body: [{
    type: 'ImportDeclaration',
    specifiers: [..],
    source: ..,
    attributesKeyword: 'with',
    attributes: [{
      type: 'ImportAttribute',
      key: { type: 'Identifier', name: 'type' },
      value: { type: 'Literal', value: 'json' }
    }]
  }]
}

API

importAttributes(options)

The default export is a factory function which produces a plugin. Pass an options object to configure which keywords to recognize.

Options

NameTypeDescriptionDefault
withbooleanRecognize with as an import attributes keyword.true
assertbooleanRecognize assert as an import attributes keyword.false

ESTree

Extensions to the Import Attributes ESTree structure. attributesKeyword is null when no attributes are present.

ImportDeclaration

extend interface ImportDeclaration {
    attributesKeyword: "with" | "assert" | null;
}

ExportNamedDeclaration

extend interface ExportNamedDeclaration {
    attributesKeyword: "with" | "assert" | null;
}

ExportAllDeclaration

extend interface ExportAllDeclaration {
    attributesKeyword: "with" | "assert" | null;
}
2.0.1

2 months ago

2.0.0

2 months ago

1.0.1

10 months ago

1.0.0

10 months ago