Skip to content

Commit 05e9e03

Browse files
committed
Warn and dot or hash
1 parent 56d01e0 commit 05e9e03

File tree

2 files changed

+74
-39
lines changed

2 files changed

+74
-39
lines changed

src/index.js

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ const trimNodes = nodes => dropWhile(dropRightWhile(nodes, isSpace), isSpace);
4646
const getPathValue = nodes =>
4747
nodes.length === 1 && nodes[0].type === "string" ? nodes[0].value : null;
4848

49-
const expandValuesParentheses = valuesNodes =>
50-
valuesNodes.length === 1 &&
51-
valuesNodes[0].type === "function" &&
52-
valuesNodes[0].value === ""
53-
? valuesNodes[0].nodes
54-
: valuesNodes;
49+
const expandValuesParentheses = nodes =>
50+
nodes.length === 1 && nodes[0].type === "function" && nodes[0].value === ""
51+
? nodes[0].nodes
52+
: nodes;
5553

5654
const getAliasesPairs = valuesNodes =>
5755
chunkBy(expandValuesParentheses(valuesNodes), isComma).map(pairNodes => {
@@ -95,19 +93,14 @@ const parse = value => {
9593
return null;
9694
};
9795

98-
const getAliasName = (name, index) =>
99-
`__value__${name.replace(/\W/g, "_")}__${index}`;
96+
const isForbidden = name => name.includes(".") || name.includes("#");
10097

101-
// TODO forbig '.' and '#' in names
98+
const createGenerator = (i = 0) => name =>
99+
`__value__${name.replace(/\W/g, "_")}__${i++}`;
102100

103101
module.exports = postcss.plugin(plugin, () => (css, result) => {
104102
const { icssImports, icssExports } = extractICSS(css);
105-
let importIndex = 0;
106-
const createImportedName = (path, name) => {
107-
const importedName = getAliasName(name, importIndex);
108-
importIndex += 1;
109-
return importedName;
110-
};
103+
const getAliasName = createGenerator();
111104

112105
css.walkAtRules("value", atRule => {
113106
if (atRule.params.indexOf("@value") !== -1) {
@@ -118,33 +111,50 @@ module.exports = postcss.plugin(plugin, () => (css, result) => {
118111
const parsed = parse(atRule.params);
119112
if (parsed) {
120113
if (parsed.type === "value") {
121-
if (icssExports[parsed.name]) {
122-
result.warn(`"${parsed.name}" value already declared`, {
123-
node: atRule
124-
});
125-
}
126-
icssExports[parsed.name] = replaceValueSymbols(
127-
parsed.value,
128-
icssExports
129-
);
130-
}
131-
if (parsed.type === "import") {
132-
const pairs = parsed.pairs.map(([key, value]) => {
133-
let importedName = createImportedName(parsed.path, value);
134-
if (icssExports[value]) {
135-
result.warn(`"${value}" value already declared`, {
114+
const { name, value } = parsed;
115+
if (isForbidden(name)) {
116+
result.warn(
117+
`Dot and hash symbols are not allowed in value "${name}"`,
118+
{ node: atRule }
119+
);
120+
} else {
121+
if (icssExports[name]) {
122+
result.warn(`"${name}" value already declared`, {
136123
node: atRule
137124
});
138125
}
139-
icssExports[value] = importedName;
140-
return [importedName, key];
141-
});
142-
const aliases = fromPairs(pairs);
143-
icssImports[parsed.path] = Object.assign(
144-
{},
145-
icssImports[parsed.path],
146-
aliases
147-
);
126+
icssExports[name] = replaceValueSymbols(value, icssExports);
127+
}
128+
}
129+
if (parsed.type === "import") {
130+
const pairs = parsed.pairs
131+
.filter(([, local]) => {
132+
if (isForbidden(local)) {
133+
result.warn(
134+
`Dot and hash symbols are not allowed in value "${local}"`
135+
);
136+
return false;
137+
}
138+
return true;
139+
})
140+
.map(([imported, local]) => {
141+
const alias = getAliasName(local);
142+
if (icssExports[local]) {
143+
result.warn(`"${local}" value already declared`, {
144+
node: atRule
145+
});
146+
}
147+
icssExports[local] = alias;
148+
return [alias, imported];
149+
});
150+
if (pairs.length) {
151+
const aliases = fromPairs(pairs);
152+
icssImports[parsed.path] = Object.assign(
153+
{},
154+
icssImports[parsed.path],
155+
aliases
156+
);
157+
}
148158
}
149159
} else {
150160
result.warn(`Invalid value definition "${atRule.params}"`, {

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,28 @@ test("save :import and :export statements", () => {
338338
expected: input
339339
});
340340
});
341+
342+
test("warn on using dot or hash in value name", () => {
343+
return run({
344+
fixture: `
345+
@value colors.red #f00;
346+
@value colors#blue #00f;
347+
@value .red from 'path';
348+
@value #blue from 'path';
349+
.foo { color: colors.red; background: colors#blue }
350+
.red {}
351+
#blue {}
352+
`,
353+
expected: `
354+
.foo { color: colors.red; background: colors#blue }
355+
.red {}
356+
#blue {}
357+
`,
358+
warnings: [
359+
`Dot and hash symbols are not allowed in value "colors.red"`,
360+
`Dot and hash symbols are not allowed in value "colors#blue"`,
361+
`Dot and hash symbols are not allowed in value ".red"`,
362+
`Dot and hash symbols are not allowed in value "#blue"`
363+
]
364+
});
365+
});

0 commit comments

Comments
 (0)