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

Export types #162

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Export types for Prettier config ([#162](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/162))

### Fixed

- Don't move partial classes inside Liquid script attributes ([#164](https://github.com/tailwindlabs/prettier-plugin-tailwindcss/pull/164))
Expand Down
11 changes: 11 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,16 @@ esbuild.build({
})
},
},
{
name: 'copy-types',
setup(build) {
build.onEnd(() =>
fs.promises.copyFile(
path.resolve(__dirname, './src/index.d.ts'),
path.resolve(__dirname, './dist/index.d.ts'),
),
)
},
},
],
})
14 changes: 8 additions & 6 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
// @ts-check
import { expiringMap } from './expiring-map.js'
import clearModule from 'clear-module'
import escalade from 'escalade/sync'
import * as path from 'path'
import prettier from 'prettier'
import resolveFrom from 'resolve-from'
// @ts-ignore
import { generateRules as generateRulesFallback } from 'tailwindcss/lib/lib/generateRules'
// @ts-ignore
import { createContext as createContextFallback } from 'tailwindcss/lib/lib/setupContextUtils'
import loadConfigFallback from 'tailwindcss/loadConfig'
import resolveConfigFallback from 'tailwindcss/resolveConfig'
import { expiringMap } from './expiring-map.js'

/** @typedef {import('prettier').ParserOptions} ParserOptions **/
/** @typedef {import('./types').ContextContainer} ContextContainer **/
/** @typedef {import('./types').PluginOptions} PluginOptions **/

/**
* @template K
Expand All @@ -29,7 +31,7 @@ let pathToContextMap = expiringMap(10_000)
let prettierConfigCache = expiringMap(10_000)

/**
* @param {PluginOptions} options
* @param {ParserOptions} options
* @returns {ContextContainer}
*/
export function getTailwindConfig(options) {
Expand Down Expand Up @@ -59,7 +61,7 @@ export function getTailwindConfig(options) {

/**
*
* @param {PluginOptions} options
* @param {ParserOptions} options
* @returns {string | null}
*/
function getPrettierConfigPath(options) {
Expand All @@ -76,7 +78,7 @@ function getPrettierConfigPath(options) {
}

/**
* @param {PluginOptions} options
* @param {ParserOptions} options
* @returns {string}
*/
function getBaseDir(options) {
Expand Down Expand Up @@ -143,7 +145,7 @@ function loadTailwindConfig(baseDir, tailwindConfigPath) {
}

/**
* @param {PluginOptions} options
* @param {ParserOptions} options
* @param {string} baseDir
* @returns {string | null}
*/
Expand Down
27 changes: 27 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Parser, Printer, SupportOption } from 'prettier';

export interface PluginOptions {
/**
* Path to the Tailwind config file.
*/
tailwindConfig?: string

/**
* List of custom function and tag names that contain classes.
*/
tailwindFunctions: string[]

/**
* List of custom attributes that contain classes.
*/
tailwindAttributes: string[]
}

declare module 'prettier' {
interface RequiredOptions extends PluginOptions {}
interface ParserOptions extends PluginOptions {}
}

export const options: Record<keyof PluginOptions, SupportOption>
export const parsers: Record<string, Parser>
export const printers: Record<string, Printer>
23 changes: 17 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function createParser(parserFormat, transform, meta = {}) {
*
* @param {string} text
* @param {any} parsers
* @param {import('./types').PluginOptions} options
* @param {import('prettier').ParserOptions} options
* @returns
*/
parse(text, parsers, options = {}) {
Expand Down Expand Up @@ -446,12 +446,13 @@ function sortTemplateLiteral(node, { env }) {
}

/**
* @param {any} ast
* @param {import('@babel/types').Node} ast
* @param {TransformerContext} param1
*/
function transformJavaScript(ast, { env }) {
let { staticAttrs, functions } = env.customizations

/** @param {import('@babel/types').Node} ast */
function sortInside(ast) {
visit(ast, (node) => {
if (isStringLiteral(node)) {
Expand All @@ -467,6 +468,7 @@ function transformJavaScript(ast, { env }) {
}

visit(ast, {
/** @param {import('@babel/types').JSXAttribute} node */
JSXAttribute(node) {
if (!node.value) {
return
Expand All @@ -483,17 +485,26 @@ function transformJavaScript(ast, { env }) {
}
},

/** @param {import('@babel/types').CallExpression} node */
CallExpression(node) {
if (!node.arguments?.length) return
if (!functions.has(node.callee?.name ?? '')) return
if (!node.arguments?.length) {
return
}

if (!functions.has(node.callee?.name ?? '')) {
return
}

node.arguments.forEach((arg) => sortInside(arg))
},

/** @param {import('@babel/types').TaggedTemplateExpression} node */
TaggedTemplateExpression(node) {
if (node.tag.type === 'Identifier' && functions.has(node.tag.name)) {
sortTemplateLiteral(node.quasi, { env })
if (node.tag.type !== 'Identifier' || !functions.has(node.tag.name)) {
return
}

sortTemplateLiteral(node.quasi, { env })
},
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export const options = {
},
}

/** @typedef {import('./types').PluginOptions} PluginOptions */
/** @typedef {import('prettier').RequiredOptions} RequiredOptions */
/** @typedef {import('./types').Customizations} Customizations */

/**
* @param {PluginOptions} options
* @param {RequiredOptions} options
* @param {string} parser
* @param {Customizations} defaults
* @returns {Customizations}
Expand Down
17 changes: 7 additions & 10 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@ export interface TransformerEnv {
options: any
}

export interface RawOptions {
tailwindConfig?: string
tailwindFunctions?: string[]
tailwindAttributes?: string[]
export interface ContextContainer {
context: any
generateRules: () => any
tailwindConfig: any
}

export interface InternalOptions {
printer: Printer<any>
}

export interface ContextContainer {
context: any
generateRules: () => any
tailwindConfig: any
declare module 'prettier' {
interface RequiredOptions extends InternalOptions {}
interface ParserOptions extends InternalOptions {}
}

export type PluginOptions = ParserOptions<any> & RawOptions & InternalOptions