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 : invalid characters in JSON5 #194

Open
clement445 opened this issue Oct 18, 2022 · 2 comments
Open

Fix : invalid characters in JSON5 #194

clement445 opened this issue Oct 18, 2022 · 2 comments

Comments

@clement445
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/gulp-file-include/lib/indent.js b/node_modules/gulp-file-include/lib/indent.js
deleted file mode 100644
index a6ea998..0000000
--- a/node_modules/gulp-file-include/lib/indent.js
+++ /dev/null
@@ -1,20 +0,0 @@
-module.exports = function(src, index, dest) {
-  var indent = ''
-  var valid = false
-
-  while (src[index -= 1] == 0) { // eslint-disable-line
-    if (src[index] === '\n') {
-      valid = true
-      break
-    }
-    indent = src[index] + indent
-  }
-
-  if (valid) {
-    dest = dest.split('\n').map(function(str, i) {
-      return str == 0 || i === 0 ? str : (indent + str) // eslint-disable-line
-    }).join('\n')
-  }
-
-  return dest
-}
diff --git a/node_modules/gulp-file-include/lib/index.js b/node_modules/gulp-file-include/lib/index.js
index 89168d8..a64bac4 100644
--- a/node_modules/gulp-file-include/lib/index.js
+++ b/node_modules/gulp-file-include/lib/index.js
@@ -4,7 +4,6 @@ const replaceOperator = require('./replace-operator')
 const replaceFunction = require('./replace-function')
 const replaceVariable = require('./replace-variable')
 const concat = require('concat-stream')
-const setIndent = require('./indent')
 const through = require('through2')
 const Vinyl = require('vinyl')
 const PluginError = require('plugin-error')
@@ -23,8 +22,7 @@ module.exports = function(opts) {
     prefix: '@@',
     suffix: '',
     context: {},
-    filters: false,
-    indent: false
+    filters: false
   }, opts)
 
   if (opts.basepath !== '@file') {
@@ -167,10 +165,6 @@ module.exports = function(opts) {
 
         var includeContent = fs.readFileSync(includePath, 'utf-8')
 
-        if (opts.indent) {
-          includeContent = setIndent(inst.before, inst.before.length, includeContent)
-        }
-
         // need to double each `$` to escape it in the `replace` function
         // includeContent = includeContent.replace(/\$/gi, '$$$$');
 
@@ -186,7 +180,7 @@ module.exports = function(opts) {
           contents: Buffer.from(includeContent)
         })
 
-        recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename)
+        recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3].replace(/(\r|\n)/g, " ")) : {}, inst.sourceFile != "" ? inst.sourceFile : currentFilename);
 
         return String(recFile.contents)
       }
@@ -213,7 +207,7 @@ module.exports = function(opts) {
         } else {
           // loop array in the function
           try {
-            arr = JSON5.parse(args[3])
+            arr = JSON5.parse(args[3].replace(/(\r|\n)/g, " "));
           } catch (err) {
             return console.error(err, args[3])
           }
@@ -228,10 +222,6 @@ module.exports = function(opts) {
 
           var includeContent = fs.readFileSync(includePath, 'utf-8')
 
-          if (opts.indent) {
-            includeContent = setIndent(inst.before, inst.before.length, includeContent)
-          }
-
           // apply filters on include content
           if (typeof opts.filters === 'object') {
             includeContent = applyFilters(includeContent, args.input)

This issue body was partially generated by patch-package.

@Giantswing
Copy link

Giantswing commented Dec 1, 2022

Hey, sorry I'm a bit of a noob, I guess this is a fix to the problem where every time a line is too long and vs code wraps it gulp file include breaks too with the "invalid character \r" right? Would you be so kind as to explain a little bit how did you use patch-package to fix it? I'm currently facing this issue and I'm kinda stuck here, thanks

EDIT: Also, could you post the code without the line adding and deleting so I can try to copy it? Thanks a lot, trully.

EDIT2: Ok I actually did it, thanks, you're a legend, I really hope this somehow gets into the main branch so we don't have to fix it with patch-package, again, thanks a lot :)

@clement445
Copy link
Author

clement445 commented Dec 1, 2022

Hello, yes that was my problem too !
You can read the process on this article
Here's the main changes:

  • node_modules\gulp-file-include\lib\index.js :
    • Change ligne 189 : "JSON5.parse(args[3])" into "JSON5.parse(args[3].replace(/(\r|\n)/g, " "))
    • Change ligne 216 : "arr = JSON5.parse(args[3])" into "arr = JSON5.parse(args[3].replace(/(\r|\n)/g, " "))"
  • You can also remove the indent.js and everythings related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants