Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/eslint-plugin-import
Path: blob/main/tests/src/core/parse.js
829 views
1
import * as fs from 'fs';
2
import { expect } from 'chai';
3
import sinon from 'sinon';
4
import parse from 'eslint-module-utils/parse';
5
6
import { getFilename } from '../utils';
7
8
describe('parse(content, { settings, ecmaFeatures })', function () {
9
const path = getFilename('jsx.js');
10
const parseStubParser = require('./parseStubParser');
11
const parseStubParserPath = require.resolve('./parseStubParser');
12
const eslintParser = require('./eslintParser');
13
const eslintParserPath = require.resolve('./eslintParser');
14
let content;
15
16
before((done) =>
17
fs.readFile(path, { encoding: 'utf8' },
18
(err, f) => { if (err) { done(err); } else { content = f; done(); }}));
19
20
it('doesn\'t support JSX by default', function () {
21
expect(() => parse(path, content, { parserPath: 'espree' })).to.throw(Error);
22
});
23
24
it('infers jsx from ecmaFeatures when using stock parser', function () {
25
expect(() => parse(path, content, { settings: {}, parserPath: 'espree', parserOptions: { ecmaVersion: 2015, sourceType: 'module', ecmaFeatures: { jsx: true } } }))
26
.not.to.throw(Error);
27
});
28
29
it('passes expected parserOptions to custom parser', function () {
30
const parseSpy = sinon.spy();
31
const parserOptions = { ecmaFeatures: { jsx: true } };
32
parseStubParser.parse = parseSpy;
33
parse(path, content, { settings: {}, parserPath: parseStubParserPath, parserOptions });
34
expect(parseSpy.callCount, 'custom parser to be called once').to.equal(1);
35
expect(parseSpy.args[0][0], 'custom parser to get content as its first argument').to.equal(content);
36
expect(parseSpy.args[0][1], 'custom parser to get an object as its second argument').to.be.an('object');
37
expect(parseSpy.args[0][1], 'custom parser to clone the parserOptions object').to.not.equal(parserOptions);
38
expect(parseSpy.args[0][1], 'custom parser to get ecmaFeatures in parserOptions which is a clone of ecmaFeatures passed in')
39
.to.have.property('ecmaFeatures')
40
.that.is.eql(parserOptions.ecmaFeatures)
41
.and.is.not.equal(parserOptions.ecmaFeatures);
42
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.attachComment equal to true').to.have.property('attachComment', true);
43
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.tokens equal to true').to.have.property('tokens', true);
44
expect(parseSpy.args[0][1], 'custom parser to get parserOptions.range equal to true').to.have.property('range', true);
45
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);
46
});
47
48
it('passes with custom `parseForESLint` parser', function () {
49
const parseForESLintSpy = sinon.spy(eslintParser, 'parseForESLint');
50
const parseSpy = sinon.spy();
51
eslintParser.parse = parseSpy;
52
parse(path, content, { settings: {}, parserPath: eslintParserPath });
53
expect(parseForESLintSpy.callCount, 'custom `parseForESLint` parser to be called once').to.equal(1);
54
expect(parseSpy.callCount, '`parseForESLint` takes higher priority than `parse`').to.equal(0);
55
});
56
57
it('throws on context == null', function () {
58
expect(parse.bind(null, path, content, null)).to.throw(Error);
59
});
60
61
it('throws on unable to resolve parserPath', function () {
62
expect(parse.bind(null, path, content, { settings: {}, parserPath: null })).to.throw(Error);
63
});
64
65
it('takes the alternate parser specified in settings', function () {
66
const parseSpy = sinon.spy();
67
const parserOptions = { ecmaFeatures: { jsx: true } };
68
parseStubParser.parse = parseSpy;
69
expect(parse.bind(null, path, content, { settings: { 'import/parsers': { [parseStubParserPath]: [ '.js' ] } }, parserPath: null, parserOptions })).not.to.throw(Error);
70
expect(parseSpy.callCount, 'custom parser to be called once').to.equal(1);
71
});
72
});
73
74