Giter Club home page Giter Club logo

acorn-import-attributes's Introduction

@bojavou/acorn-import-attributes

Import attributes for acorn.

Parses to the experimental 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

Name Type Description Default
with boolean Recognize with as an import attributes keyword. true
assert boolean Recognize 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;
}

acorn-import-attributes's People

Contributors

bojavou avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.