Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Just always use regexp
  • Loading branch information
Tyler Rockwood committed Oct 19, 2021
commit c612dab133fd0443c72bbdf34d00767b8a015d8b
25 changes: 8 additions & 17 deletions src/utils/detectors.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import {
useTopLevelImportPaths,
useTopLevelImportPathPatterns,
} from './options'
import escapeRegExp from 'lodash/escapeRegExp'
import { useTopLevelImportPathMatchers } from './options'

const VALID_TOP_LEVEL_IMPORT_PATHS = [
const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
'styled-components',
'styled-components/no-tags',
'styled-components/native',
'styled-components/primitives',
]
].map(literal => new RegExp(`^${escapeRegExp(literal)}$`))

export const isValidTopLevelImport = (x, state) => {
const isValid = [
...VALID_TOP_LEVEL_IMPORT_PATHS,
...useTopLevelImportPaths(state),
].includes(x)
if (isValid) return true
for (const pattern of useTopLevelImportPathPatterns(state)) {
if (pattern.test(x)) {
return x
}
}
return false
return [
...VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS,
...useTopLevelImportPathMatchers(state),
].some(re => re.test(x));
}

const localNameCache = {}
Expand Down
8 changes: 2 additions & 6 deletions src/utils/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ function getOption({ opts }, name, defaultValue = true) {
}

export const useDisplayName = state => getOption(state, 'displayName')
export const useTopLevelImportPaths = state =>
getOption(state, 'topLevelImportPaths', [])
export const useTopLevelImportPathPatterns = state =>
getOption(state, 'topLevelImportPathPatterns', []).map(
pattern => new RegExp(pattern)
)
export const useTopLevelImportPathMatchers = state =>
getOption(state, 'topLevelImportPaths', []).map(pattern => new RegExp(pattern))
export const useSSR = state => getOption(state, 'ssr', true)
export const useFileName = state => getOption(state, 'fileName')
export const useMinify = state => getOption(state, 'minify')
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"displayName": true,
"fileName": false,
"ssr": true,
"topLevelImportPaths": ["@example/example"],
"topLevelImportPaths": [".*\/example$"],
"transpileTemplateLiterals": false
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { styled } from '@example/example'
import { styled } from '../../relative/example'

const Test = styled.div`
width: 100%;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { styled } from '@example/example';
import { styled } from '../../relative/example';
const Test = styled.div.withConfig({
displayName: "Test",
componentId: "sc-elhbfv-0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"ssr": true,
"topLevelImportPaths": [
"@xstyled/styled-components",
"@xstyled/styled-components/no-tags",
"@xstyled/styled-components/native",
"@xstyled/styled-components/primitives"
"@xstyled/styled-components/*"
],
"transpileTemplateLiterals": false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from '@xstyled/styled-components'
import styled from '@xstyled/styled-components/test'

const Test = styled.div`
width: 100%;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import styled from '@xstyled/styled-components';
import styled from '@xstyled/styled-components/test';
const Test = styled.div.withConfig({
componentId: "sc-1mlyrvc-0"
})`width:100%;`;
Expand Down