benjamn / recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
MIT License
5.01k stars 350 forks source link

useTabs option works only partially #315

Open nene opened 8 years ago

nene commented 8 years ago

When running the following code:

var recast = require('recast');

var ast = recast.parse(
  'while (true) {\n' +
  '\tx;\n' +
  '}'
, {useTabs: true});

// Add one line inside WHILE loop
ast.program.body[0].body.body.push({
  'type': 'ExpressionStatement',
  'expression': {
    'type': 'Identifier',
    'name': 'y'
  }
});

console.log(recast.print(ast, {useTabs: true}).code.replace(/\t/g, "<TAB>"));

I'd expect to get:

while (true) {
<TAB>x;
<TAB>y;
}

But instead Recast gives me:

while (true) {
    x;
<TAB>y;
}

NOTE: There are no docs to tell me whether I should pass useTabs option to recast.parse() or recast.print(), it seems that it's only needed for the latter, but just in case I'm passing it to both.

ghost commented 7 years ago

Specifying various settings such as { useTabs: true, quote: 'single' } would work in most cases. Sometimes, however, the indentation was mixed. I imagine this relates to the discussion here: https://github.com/benjamn/recast/pull/404. The most reliable workaround I have found is to simply call recast.print without any options, and then use eslint --fix to reindent correctly.