Sha256: 24d9d4ffbead79273f0765735e80e24792e69e67eea10b0dbfb96d5877667a41

Contents?: true

Size: 1.7 KB

Versions: 37

Compression:

Stored size: 1.7 KB

Contents

"use strict"
exports.__esModule = true

const moduleRequire = require('./module-require').default
const extname = require('path').extname

const log = require('debug')('eslint-plugin-import:parse')

exports.default = function parse(path, content, context) {

  if (context == null) throw new Error('need context to parse properly')

  let parserOptions = context.parserOptions
  const parserPath = getParserPath(path, context)

  if (!parserPath) throw new Error('parserPath is required!')

  // hack: espree blows up with frozen options
  parserOptions = Object.assign({}, parserOptions)
  parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)

  // always include comments and tokens (for doc parsing)
  parserOptions.comment = true
  parserOptions.attachComment = true  // keeping this for backward-compat with  older parsers
  parserOptions.tokens = true

  // attach node locations
  parserOptions.loc = true
  parserOptions.range = true

  // provide the `filePath` like eslint itself does, in `parserOptions`
  // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  parserOptions.filePath = path

  // require the parser relative to the main module (i.e., ESLint)
  const parser = moduleRequire(parserPath)

  return parser.parse(content, parserOptions)
}

function getParserPath(path, context) {
  const parsers = context.settings['import/parsers']
  if (parsers != null) {
    const extension = extname(path)
    for (let parserPath in parsers) {
      if (parsers[parserPath].indexOf(extension) > -1) {
        // use this alternate parser
        log('using alt parser:', parserPath)
        return parserPath
      }
    }
  }
  // default to use ESLint parser
  return context.parserPath
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
govuk_publishing_components-17.17.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.16.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.15.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.14.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.13.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.12.2 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.12.1 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.12.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.11.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.10.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.9.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.8.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.7.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.6.1 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.6.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.5.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.4.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.3.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.2.0 node_modules/eslint-module-utils/parse.js
govuk_publishing_components-17.1.1 node_modules/eslint-module-utils/parse.js