Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Support to specify parser plugins #229

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/parsers/babylon-parser.js
Original file line number Diff line number Diff line change
@@ -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'
]
)
})
2 changes: 1 addition & 1 deletion src/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
50 changes: 49 additions & 1 deletion test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('options', () => {
.lint({
files: [fixture],
config: {
// Set moduleName option to "emotion"
// Set importName option to "notDefault"
processors: [[processor, { importName: 'notDefault' }]],
rules
}
Expand Down Expand Up @@ -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)
})
})
})