From d27a9e3edf8eff410841ed5ce415cded9ab1b8d4 Mon Sep 17 00:00:00 2001 From: Xianming Zhong Date: Wed, 3 Oct 2018 00:02:27 +0800 Subject: [PATCH] support options.parserPlugins --- src/parsers/babylon-parser.js | 31 +++++++++++----------- src/parsers/index.js | 2 +- test/options.test.js | 48 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/parsers/babylon-parser.js b/src/parsers/babylon-parser.js index 92dcf8b..55c1799 100644 --- a/src/parsers/babylon-parser.js +++ b/src/parsers/babylon-parser.js @@ -1,20 +1,21 @@ const babylon = require('@babel/parser') -module.exports = type => input => +module.exports = (type, plugins) => input => babylon.parse(input, { sourceType: 'module', - plugins: [ - 'jsx', - type, - 'objectRestSpread', - ['decorators', { decoratorsBeforeExport: true }], - 'classProperties', - 'exportExtensions', - 'asyncGenerators', - 'functionBind', - 'functionSent', - 'dynamicImport', - 'optionalCatchBinding', - 'optionalChaining' - ] + plugins: [type].concat( + plugins || [ + 'jsx', + 'objectRestSpread', + ['decorators', { decoratorsBeforeExport: true }], + 'classProperties', + 'exportExtensions', + 'asyncGenerators', + 'functionBind', + 'functionSent', + 'dynamicImport', + 'optionalCatchBinding', + 'optionalChaining' + ] + ) }) diff --git a/src/parsers/index.js b/src/parsers/index.js index fd17213..713a3c3 100644 --- a/src/parsers/index.js +++ b/src/parsers/index.js @@ -109,6 +109,6 @@ module.exports = (input, absolutePath, options) => { const typedParser = absolutePath.endsWith('.ts') || absolutePath.endsWith('.tsx') ? 'typescript' : 'flow' - const ast = estreeParse(typedParser)(input) + const ast = estreeParse(typedParser, options.parserPlugins)(input) return processStyledComponentsFile(ast, absolutePath, options) } diff --git a/test/options.test.js b/test/options.test.js index 5d2fbba..b3ae3c6 100644 --- a/test/options.test.js +++ b/test/options.test.js @@ -183,4 +183,52 @@ describe('options', () => { }) }) }) + + describe('parserPlugins', () => { + // NOTE beforeEach() runs _after_ the beforeAll() hooks of the describe() blocks, so `fixture` + // will have the right path + beforeEach(done => { + const plugins = [ + 'jsx', + 'objectRestSpread', + ['decorators', { decoratorsBeforeExport: true }], + 'classProperties', + 'exportExtensions', + 'asyncGenerators', + 'functionBind', + 'functionSent', + 'dynamicImport', + 'optionalCatchBinding', + 'optionalChaining', + // Enable experimental feature + 'exportDefaultFrom' + ] + + stylelint + .lint({ + code: "export Container from './Container';", + config: { + processors: [[processor, { parserPlugins: plugins }]], + rules + } + }) + .then(result => { + data = result + done() + }) + .catch(err => { + console.log(err) + data = err + done() + }) + }) + + it('should have one result', () => { + expect(data.results.length).toEqual(1) + }) + + it('should have not errored', () => { + expect(data.results[0].errored).toEqual(undefined) + }) + }) })