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

Commit aac20ac

Browse files
authored
Merge pull request #112 from styled-components/import-name
Add moduleName option
2 parents 6984963 + 6b3d20c commit aac20ac

11 files changed

+188
-18
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ Now use those in your `.stylelintrc` and run stylelint with your JavaScript file
5252
- [sc-custom](https://www.styled-components.com/docs/tooling#sc-custom)
5353
- [Syntax Notes](https://www.styled-components.com/docs/tooling#syntax-notes)
5454

55-
5655
## License
5756

5857
Licensed under the MIT License, Copyright © 2017 Maximilian Stoiber. See [LICENSE.md](./LICENSE.md) for more information!

package-lock.json

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ const path = require('path')
22
const parse = require('./parsers/index')
33

44
const sourceMapsCorrections = {}
5+
const DEFAULT_OPTIONS = {
6+
moduleName: 'styled-components'
7+
}
58

6-
module.exports = (/* options */) => ({
9+
module.exports = options => ({
710
// Get string for stylelint to lint
811
code(input, filepath) {
912
const absolutePath = path.resolve(process.cwd(), filepath)
1013
sourceMapsCorrections[absolutePath] = {}
11-
const { extractedCSS, sourceMap } = parse(input, absolutePath)
14+
const { extractedCSS, sourceMap } = parse(
15+
input,
16+
absolutePath,
17+
Object.assign({}, DEFAULT_OPTIONS, options)
18+
)
1219
// Save source location, merging existing corrections with current corrections
1320
sourceMapsCorrections[absolutePath] = Object.assign(
1421
sourceMapsCorrections[absolutePath],

src/parsers/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const getTTLContent = require('../utils/tagged-template-literal.js').getTaggedTe
1717
const parseImports = require('../utils/parse').parseImports
1818
const getSourceMap = require('../utils/parse').getSourceMap
1919

20-
const processStyledComponentsFile = (ast, absolutePath) => {
20+
const processStyledComponentsFile = (ast, absolutePath, options) => {
2121
const extractedCSS = []
2222
let ignoreRuleComments = []
2323
let importedNames = {
@@ -37,7 +37,7 @@ const processStyledComponentsFile = (ast, absolutePath) => {
3737
}
3838
})
3939
}
40-
if (isStyledImport(node)) {
40+
if (isStyledImport(node, options.moduleName)) {
4141
importedNames = parseImports(node)
4242
return
4343
}
@@ -90,7 +90,7 @@ const processStyledComponentsFile = (ast, absolutePath) => {
9090
}
9191
}
9292

93-
module.exports = (input, absolutePath) => {
93+
module.exports = (input, absolutePath, options) => {
9494
let ast = null
9595
if (absolutePath.endsWith('.ts') || absolutePath.endsWith('.tsx')) {
9696
// We import it dynamically in order to be able to not include typescript as a dependency
@@ -101,5 +101,5 @@ module.exports = (input, absolutePath) => {
101101
} else {
102102
ast = estreeParse(input)
103103
}
104-
return processStyledComponentsFile(ast, absolutePath)
104+
return processStyledComponentsFile(ast, absolutePath, options)
105105
}

src/utils/styled.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
const path = require('path')
12
const isTaggedTemplateLiteral = require('./tagged-template-literal').isTaggedTemplateLiteral
23

34
/**
45
* Check if something is a styled-components import declaration
56
*/
6-
const isStyledImport = node =>
7-
node.type === 'ImportDeclaration' && node.source.value === 'styled-components'
7+
const isStyledImport = (node, moduleName) =>
8+
node.type === 'ImportDeclaration' && path.basename(node.source.value) === moduleName
89

910
/**
1011
* Check if something is a styled shorthand call
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import something from 'some-lib'
2+
3+
4+
// Empty block, but moduleName isn't set to some-lib
5+
// so shouldn't be an error
6+
const Button = something.div`
7+
8+
`

test/fixtures/options/module-name.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import emotion from 'emotion'
2+
3+
// ⚠️ EMPTY BLOCK ⚠️
4+
const Button = emotion.div`
5+
6+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import emotion from '../../emotion'
2+
3+
// ⚠️ EMPTY BLOCK ⚠️
4+
const Button = emotion.div`
5+
6+
`

test/fixtures/simple/other-library.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import styled from 'some-other-lib'
2+
3+
// ⚠️ BAD INDENTATION ⚠️
4+
const Button2 = styled.button`
5+
color: blue;
6+
`

test/options.test.js

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const stylelint = require('stylelint')
2+
const path = require('path')
3+
4+
const processor = path.join(__dirname, '../src/index.js')
5+
const rules = {
6+
'block-no-empty': true,
7+
indentation: 2,
8+
'rule-empty-line-before': [
9+
'always-multi-line',
10+
{
11+
except: ['first-nested'],
12+
ignore: ['after-comment']
13+
}
14+
],
15+
'selector-type-no-unknown': true
16+
}
17+
18+
describe('options', () => {
19+
let fixture
20+
let data
21+
22+
// NOTE beforeEach() runs _after_ the beforeAll() hooks of the describe() blocks, so `fixture`
23+
// will have the right path
24+
beforeEach(done => {
25+
stylelint
26+
.lint({
27+
files: [fixture],
28+
syntax: 'scss',
29+
config: {
30+
// Set moduleName option to "emotion"
31+
processors: [[processor, { moduleName: 'emotion' }]],
32+
rules
33+
}
34+
})
35+
.then(result => {
36+
data = result
37+
done()
38+
})
39+
.catch(err => {
40+
console.log(err)
41+
data = err
42+
done()
43+
})
44+
})
45+
46+
describe('moduleName', () => {
47+
beforeAll(() => {
48+
fixture = path.join(__dirname, './fixtures/options/module-name.js')
49+
})
50+
51+
it('should have one result', () => {
52+
expect(data.results.length).toEqual(1)
53+
})
54+
55+
it('should use the right file', () => {
56+
expect(data.results[0].source).toEqual(fixture)
57+
})
58+
59+
it('should have errored', () => {
60+
expect(data.results[0].errored).toEqual(true)
61+
})
62+
63+
it('should have one warning (i.e. wrong lines of code)', () => {
64+
expect(data.results[0].warnings.length).toEqual(1)
65+
})
66+
67+
it('should have a block-no-empty as the first warning', () => {
68+
expect(data.results[0].warnings[0].rule).toEqual('block-no-empty')
69+
})
70+
})
71+
72+
describe('relative moduleName', () => {
73+
beforeAll(() => {
74+
fixture = path.join(__dirname, './fixtures/options/relative-module-name.js')
75+
})
76+
77+
it('should have one result', () => {
78+
expect(data.results.length).toEqual(1)
79+
})
80+
81+
it('should use the right file', () => {
82+
expect(data.results[0].source).toEqual(fixture)
83+
})
84+
85+
it('should have errored', () => {
86+
expect(data.results[0].errored).toEqual(true)
87+
})
88+
89+
it('should have one warning (i.e. wrong lines of code)', () => {
90+
expect(data.results[0].warnings.length).toEqual(1)
91+
})
92+
93+
it('should have a block-no-empty as the first warning', () => {
94+
expect(data.results[0].warnings[0].rule).toEqual('block-no-empty')
95+
})
96+
})
97+
98+
describe('invalid moduleName', () => {
99+
beforeAll(() => {
100+
fixture = path.join(__dirname, './fixtures/options/invalid-module-name.js')
101+
})
102+
103+
it('should have one result', () => {
104+
expect(data.results.length).toEqual(1)
105+
})
106+
107+
it('should use the right file', () => {
108+
expect(data.results[0].source).toEqual(fixture)
109+
})
110+
111+
it('should not have errored', () => {
112+
expect(data.results[0].errored).toEqual(undefined)
113+
})
114+
})
115+
})

test/simple.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,28 @@ describe('simple', () => {
198198
})
199199
})
200200

201+
describe('other library', () => {
202+
beforeAll(() => {
203+
fixture = path.join(__dirname, './fixtures/simple/other-library.js')
204+
})
205+
206+
it('should have one result', () => {
207+
expect(data.results.length).toEqual(1)
208+
})
209+
210+
it('should use the right file', () => {
211+
expect(data.results[0].source).toEqual(fixture)
212+
})
213+
214+
it('should have errored', () => {
215+
expect(data.errored).toEqual(true)
216+
})
217+
218+
it('should have 1 warning', () => {
219+
expect(data.results[0].warnings.length).toEqual(1)
220+
})
221+
})
222+
201223
describe('identify styled', () => {
202224
beforeAll(() => {
203225
fixture = path.join(__dirname, './fixtures/simple/identify-styled.js')

0 commit comments

Comments
 (0)