Skip to content

Lexer, Nodes: Strings with newlines should be output as template literals with unescaped newlines #5019

@amozoss

Description

@amozoss

When using react relay compiler, Tagged Template Literals have newline characters which causes errors in relay compiler. I've written the sample from the Tagged Template Literal, but included a sample graphql query instead. Would it be possible to compile the literals to not include the new line characters?

Input Code

Sample

upperCaseExpr = (textParts, expressions...) ->
  textParts.reduce (text, textPart, i) ->
    text + expressions[i - 1].toUpperCase() + textPart

greet = (name, adjective) ->
  upperCaseExpr"""
   query HelloQuery {
     allLinks {
       id
       url
     }
   }    
"""

Expected Behavior

Ideally it would compile to the following

upperCaseExpr = function(textParts, ...expressions) {
  return textParts.reduce(function(text, textPart, i) {
    return text + expressions[i - 1].toUpperCase() + textPart;
  });
};

greet = function(name, adjective) {
  return upperCaseExpr`query HelloQuery {
  allLinks {
    id
    url
  }
}    
`;
};

Current Behavior

Instead it is including newline characters

var greet, upperCaseExpr;
upperCaseExpr = function(textParts, ...expressions) {
  return textParts.reduce(function(text, textPart, i) {
    return text + expressions[i - 1].toUpperCase() + textPart;
  });
};

greet = function(name, adjective) {
  return upperCaseExpr`query HelloQuery {\n  allLinks {\n    id\n    url\n  }\n}    `;
};

Possible Solution

Not entirely sure, on the surface it seems like the newline characters change the meaning of the Tagged Template Literal. I can put my literal on one line, but thats not ideal for readability of my graphql query.

Context

When running my code through the relay-coffee-compiler I get the error

Module build failed: Syntax Error: Cannot parse the unexpected character "\\".                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                              
GraphQL request (1:19)                                                                                                                                                                                                                                                                                                        
1: query HelloQuery {\n  allLinks {\n    id\n    url\n  }\n}                                                                                                                                                                                                                                                                  
                     ^                                                                                                                                                                                                                                                                                                                                            

When I only put my coffee template literal on one line, relay-coffee-compiler works. i.e. upperCaseExpr""" query HelloQuery { allLinks { id, url } } """.

Environment

"relay-coffee-compiler": "^0.0.4"

  • CoffeeScript version: 2.2.3

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions