Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "meaninglessFileNames" option #304

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/utils/options.js
Expand Up @@ -9,6 +9,7 @@ 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 useMeaninglessFileNames = state => getOption(state, 'meaninglessFileNames', ['index'])
export const useMinify = state => getOption(state, 'minify')
export const useTranspileTemplateLiterals = state =>
getOption(state, 'transpileTemplateLiterals')
Expand Down
12 changes: 7 additions & 5 deletions src/visitors/displayNameAndId.js
Expand Up @@ -4,6 +4,7 @@ import {
useFileName,
useDisplayName,
useSSR,
useMeaninglessFileNames,
useNamespace,
} from '../utils/options'
import getName from '../utils/getName'
Expand Down Expand Up @@ -111,21 +112,22 @@ const getExistingConfig = t => path => {
}
}

const getBlockName = file => {
const getBlockName = (file, meaninglessFileNames) => {
const name = path.basename(
file.opts.filename,
path.extname(file.opts.filename)
)
return name !== 'index'
? name
: path.basename(path.dirname(file.opts.filename))

return meaninglessFileNames.includes(name)
? path.basename(path.dirname(file.opts.filename))
: name
}

const getDisplayName = t => (path, state) => {
const { file } = state
const componentName = getName(t)(path)
if (file) {
const blockName = getBlockName(file)
const blockName = getBlockName(file, useMeaninglessFileNames(state))
if (blockName === componentName) {
return componentName
}
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/use-directory-name/.babelrc
@@ -0,0 +1,12 @@
{
"plugins": [
[
"../../../src",
{
"meaninglessFileNames": ["code"],
"transpileTemplateLiterals": false,
"ssr": true
}
]
]
}
6 changes: 6 additions & 0 deletions test/fixtures/use-directory-name/code.js
@@ -0,0 +1,6 @@
import styled from "styled-components";

const Test = styled.div`color: red;`;
const before = styled.div`color: blue;`;
styled.div``;
export default styled.button``;
17 changes: 17 additions & 0 deletions test/fixtures/use-directory-name/output.js
@@ -0,0 +1,17 @@
import styled from "styled-components";
const Test = styled.div.withConfig({
displayName: "use-directory-name__Test",
componentId: "sc-193y009-0"
})`color:red;`;
const before = styled.div.withConfig({
displayName: "use-directory-name__before",
componentId: "sc-193y009-1"
})`color:blue;`;
styled.div.withConfig({
displayName: "use-directory-name",
componentId: "sc-193y009-2"
})``;
export default styled.button.withConfig({
displayName: "use-directory-name",
componentId: "sc-193y009-3"
})``;