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 option to use parent folder name as component name #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function getOption({ opts }, name, defaultValue = true) {
export const useDisplayName = state => getOption(state, 'displayName')
export const useSSR = state => getOption(state, 'ssr', true)
export const useFileName = state => getOption(state, 'fileName')
export const useFolderName = state => getOption(state, 'folderName', false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be named more obviously... perhaps prefixComponentWithFolderName

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

folderName sounds not less clearer as fileName. Context matters.

export const useMinify = state => getOption(state, 'minify')
export const useTranspileTemplateLiterals = state =>
getOption(state, 'transpileTemplateLiterals')
Expand Down
13 changes: 9 additions & 4 deletions src/visitors/displayNameAndId.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path'
import fs from 'fs'
import { useFileName, useDisplayName, useSSR } from '../utils/options'
import { useFileName, useFolderName, useDisplayName, useSSR } from '../utils/options'
import getName from '../utils/getName'
import prefixLeadingDigit from '../utils/prefixDigit'
import hash from '../utils/hash'
Expand Down Expand Up @@ -48,21 +48,26 @@ const addConfig = t => (path, displayName, componentId) => {
}
}

const getBlockName = file => {
const getBlockName = (file, state) => {
if (state && useFolderName(state)) return getFolderName(file)

const name = path.basename(
file.opts.filename,
path.extname(file.opts.filename)
)

return name !== 'index'
? name
: path.basename(path.dirname(file.opts.filename))
: getFolderName(file)
}

const getFolderName = file => path.basename(path.dirname(file.opts.filename))

const getDisplayName = t => (path, state) => {
const { file } = state
const componentName = getName(t)(path)
if (file) {
const blockName = getBlockName(file)
const blockName = getBlockName(file, state)
if (blockName === componentName) {
return componentName
}
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/use-folder-name/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"plugins": [
[
"../../../src",
{
"transpileTemplateLiterals": false,
"ssr": true,
"folderName": true
}
]
]
}
6 changes: 6 additions & 0 deletions test/fixtures/use-folder-name/code.js
Original file line number Diff line number Diff line change
@@ -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-folder-name/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import styled from "styled-components";
const Test = styled.div.withConfig({
displayName: "use-folder-name__Test",
componentId: "sc-1lbi44x-0"
})`color:red;`;
const before = styled.div.withConfig({
displayName: "use-folder-name__before",
componentId: "sc-1lbi44x-1"
})`color:blue;`;
styled.div.withConfig({
displayName: "use-folder-name",
componentId: "sc-1lbi44x-2"
})``;
export default styled.button.withConfig({
displayName: "use-folder-name",
componentId: "sc-1lbi44x-3"
})``;