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

Strip indent having issues when using something like util.inspect #109

Closed
basicdays opened this issue Jun 22, 2017 · 4 comments
Closed

Strip indent having issues when using something like util.inspect #109

basicdays opened this issue Jun 22, 2017 · 4 comments

Comments

@basicdays
Copy link

basicdays commented Jun 22, 2017

When I'm including data that was serialized using node's util.inspect function, stripIndent seems to be making some mistakes on how to handle the indent stripping. Here's the script and the output as an example:

'use strict';
const commonTags = require('common-tags');
const util = require('util');


const obj = {
	prop1: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
	prop2: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
	shape: {
		prop3: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
		prop4: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf'
	}
}

if (true) {
	if (true) {
		console.log(commonTags.stripIndent`
			Some firstline message
			Some secondline message
			${util.inspect(obj)}
		`);
	}
}

Actual output:

Some firstline message
        Some secondline message
        { prop1: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
prop2: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
shape:
 { prop3: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
   prop4: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf' } }
vagrant@dev:/opt/signal (alert-check-update)$ node scratch/data-test.js

Expected output:

Some firstline message
Some secondline message
{ prop1: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
  prop2: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
  shape:
   { prop3: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf',
     prop4: 'asdfasdfasdfsadfasdf asdfasdfasdfsadfasdf' } }
@ibrahima
Copy link
Contributor

ibrahima commented Sep 8, 2017

I think I came across a similar issue. The problem, I think, is that if you have some interpolated values that don't have any leading whitespace, that becomes the minimum whitespace. Then, stripIndent does also include the trim transformer, so the first line gets trimmed of the leading whitespace, but all other lines are untouched.

This is a little surprising to me, because I expected template tags to operate on the template literal before interpolation, rather than not after, but I haven't fully understood how this stuff all works yet.

@ibrahima
Copy link
Contributor

ibrahima commented Sep 8, 2017

Aha! In this case, you're actually meant to use the html tag which does handle embedded newlines.

This tag is also aliased to source and codeBlock, which make more sense to me, since personally I'm not writing HTML here.

@fatfisz fatfisz added the bug label Nov 23, 2017
@fatfisz
Copy link
Collaborator

fatfisz commented Nov 23, 2017

Hi, I can confirm that the issue is there in v1.4.0. I can't check right now whether it was fixed in #114, but will do so later today. In any case, a version with a fix should be released in a matter of days - keep your fingers crossed 🤞

@fatfisz
Copy link
Collaborator

fatfisz commented Nov 23, 2017

@ibrahima is right - "fixing" stripIndent would turn it into source, so I guess the only thing that's not working here is the naming (one of the unsolved problems of computer science 😉). I'll be adding a note about this particular behavior to the docs.

@fatfisz fatfisz closed this as completed Nov 23, 2017
@fatfisz fatfisz removed the bug label Nov 23, 2017
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

3 participants