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

Commit

Permalink
Merge pull request #229 from styled-components/parser-options
Browse files Browse the repository at this point in the history
Support to specify parser plugins
  • Loading branch information
chinesedfan authored Oct 4, 2018
2 parents 3ca7c63 + d27a9e3 commit 5e1485b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
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)
})
})
})

0 comments on commit 5e1485b

Please sign in to comment.