Skip to content

Commit 996bc58

Browse files
authored
Use picomatch instead of regex (#369)
* Use picomatch instead of regex * Fix test
1 parent 16e59a8 commit 996bc58

File tree

8 files changed

+16
-10
lines changed

8 files changed

+16
-10
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"@babel/helper-annotate-as-pure": "^7.16.0",
3535
"@babel/helper-module-imports": "^7.16.0",
3636
"babel-plugin-syntax-jsx": "^6.18.0",
37-
"lodash": "^4.17.11"
37+
"lodash": "^4.17.11",
38+
"picomatch": "^2.3.0"
3839
},
3940
"resolutions": {
4041
"babel-core": "7.0.0-bridge.0"

src/utils/detectors.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import escapeRegExp from 'lodash/escapeRegExp'
21
import { useTopLevelImportPathMatchers } from './options'
32

43
const VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS = [
54
'styled-components',
65
'styled-components/no-tags',
76
'styled-components/native',
87
'styled-components/primitives',
9-
].map(literal => new RegExp(`^${escapeRegExp(literal)}$`))
8+
].map(literal => x => x === literal);
109

1110
export const isValidTopLevelImport = (x, state) => {
1211
return [
1312
...VALID_TOP_LEVEL_IMPORT_PATH_MATCHERS,
1413
...useTopLevelImportPathMatchers(state),
15-
].some(re => re.test(x));
14+
].some(isMatch => isMatch(x));
1615
}
1716

1817
const localNameCache = {}

src/utils/options.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pm from 'picomatch';
2+
13
function getOption({ opts }, name, defaultValue = true) {
24
return opts[name] === undefined || opts[name] === null
35
? defaultValue
@@ -6,7 +8,7 @@ function getOption({ opts }, name, defaultValue = true) {
68

79
export const useDisplayName = state => getOption(state, 'displayName')
810
export const useTopLevelImportPathMatchers = state =>
9-
getOption(state, 'topLevelImportPaths', []).map(pattern => new RegExp(pattern))
11+
getOption(state, 'topLevelImportPaths', []).map(pattern => pm(pattern))
1012
export const useSSR = state => getOption(state, 'ssr', true)
1113
export const useFileName = state => getOption(state, 'fileName')
1214
export const useMeaninglessFileNames = state => getOption(state, 'meaninglessFileNames', ['index'])

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$"],
9+
"topLevelImportPaths": ["*(../)**/example"],
1010
"transpileTemplateLiterals": false
1111
}
1212
]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"ssr": true,
99
"topLevelImportPaths": [
1010
"@xstyled/styled-components",
11-
"@xstyled/styled-components/*"
11+
"@xstyled/styled-components/*",
1212
],
1313
"transpileTemplateLiterals": false
1414
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import styled from '@xstyled/styled-components/test'
1+
import styled from '@xstyled/styled-components'
2+
import unstyled from '@xstyled/styled-components-test'
23

34
const Test = styled.div`
45
width: 100%;
@@ -8,3 +9,4 @@ const styles = { One: styled.div`` }
89
let Component
910
Component = styled.div``
1011
const WrappedComponent = styled(Inner)``
12+
const NoTransformComponent = unstyled.div``;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import styled from '@xstyled/styled-components/test';
1+
import styled from '@xstyled/styled-components';
2+
import unstyled from '@xstyled/styled-components-test';
23
const Test = styled.div.withConfig({
34
componentId: "sc-1mlyrvc-0"
45
})`width:100%;`;
@@ -19,3 +20,4 @@ Component = styled.div.withConfig({
1920
const WrappedComponent = styled(Inner).withConfig({
2021
componentId: "sc-1mlyrvc-5"
2122
})``;
23+
const NoTransformComponent = unstyled.div``;

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3365,7 +3365,7 @@ picocolors@^1.0.0:
33653365
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
33663366
integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==
33673367

3368-
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3:
3368+
picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3, picomatch@^2.3.0:
33693369
version "2.3.0"
33703370
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.0.tgz#f1f061de8f6a4bf022892e2d128234fb98302972"
33713371
integrity sha512-lY1Q/PiJGC2zOv/z391WOTD+Z02bCgsFfvxoXXf6h7kv9o+WmsmzYqrAwY63sNgOxE4xEdq0WyUnXfKeBrSvYw==

0 commit comments

Comments
 (0)