Skip to content

Commit c612dab

Browse files
author
Tyler Rockwood
committed
Just always use regexp
1 parent d291ea3 commit c612dab

File tree

14 files changed

+16
-127
lines changed

14 files changed

+16
-127
lines changed

src/utils/detectors.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
1-
import {
2-
useTopLevelImportPaths,
3-
useTopLevelImportPathPatterns,
4-
} from './options'
1+
import escapeRegExp from 'lodash/escapeRegExp'
2+
import { useTopLevelImportPathMatchers } from './options'
53

6-
const VALID_TOP_LEVEL_IMPORT_PATHS = [
4+
const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
75
'styled-components',
86
'styled-components/no-tags',
97
'styled-components/native',
108
'styled-components/primitives',
11-
]
9+
].map(literal => new RegExp(`^${escapeRegExp(literal)}$`))
1210

1311
export const isValidTopLevelImport = (x, state) => {
14-
const isValid = [
15-
...VALID_TOP_LEVEL_IMPORT_PATHS,
16-
...useTopLevelImportPaths(state),
17-
].includes(x)
18-
if (isValid) return true
19-
for (const pattern of useTopLevelImportPathPatterns(state)) {
20-
if (pattern.test(x)) {
21-
return x
22-
}
23-
}
24-
return false
12+
return [
13+
...VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS,
14+
...useTopLevelImportPathMatchers(state),
15+
].some(re => re.test(x));
2516
}
2617

2718
const localNameCache = {}

src/utils/options.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ function getOption({ opts }, name, defaultValue = true) {
55
}
66

77
export const useDisplayName = state => getOption(state, 'displayName')
8-
export const useTopLevelImportPaths = state =>
9-
getOption(state, 'topLevelImportPaths', [])
10-
export const useTopLevelImportPathPatterns = state =>
11-
getOption(state, 'topLevelImportPathPatterns', []).map(
12-
pattern => new RegExp(pattern)
13-
)
8+
export const useTopLevelImportPathMatchers = state =>
9+
getOption(state, 'topLevelImportPaths', []).map(pattern => new RegExp(pattern))
1410
export const useSSR = state => getOption(state, 'ssr', true)
1511
export const useFileName = state => getOption(state, 'fileName')
1612
export const useMinify = state => getOption(state, 'minify')

test/fixtures/add-identifier-with-top-level-import-path-patterns-and-named-import/.babelrc

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-path-patterns-and-named-import/code.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-path-patterns-and-named-import/output.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-path-patterns/.babelrc

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-path-patterns/code.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-path-patterns/output.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

test/fixtures/add-identifier-with-top-level-import-paths-and-named-import/.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"displayName": true,
77
"fileName": false,
88
"ssr": true,
9-
"topLevelImportPaths": ["@example/example"],
9+
"topLevelImportPaths": [".*\/example$"],
1010
"transpileTemplateLiterals": false
1111
}
1212
]

test/fixtures/add-identifier-with-top-level-import-paths-and-named-import/code.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { styled } from '@example/example'
1+
import { styled } from '../../relative/example'
22

33
const Test = styled.div`
44
width: 100%;

0 commit comments

Comments
 (0)