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

Tab style is not kept #34

Open
innocenzi opened this issue Apr 5, 2023 · 2 comments
Open

Tab style is not kept #34

innocenzi opened this issue Apr 5, 2023 · 2 comments

Comments

@innocenzi
Copy link

innocenzi commented Apr 5, 2023

Environment

magicast@0.2
typescript@5.0.3
node@19.8.1

Reproduction

import { parseModule, generateCode } from 'magicast'
import { addVitePlugin } from 'magicast/helpers'

const mod = parseModule(`
import { defineConfig } from 'vite'

export default defineConfig({
	plugins: []
})
`)

addVitePlugin(mod, {
	from: '@vitejs/plugin-vue',
	constructor: 'vue',
	options: {
		owo: true,
	},
})

const { code } = generateCode(mod)

console.log({ code })

Describe the bug

In the reproduction above, the indentation of plugins is a single tab. However, the generated code uses four tabs.

Additional context

Result:

{
  code: "import vue from '@vitejs/plugin-vue';\n" +
    "import { defineConfig } from 'vite'\n" +
    '\n' +
    'export default defineConfig({\n' +
    '\t\t\t\tplugins: [vue({\n' +
    '\t\t\t\t owo: true\n' +
    '\t\t\t\t})]\n' +
    '})'
}

Expected:

{
  code: "import vue from '@vitejs/plugin-vue';\n" +
    "import { defineConfig } from 'vite'\n" +
    '\n' +
    'export default defineConfig({\n' +
    '\tplugins: [vue({\n' +
    '\t\towo: true\n' +
    '\t})]\n' +
    '})'
}
@antfu
Copy link
Member

antfu commented Apr 5, 2023

This feels a bit like recast's bug to me. As I checked, we seems to be detecting them correctly and passing { useTabs: true, tabWidth: 1 }. And normally I think when useTabs is set, tabWidth should be ignored, but in real, it actually affects how many tabs are generated (passing 2 makes it generate fewer tabs somehow).

Could use some help if anyone wants to go deeper on it.

@tushar-deepsource
Copy link

tushar-deepsource commented Apr 5, 2023

@antfu I assume tabWidth is a bit like the ts query parameter in github. If the indentation is 2 tabs in your code, and you set ?ts=2 in any GitHub URL, the 2 tabs will look like 4 spaces. Similarly if you do ?ts=4, 4 tabs will look like 4 spaces.

Hence I guess tabWidth: 1 tells the generator that to preserve the look of 4 spaces, insert 4 tabs there.

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