postcss / postcss-calc

PostCSS plugin to reduce calc()
MIT License
212 stars 34 forks source link

Floating number with unknown unit can't be parsed #132

Open perqin opened 3 years ago

perqin commented 3 years ago

postcss-calc leaves integer with unknown unit untouched, but when it comes to floating number, it fails.

// dependencies
var fs = require("fs")
var postcss = require("postcss")
var calc = require("postcss-calc")

var css = 'body { height: calc(120rpx - 41.7rpx); }';

console.log(postcss().use(calc()).process('body { height: calc(120rpx - 41rpx); }').css);
console.log('Passed test 1');

// process css
var output = postcss()
  .use(calc())
  .process(css)
  .css

console.log('output: ', output);
console.log('Passed test 2');

Here's the error log:

/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/lazy-result.js:288
        throw this.handleError(e, node.proxyOf)
        ^
JisonParserError: Parse error on line 1:
120rpx - 41.7rpx
-----------^
Expecting end of input, "RPAREN", "ADD", "SUB", "MUL", "DIV", got unexpected "UNKNOWN_DIMENSION"
    at <input css XMfWJ3>:1:1
    at <input css XMfWJ3>:1:8
    at Parser.parseError (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/parser.js:1200:15)
    at Parser.parse (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/parser.js:1716:30)
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:33:32
    at walk (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-value-parser/lib/walk.js:7:16)
    at ValueParser.walk (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-value-parser/lib/index.js:18:3)
    at transformValue (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:24:50)
    at _default (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/lib/transform.js:66:102)
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss-calc/dist/index.js:32:34
    at /Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/container.js:74:18
    at Rule.each (/Users/perqinxie/Workspaces/_demo/postcss-calc-issue/node_modules/postcss/lib/container.js:60:16) {
  hash: {
    errStr: 'Parse error on line 1: \n' +
      '120rpx - 41.7rpx\n' +
      '-----------^\n' +
      'Expecting end of input, "RPAREN", "ADD", "SUB", "MUL", "DIV", got unexpected "UNKNOWN_DIMENSION"',
    exception: null,
    text: '.7rpx',
    value: '.7rpx',
    token: '"UNKNOWN_DIMENSION"',
    token_id: 16,
    line: 0,
    expected: [ 'end of input', '"RPAREN"', '"ADD"', '"SUB"', '"MUL"', '"DIV"' ],
    recoverable: false,
    state: 41,
    action: 0,
    new_state: 47,
    symbol_stack: [],
    state_stack: [],
    value_stack: [],
    stack_pointer: 4,
    yy: {
      parseError: undefined,
      quoteName: undefined,
      lexer: undefined,
      parser: undefined,
      pre_parse: undefined,
      post_parse: undefined,
      pre_lex: undefined,
      post_lex: undefined
    },
    lexer: {
      yy: {},
      __decompressed: true,
      _input: '',
      yytext: '',
      yyleng: 0,
      match: '',
      matches: false,
      _more: false,
      _backtrack: false,
      yylloc: {
        first_line: 1,
        first_column: 0,
        last_line: 1,
        last_column: 0,
        range: [ 0, 0 ]
      },
      _signaled_error_token: false,
      done: false,
      yylineno: 0,
      matched: '',
      conditionStack: [ 'INITIAL' ],
      __currentRuleSet__: null,
      offset: 0
    },
    parser: {
      yy: {},
      __lexer__: {
        yy: {},
        __decompressed: true,
        _input: '',
        yytext: '',
        yyleng: 0,
        match: '',
        matches: false,
        _more: false,
        _backtrack: false,
        yylloc: {
          first_line: 1,
          first_column: 0,
          last_line: 1,
          last_column: 0,
          range: [ 0, 0 ]
        },
        _signaled_error_token: false,
        done: false,
        yylineno: 0,
        matched: '',
        conditionStack: [ 'INITIAL' ],
        __currentRuleSet__: null,
        offset: 0
      },
      yyGetSharedState: [Function: yyGetSharedState],
      parseError: [Function: parseError],
      quoteName: [Function: parser_quoteName],
      cleanupAfterParse: [Function: parser_cleanupAfterParse],
      constructParseErrorInfo: [Function: parser_constructParseErrorInfo],
      __reentrant_call_depth: 0
    },
    destroy: [Function: destructParseErrorInfo]
  },
  postcssNode: <ref *2> Root {
    raws: { semicolon: false, after: '' },
    type: 'root',
    nodes: [
      <ref *1> Rule {
        raws: { before: '', between: ' ', semicolon: true, after: ' ' },
        type: 'rule',
        nodes: [
          Declaration {
            raws: [Object],
            type: 'decl',
            parent: [Circular *1],
            source: [Object],
            prop: 'height',
            value: 'calc(120rpx - 41.7rpx)',
            [Symbol(isClean)]: true
          }
        ],
        parent: [Circular *2],
        source: {
          start: { offset: 0, line: 1, column: 1 },
          input: Input {
            css: 'body { height: calc(120rpx - 41.7rpx); }',
            hasBOM: false,
            id: '<input css XMfWJ3>',
            [Symbol(fromOffset cache)]: [Array]
          },
          end: { offset: 39, line: 1, column: 40 }
        },
        selector: 'body',
        lastEach: 2,
        indexes: { '2': 0 },
        [Symbol(isClean)]: true
      }
    ],
    source: {
      input: Input {
        css: 'body { height: calc(120rpx - 41.7rpx); }',
        hasBOM: false,
        id: '<input css XMfWJ3>',
        [Symbol(fromOffset cache)]: [ 0 ]
      },
      start: { offset: 0, line: 1, column: 1 }
    },
    lastEach: 2,
    indexes: { '2': 0 },
    [Symbol(isClean)]: true
  }
}
ludofischer commented 2 years ago

In 8.2, it still fails to parse, but at least does not crash.

ludofischer commented 2 years ago

Moving the UNKNOWN_DIMENSION before NUMBER in the lexer solves this issue, but then it prevents numbers with exponents to be reduced.

tutuxxx commented 1 year ago

same problem

jerome-rdlv commented 3 days ago

I’m having the issue with relatively new cqi unit :

Message:
    Parse error on line 1: 
-0.9375rem + 12.5cqi
---------------^
Expecting end of input, "RPAREN", "ADD", "SUB", "MUL", "DIV", got unexpected "UNKNOWN_DIMENSION"
Details:
    hash: [object Object]
    postcssNode: padding:clamp(0.625rem, calc(-0.9375rem + 12.5cqi), 1.875rem)