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 the 'jsCustomTags' option #6626

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions scripts/generate-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ function optionTypeToSchemaType(optionType) {
);
case "path":
return "string";
case "any":
return "object";
default:
throw new Error(`Unexpected optionType '${optionType}'`);
}
Expand Down
15 changes: 11 additions & 4 deletions src/language-js/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function embed(path, print, textToDoc, options) {
isStyledComponents,
isCssProp,
isAngularComponentStyles
].some(isIt => isIt(path));
].some(isIt => isIt(path, options));

if (isCss) {
// Get full template literal with expressions replaced by placeholders
Expand Down Expand Up @@ -431,7 +431,7 @@ function getAngularComponentObjectExpressionPredicates() {
/**
* styled-components template literals
*/
function isStyledComponents(path) {
function isStyledComponents(path, options) {
const parent = path.getParentNode();

if (!parent || parent.type !== "TaggedTemplateExpression") {
Expand All @@ -440,13 +440,20 @@ function isStyledComponents(path) {

const tag = parent.tag;

const customTags = options.jsCustomTags["styled-components"] || [];

function isCustomIdentifier(node) {
return node.type === "Identifier" && customTags.includes(node.name);
}

switch (tag.type) {
case "MemberExpression":
return (
// styled.foo``
isStyledIdentifier(tag.object) ||
// Component.extend``
isStyledExtend(tag)
isStyledExtend(tag) ||
isCustomIdentifier(tag.object)
);

case "CallExpression":
Expand All @@ -466,7 +473,7 @@ function isStyledComponents(path) {

case "Identifier":
// css``
return tag.name === "css";
return tag.name === "css" || customTags.includes(tag.name);

default:
return false;
Expand Down
8 changes: 8 additions & 0 deletions src/language-js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ module.exports = {
]
},
bracketSpacing: commonOptions.bracketSpacing,
jsCustomTags: {
since: "1.18.2",
category: CATEGORY_JAVASCRIPT,
type: "any",
default: {},
description:
"A map of languages and custom template literal tag names. Example: { 'styled-components': ['media'] }"
},
jsxBracketSameLine: {
since: "0.17.0",
category: CATEGORY_JAVASCRIPT,
Expand Down
3 changes: 3 additions & 0 deletions src/main/options-normalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ function optionInfoToSchema(optionInfo, { isCLI, optionInfos }) {
case "path":
SchemaConstructor = vnopts.StringSchema;
break;
case "any":
SchemaConstructor = vnopts.AnySchema;
break;
default:
throw new Error(`Unexpected type ${optionInfo.type}`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`custom-tags.js 1`] = `
====================================options=====================================
jsCustomTags: {"styled-components":["myCustomTag","media"]}
parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
const Button = styled.a\`
/* Comment */
display: \${props=>props.display};
\`;

styled.div\`
display: \${props=>props.display};
border: \${props=>props.border}px;
margin: 10px \${props=>props.border}px ;
\`;

const EqualDivider = styled.div\`
margin: 0.5rem;
padding: 1rem;
background: papayawhip ;

> * {
flex: 1;

&:not(:first-child) {
\${props => props.vertical ? 'margin-top' : 'margin-left'}: 1rem;
}
}
\`;

const header = css\`
.top-bar {background:black;
margin: 0;
position: fixed;
top: 0;left:0;
width: 100%;
text-align: center ;
padding: 15px 0 0 1em;
z-index: 9999;
}

.top-bar .logo {
height: 30px;
margin: auto;
position: absolute;
left: 0;right: 0;

\${myCustomTag\`
background-color:red;

:after { color:red;

}
\`};

\${media.phone\`
background-color:red;

:after { color:red;

}
\`};
}
\`;

=====================================output=====================================
const Button = styled.a\`
/* Comment */
display: \${props => props.display};
\`;

styled.div\`
display: \${props => props.display};
border: \${props => props.border}px;
margin: 10px \${props => props.border}px;
\`;

const EqualDivider = styled.div\`
margin: 0.5rem;
padding: 1rem;
background: papayawhip;

> * {
flex: 1;

&:not(:first-child) {
\${props => (props.vertical ? "margin-top" : "margin-left")}: 1rem;
}
}
\`;

const header = css\`
.top-bar {
background: black;
margin: 0;
position: fixed;
top: 0;
left: 0;
width: 100%;
text-align: center;
padding: 15px 0 0 1em;
z-index: 9999;
}

.top-bar .logo {
height: 30px;
margin: auto;
position: absolute;
left: 0;
right: 0;

\${myCustomTag\`
background-color: red;

:after {
color: red;
}
\`};

\${media.phone\`
background-color: red;

:after {
color: red;
}
\`};
}
\`;

================================================================================
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const Button = styled.a`
/* Comment */
display: ${props=>props.display};
`;

styled.div`
display: ${props=>props.display};
border: ${props=>props.border}px;
margin: 10px ${props=>props.border}px ;
`;

const EqualDivider = styled.div`
margin: 0.5rem;
padding: 1rem;
background: papayawhip ;

> * {
flex: 1;

&:not(:first-child) {
${props => props.vertical ? 'margin-top' : 'margin-left'}: 1rem;
}
}
`;

const header = css`
.top-bar {background:black;
margin: 0;
position: fixed;
top: 0;left:0;
width: 100%;
text-align: center ;
padding: 15px 0 0 1em;
z-index: 9999;
}

.top-bar .logo {
height: 30px;
margin: auto;
position: absolute;
left: 0;right: 0;

${myCustomTag`
background-color:red;

:after { color:red;

}
`};

${media.phone`
background-color:red;

:after { color:red;

}
`};
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run_spec(__dirname, ["babel", "flow", "typescript"], {
jsCustomTags: {
"styled-components": ["myCustomTag", "media"]
}
});