Skip to content

Commit 37582ff

Browse files
committed
Tweak prettier config
1 parent e0f9bd5 commit 37582ff

File tree

4 files changed

+34
-107
lines changed

4 files changed

+34
-107
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"dev": "npm run _esbuild -- --watch",
2323
"test": "jest",
2424
"prepublishOnly": "npm run build && npm test && node scripts/copy-licenses.js",
25-
"format": "prettier \"src/**/*.js\" \"scripts/**/*.js\" \"tests/test.js\" --write --print-width 80 --single-quote --no-semi --plugin-search-dir ./tests"
25+
"format": "prettier \"src/**/*.js\" \"scripts/**/*.js\" \"tests/test.js\" --write --print-width 100 --single-quote --no-semi --plugin-search-dir ./tests"
2626
},
2727
"devDependencies": {
2828
"@tailwindcss/line-clamp": "^0.3.0",

scripts/copy-licenses.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ let exclude = [
1717
checker.init({ start: path.resolve(__dirname, '..') }, (_err, packages) => {
1818
for (let key in packages) {
1919
let name = key.split(/(?<=.)@/)[0]
20-
if (
21-
name in devDependencies &&
22-
!exclude.includes(name) &&
23-
packages[key].licenseFile
24-
) {
20+
if (name in devDependencies && !exclude.includes(name) && packages[key].licenseFile) {
2521
let dir = path.resolve(__dirname, '../dist/licenses', name)
2622
fs.mkdirSync(dir, { recursive: true })
2723
fs.copyFileSync(

src/index.js

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,7 @@ function getClassOrderPolyfill(classes, { env }) {
6565

6666
function sortClasses(
6767
classStr,
68-
{
69-
env,
70-
ignoreFirst = false,
71-
ignoreLast = false,
72-
tidyWhitespace = { start: true, end: true },
73-
}
68+
{ env, ignoreFirst = false, ignoreLast = false, tidyWhitespace = { start: true, end: true } }
7469
) {
7570
if (typeof classStr !== 'string' || classStr === '') {
7671
return classStr
@@ -161,9 +156,7 @@ function createParser(original, transform) {
161156
let prettierConfigPath = prettier.resolveConfigFile.sync(options.filepath)
162157

163158
if (options.tailwindConfig) {
164-
baseDir = prettierConfigPath
165-
? path.dirname(prettierConfigPath)
166-
: process.cwd()
159+
baseDir = prettierConfigPath ? path.dirname(prettierConfigPath) : process.cwd()
167160
tailwindConfigPath = path.resolve(baseDir, options.tailwindConfig)
168161
tailwindConfig = requireFresh(tailwindConfigPath)
169162
} else {
@@ -191,14 +184,8 @@ function createParser(original, transform) {
191184

192185
try {
193186
resolveConfig = requireFrom(baseDir, 'tailwindcss/resolveConfig')
194-
createContext = requireFrom(
195-
baseDir,
196-
'tailwindcss/lib/lib/setupContextUtils'
197-
).createContext
198-
generateRules = requireFrom(
199-
baseDir,
200-
'tailwindcss/lib/lib/generateRules'
201-
).generateRules
187+
createContext = requireFrom(baseDir, 'tailwindcss/lib/lib/setupContextUtils').createContext
188+
generateRules = requireFrom(baseDir, 'tailwindcss/lib/lib/generateRules').generateRules
202189
} catch {}
203190

204191
// suppress "empty content" warning
@@ -261,9 +248,7 @@ function transformHtml(attributes, computedAttributes = []) {
261248
})
262249

263250
if (didChange) {
264-
attr.value = recast.print(
265-
ast.program.body[0].declarations[0].init
266-
).code
251+
attr.value = recast.print(ast.program.body[0].declarations[0].init).code
267252
}
268253
}
269254
}
@@ -297,8 +282,7 @@ function sortStringLiteral(node, { env }) {
297282

298283
function isStringLiteral(node) {
299284
return (
300-
node.type === 'StringLiteral' ||
301-
(node.type === 'Literal' && typeof node.value === 'string')
285+
node.type === 'StringLiteral' || (node.type === 'Literal' && typeof node.value === 'string')
302286
)
303287
}
304288

@@ -326,18 +310,14 @@ function sortTemplateLiteral(node, { env }) {
326310
: sortClasses(quasi.value.cooked, {
327311
env,
328312
ignoreFirst: i > 0 && !/^\s/.test(quasi.value.cooked),
329-
ignoreLast:
330-
i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
313+
ignoreLast: i < node.expressions.length && !/\s$/.test(quasi.value.cooked),
331314
tidyWhitespace: {
332315
start: i === 0,
333316
end: i >= node.expressions.length,
334317
},
335318
})
336319

337-
if (
338-
quasi.value.raw !== originalRaw ||
339-
quasi.value.cooked !== originalCooked
340-
) {
320+
if (quasi.value.raw !== originalRaw || quasi.value.cooked !== originalCooked) {
341321
didChange = true
342322
}
343323
}
@@ -400,14 +380,8 @@ export const printers = {
400380
let finder = lineColumn(options.originalText)
401381

402382
for (let change of changes) {
403-
let start = finder.toIndex(
404-
change.loc.start.line,
405-
change.loc.start.column + 1
406-
)
407-
let end = finder.toIndex(
408-
change.loc.end.line,
409-
change.loc.end.column + 1
410-
)
383+
let start = finder.toIndex(change.loc.start.line, change.loc.start.column + 1)
384+
let end = finder.toIndex(change.loc.end.line, change.loc.end.column + 1)
411385

412386
options.originalText =
413387
options.originalText.substring(0, start) +
@@ -429,39 +403,18 @@ export const parsers = {
429403
prettierParserHTML.parsers.angular,
430404
transformHtml(['class'], ['[ngClass]'])
431405
),
432-
vue: createParser(
433-
prettierParserHTML.parsers.vue,
434-
transformHtml(['class'], [':class'])
435-
),
406+
vue: createParser(prettierParserHTML.parsers.vue, transformHtml(['class'], [':class'])),
436407
css: createParser(prettierParserPostCSS.parsers.css, transformCss),
437408
scss: createParser(prettierParserPostCSS.parsers.scss, transformCss),
438409
less: createParser(prettierParserPostCSS.parsers.less, transformCss),
439410
babel: createParser(prettierParserBabel.parsers.babel, transformJavaScript),
440-
'babel-flow': createParser(
441-
prettierParserBabel.parsers['babel-flow'],
442-
transformJavaScript
443-
),
411+
'babel-flow': createParser(prettierParserBabel.parsers['babel-flow'], transformJavaScript),
444412
flow: createParser(prettierParserFlow.parsers.flow, transformJavaScript),
445-
typescript: createParser(
446-
prettierParserTypescript.parsers.typescript,
447-
transformJavaScript
448-
),
449-
'babel-ts': createParser(
450-
prettierParserBabel.parsers['babel-ts'],
451-
transformJavaScript
452-
),
453-
espree: createParser(
454-
prettierParserEspree.parsers.espree,
455-
transformJavaScript
456-
),
457-
meriyah: createParser(
458-
prettierParserMeriyah.parsers.meriyah,
459-
transformJavaScript
460-
),
461-
__js_expression: createParser(
462-
prettierParserBabel.parsers.__js_expression,
463-
transformJavaScript
464-
),
413+
typescript: createParser(prettierParserTypescript.parsers.typescript, transformJavaScript),
414+
'babel-ts': createParser(prettierParserBabel.parsers['babel-ts'], transformJavaScript),
415+
espree: createParser(prettierParserEspree.parsers.espree, transformJavaScript),
416+
meriyah: createParser(prettierParserMeriyah.parsers.meriyah, transformJavaScript),
417+
__js_expression: createParser(prettierParserBabel.parsers.__js_expression, transformJavaScript),
465418
svelte: createParser(svelte.parsers.svelte, (ast, { env }) => {
466419
let changes = []
467420
transformSvelte(ast.html, { env, changes })
@@ -490,8 +443,7 @@ function transformSvelte(ast, { env, changes }) {
490443
: sortClasses(value.data, {
491444
env,
492445
ignoreFirst: i > 0 && !/^\s/.test(value.data),
493-
ignoreLast:
494-
i < attr.value.length - 1 && !/\s$/.test(value.data),
446+
ignoreLast: i < attr.value.length - 1 && !/\s$/.test(value.data),
495447
tidyWhitespace: {
496448
start: i === 0,
497449
end: i >= attr.value.length - 1,

tests/test.js

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,9 @@ let javascript = [
9696
`;<div class={\`sm:p-0 p-0 \${someVar}sm:block md:inline flex\`} />`,
9797
`;<div class={\`p-0 sm:p-0 \${someVar}sm:block flex md:inline\`} />`,
9898
],
99-
[
100-
';<div class=" m-0 sm:p-0 p-0 " />',
101-
';<div class="m-0 p-0 sm:p-0" />',
102-
],
103-
[
104-
";<div class={' m-0 sm:p-0 p-0 '} />",
105-
";<div class={'m-0 p-0 sm:p-0'} />",
106-
],
107-
[
108-
';<div class={` sm:p-0\n p-0 `} />',
109-
';<div\n class={` p-0\n sm:p-0 `}\n/>',
110-
],
99+
[';<div class=" m-0 sm:p-0 p-0 " />', ';<div class="m-0 p-0 sm:p-0" />'],
100+
[";<div class={' m-0 sm:p-0 p-0 '} />", ";<div class={'m-0 p-0 sm:p-0'} />"],
101+
[';<div class={` sm:p-0\n p-0 `} />', ';<div\n class={` p-0\n sm:p-0 `}\n/>'],
111102
[';<div class="flex flex" />', ';<div class="flex" />'],
112103
[';<div class={` flex flex `} />', ';<div class={`flex`} />'],
113104
[
@@ -154,10 +145,7 @@ let vue = [
154145
let tests = {
155146
html,
156147
lwc: html,
157-
vue: [
158-
...vue,
159-
t`<div :class="\`${yes} \${someVar} ${yes} \${'${yes}'}\`"></div>`,
160-
],
148+
vue: [...vue, t`<div :class="\`${yes} \${someVar} ${yes} \${'${yes}'}\`"></div>`],
161149
angular: [
162150
...vue.map((test) => test.map((t) => t.replace(/:class=/g, '[ngClass]='))),
163151
t`<div [ngClass]='\`${yes} \${someVar} ${yes} \${"${yes}"}\`'></div>`,
@@ -215,42 +203,33 @@ describe('parsers', () => {
215203
})
216204

217205
test('non-tailwind classes', () => {
218-
expect(
219-
format('<div class="sm:lowercase uppercase potato text-sm"></div>')
220-
).toEqual('<div class="potato text-sm uppercase sm:lowercase"></div>')
206+
expect(format('<div class="sm:lowercase uppercase potato text-sm"></div>')).toEqual(
207+
'<div class="potato text-sm uppercase sm:lowercase"></div>'
208+
)
221209
})
222210

223211
test('no prettier config', () => {
224-
expect(formatFixture('no-prettier-config')).toEqual(
225-
'<div class="bg-red-500 sm:bg-tomato"></div>'
226-
)
212+
expect(formatFixture('no-prettier-config')).toEqual('<div class="bg-red-500 sm:bg-tomato"></div>')
227213
})
228214

229215
test('parasite utilities', () => {
230-
expect(
231-
format('<div class="group peer unknown-class p-0 container"></div>')
232-
).toEqual('<div class="unknown-class group peer container p-0"></div>')
216+
expect(format('<div class="group peer unknown-class p-0 container"></div>')).toEqual(
217+
'<div class="unknown-class group peer container p-0"></div>'
218+
)
233219
})
234220

235221
test('inferred config path', () => {
236-
expect(formatFixture('basic')).toEqual(
237-
'<div class="bg-red-500 sm:bg-tomato"></div>'
238-
)
222+
expect(formatFixture('basic')).toEqual('<div class="bg-red-500 sm:bg-tomato"></div>')
239223
})
240224

241225
test('inferred config path (.cjs)', () => {
242-
expect(formatFixture('cjs')).toEqual(
243-
'<div class="bg-red-500 sm:bg-hotpink"></div>'
244-
)
226+
expect(formatFixture('cjs')).toEqual('<div class="bg-red-500 sm:bg-hotpink"></div>')
245227
})
246228

247229
test('explicit config path', () => {
248230
expect(
249231
format('<div class="sm:bg-tomato bg-red-500"></div>', {
250-
tailwindConfig: path.resolve(
251-
__dirname,
252-
'fixtures/basic/tailwind.config.js'
253-
),
232+
tailwindConfig: path.resolve(__dirname, 'fixtures/basic/tailwind.config.js'),
254233
})
255234
).toEqual('<div class="bg-red-500 sm:bg-tomato"></div>')
256235
})

0 commit comments

Comments
 (0)