Skip to content

Commit

Permalink
eslint-module-utils: filePath in parserOptions (#840)
Browse files Browse the repository at this point in the history
* eslint-module-utils: filePath in parserOptions

Refs #839

* eslint-module-utils: Add tests for parserOptions

Refs #839

* eslint-module-utils: Reverted manual version bumps.

Refs #839

* Add sinon, replace eslint-module-utils test spy with sinon.spy

* Fix CHANGELOG merge error

* eslint-module-utils: Add more tests for parse (coverage 100%)

* eslint-module-utils: In tests move require stub parser to the top.
  • Loading branch information
sompylasar authored and benmosher committed May 31, 2017
1 parent 2f690b4 commit 28e1623
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]

### Added
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])

## [2.3.0] - 2017-05-18
### Added
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"nyc": "^8.3.0",
"redux": "^3.0.4",
"rimraf": "2.5.2",
"sinon": "^2.3.2",
"typescript": "^2.0.3",
"typescript-eslint-parser": "^2.1.0"
},
Expand Down
36 changes: 36 additions & 0 deletions tests/src/core/parse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as fs from 'fs'
import { expect } from 'chai'
import sinon from 'sinon'
import parse from 'eslint-module-utils/parse'

import { getFilename } from '../utils'

describe('parse(content, { settings, ecmaFeatures })', function () {
const path = getFilename('jsx.js')
const parseStubParser = require('./parseStubParser')
const parseStubParserPath = require.resolve('./parseStubParser')
let content

before((done) =>
Expand All @@ -21,4 +24,37 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
.not.to.throw(Error)
})

it('passes expected parserOptions to custom parser', function () {
const parseSpy = sinon.spy()
const parserOptions = { ecmaFeatures: { jsx: true } }
parseStubParser.parse = parseSpy
parse(path, content, { settings: {}, parserPath: parseStubParserPath, parserOptions: parserOptions })
expect(parseSpy.callCount, 'custom parser to be called once').to.equal(1)
expect(parseSpy.args[0][0], 'custom parser to get content as its first argument').to.equal(content)
expect(parseSpy.args[0][1], 'custom parser to get an object as its second argument').to.be.an('object')
expect(parseSpy.args[0][1], 'custom parser to clone the parserOptions object').to.not.equal(parserOptions)
expect(parseSpy.args[0][1], 'custom parser to get ecmaFeatures in parserOptions which is a clone of ecmaFeatures passed in')
.to.have.property('ecmaFeatures')
.that.is.eql(parserOptions.ecmaFeatures)
.and.is.not.equal(parserOptions.ecmaFeatures)
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.attachComment equal to true').to.have.property('attachComment', true)
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.filePath equal to the full path of the source file').to.have.property('filePath', path)
})

it('should throw on context == null', function () {
expect(parse.bind(null, path, content, null)).to.throw(Error)
})

it('should throw on unable to resolve parserPath', function () {
expect(parse.bind(null, path, content, { settings: {}, parserPath: null })).to.throw(Error)
})

it('should take the alternate parser specified in settings', function () {
const parseSpy = sinon.spy()
const parserOptions = { ecmaFeatures: { jsx: true } }
parseStubParser.parse = parseSpy
expect(parse.bind(null, path, content, { settings: { 'import/parsers': { [parseStubParserPath]: [ '.js' ] } }, parserPath: null, parserOptions: parserOptions })).not.to.throw(Error)
expect(parseSpy.callCount, 'custom parser to be called once').to.equal(1)
})

})
4 changes: 4 additions & 0 deletions tests/src/core/parseStubParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this stub must be in a separate file to require from parse via moduleRequire
module.exports = {
parse: function () {},
}
8 changes: 6 additions & 2 deletions utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ All notable changes to this module will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## v2 - 2016-11-07
## [Unreleased]
### Added
- `parse` now additionally passes `filePath` to `parser` in `parserOptions` like `eslint` core does

## v2.0.0 - 2016-11-07
### Changed
- `unambiguous` no longer exposes fast test regex

### Fixed
- `unambiguous.test()` regex is now properly in multiline mode
- `unambiguous.test()` regex is now properly in multiline mode
4 changes: 4 additions & 0 deletions utils/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ exports.default = function parse(path, content, context) {
// always attach comments
parserOptions.attachComment = 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)

Expand Down

0 comments on commit 28e1623

Please sign in to comment.