Skip to content

Commit 3544c0f

Browse files
committed
eslint-module-utils: Add tests for parserOptions
Refs import-js#839
1 parent b4d75c8 commit 3544c0f

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
- [`no-anonymous-default-export`] rule: report anonymous default exports ([#712], thanks [@duncanbeevers]).
99
- Add new value to [`order`]'s `newlines-between` option to allow newlines inside import groups ([#627], [#628], thanks [@giodamelio])
1010
- Add `count` option to the [`newline-after-import`] rule to allow configuration of number of newlines expected ([#742], thanks [@ntdb])
11+
- Add `filePath` into `parserOptions` passed to `parser` ([#839], thanks [@sompylasar])
1112

1213
### Changed
1314
- [`no-extraneous-dependencies`]: use `read-pkg-up` to simplify finding + loading `package.json` ([#680], thanks [@wtgtybhertgeghgtwtg])

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"debug": "^2.2.0",
8282
"doctrine": "1.5.0",
8383
"eslint-import-resolver-node": "^0.2.0",
84-
"eslint-module-utils": "^2.0.0",
84+
"eslint-module-utils": "^2.1.0",
8585
"has": "^1.0.1",
8686
"lodash.cond": "^4.3.0",
8787
"minimatch": "^3.0.3",

tests/src/core/parse.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as fs from 'fs'
22
import { expect } from 'chai'
33
import parse from 'eslint-module-utils/parse'
44

5-
import { getFilename } from '../utils'
5+
import { getFilename, makeNaiveSpy } from '../utils'
66

77
describe('parse(content, { settings, ecmaFeatures })', function () {
88
const path = getFilename('jsx.js')
@@ -21,4 +21,21 @@ describe('parse(content, { settings, ecmaFeatures })', function () {
2121
.not.to.throw(Error)
2222
})
2323

24+
it('passes expected parserOptions to custom parser', function () {
25+
const parseSpy = makeNaiveSpy()
26+
const parserOptions = { ecmaFeatures: { jsx: true } }
27+
require('./parseStubParser').parse = parseSpy
28+
parse(path, content, { settings: {}, parserPath: require.resolve('./parseStubParser'), parserOptions: parserOptions })
29+
expect(parseSpy.callCount).to.equal(1)
30+
expect(parseSpy.lastCallArguments[0]).to.equal(content)
31+
expect(parseSpy.lastCallArguments[1]).to.be.an('object')
32+
expect(parseSpy.lastCallArguments[1]).to.not.equal(parserOptions)
33+
expect(parseSpy.lastCallArguments[1])
34+
.to.have.property('ecmaFeatures')
35+
.that.is.eql(parserOptions.ecmaFeatures)
36+
.and.is.not.equal(parserOptions.ecmaFeatures)
37+
expect(parseSpy.lastCallArguments[1]).to.have.property('attachComment', true)
38+
expect(parseSpy.lastCallArguments[1]).to.have.property('filePath', path)
39+
})
40+
2441
})

tests/src/core/parseStubParser.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// this stub must be in a separate file to require from parse via moduleRequire
2+
module.exports = {
3+
parse: function () {},
4+
}

tests/src/utils.js

+14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ export function getFilename(file) {
2828
return path.join(__dirname, '..', 'files', file || 'foo.js')
2929
}
3030

31+
/**
32+
* naive implementation of a function spy
33+
* for more robust spy, consider replacing with sinon or chai-spies
34+
* @return {function}
35+
*/
36+
export function makeNaiveSpy() {
37+
const spy = function () {
38+
spy.callCount += 1
39+
spy.lastCallArguments = arguments
40+
}
41+
spy.callCount = 0
42+
return spy
43+
}
44+
3145
/**
3246
* to be added as valid cases just to ensure no nullable fields are going
3347
* to crash at runtinme

utils/CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ All notable changes to this module will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
55

6-
## v2 - 2016-11-07
6+
## v2.1.0 - 2017-05-20
7+
### Added
8+
- `parse` now additionally passes `filePath` to `parser` in `parserOptions` like `eslint` core does
9+
10+
## v2.0.0 - 2016-11-07
711
### Changed
812
- `unambiguous` no longer exposes fast test regex
913

1014
### Fixed
11-
- `unambiguous.test()` regex is now properly in multiline mode
15+
- `unambiguous.test()` regex is now properly in multiline mode

utils/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-module-utils",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Core utilities to support eslint-plugin-import and other module-related plugins.",
55
"engines": {
66
"node": ">=4"

utils/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ exports.default = function parse(path, content, context) {
2121

2222
// always attach comments
2323
parserOptions.attachComment = true
24-
24+
2525
// provide the `filePath` like eslint itself does, in `parserOptions`
26-
// https://github.com/eslint/eslint/blob/3ec436eeed0b0271e2ed0d0cb22e4246eb15f137/lib/linter.js#L637
26+
// https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
2727
parserOptions.filePath = path
2828

2929
// require the parser relative to the main module (i.e., ESLint)

0 commit comments

Comments
 (0)