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

Fix TypeError for custom properties fallback in length-zero-no-unit #4860

Merged
merged 3 commits into from Aug 10, 2020
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
11 changes: 11 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Expand Up @@ -237,6 +237,10 @@ testRule({
code: 'a { font: normal normal 400 16px /0px cursive; }',
description: 'ignore line-height in font declaration',
},
{
code: 'a { font: var(--foo, normal normal 400 16px/0px cursive); }',
description: 'ignore line-height in font declaration within var function',
},
{
code: 'a { font: normal normal 400 1.2em cursive; }',
description: 'do not fail if no line-height in font declaration',
Expand Down Expand Up @@ -530,6 +534,13 @@ testRule({
line: 1,
column: 30,
},
{
code: 'a { margin: var(--foo, 0px); }',
fixed: 'a { margin: var(--foo, 0); }',
message: messages.rejected,
line: 1,
column: 25,
},
],
});

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/length-zero-no-unit/index.js
Expand Up @@ -40,9 +40,9 @@ function rule(actual, secondary, context) {
const ignorableIndexes = new Array(stringValue.length).fill(false);
const parsedValue = valueParser(stringValue);

parsedValue.walk((node, nodeIndex) => {
parsedValue.walk((node, nodeIndex, nodes) => {
if (decl.prop.toLowerCase() === 'font' && node.type === 'div' && node.value === '/') {
const lineHeightNode = parsedValue.nodes[nodeIndex + 1];
const lineHeightNode = nodes[nodeIndex + 1];
Copy link
Member

Choose a reason for hiding this comment

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

When descending into functions, the nodes array will be scoped to the current function args. This is the array that nodeIndex refers to. See the postcss-value-parser docs for details.

e.g., to parse the value font: var(--foo, normal normal 400 16px/0 cursive);, the value parser will run two loops.

The outer loop operates on the following nodes:

// parsedValue.nodes contains this array:
[
      { type: 'word', sourceIndex: 0, value: 'font' },
      { type: 'div', sourceIndex: 4, value: ':', before: '', after: ' ' },
      {
        type: 'function',
        sourceIndex: 6,
        value: 'var',
        before: '',
        after: '',
        nodes: [
          [Object], [Object],
          [Object], [Object],
          [Object], [Object],
          [Object], [Object],
          [Object], [Object],
          [Object], [Object],
          [Object]
        ]
      }

The inner loop (equivalent to parsedValue.nodes[2].nodes) runs on the following values:

[
      { type: 'word', sourceIndex: 10, value: '--foo' },
      { type: 'div', sourceIndex: 15, value: ',', before: '', after: ' ' },
      { type: 'word', sourceIndex: 17, value: 'normal' },
      { type: 'space', sourceIndex: 23, value: ' ' },
      { type: 'word', sourceIndex: 24, value: 'normal' },
      { type: 'space', sourceIndex: 30, value: ' ' },
      { type: 'word', sourceIndex: 31, value: '400' },
      { type: 'space', sourceIndex: 34, value: ' ' },
      { type: 'word', sourceIndex: 35, value: '16px' },
      { type: 'div', sourceIndex: 39, value: '/', before: '', after: '' },
      { type: 'word', sourceIndex: 40, value: '0' },
      { type: 'space', sourceIndex: 41, value: ' ' },
      { type: 'word', sourceIndex: 42, value: 'cursive' }
    ]

Using nodes here means we can pick the right node with nodes[nodeIndex+1].

const lineHeightNodeValue = valueParser.stringify(lineHeightNode);

for (let i = 0; i < lineHeightNodeValue.length; i++) {
Expand Down