CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prettier-standard

Combines Prettier formatting with ESLint Standard linting into a unified command-line tool for JavaScript code quality.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

formatting.mddocs/

Code Formatting

Core formatting functionality that transforms JavaScript code using Prettier with Standard-compliant defaults. These functions provide direct access to formatting operations without file system interactions.

Capabilities

Format Function

Formats source code using Prettier with Standard-compliant defaults.

/**
 * Formats source code using Prettier with Standard-compliant defaults
 * @param source - JavaScript source code to format
 * @param options - Optional formatting options merged with Standard defaults
 * @returns Formatted source code string
 */
function format(source, options);

Default formatting options:

  • Single quotes instead of double quotes
  • No semicolons
  • Space before function parentheses
  • Generator and yield star spacing enabled
  • JSX single quotes

Usage Examples:

const { format } = require('prettier-standard');

// Basic formatting
const code = 'function hello(){return"world";}';
const formatted = format(code);
// Result: 'function hello () {\n  return \'world\'\n}\n'

// With custom options
const customFormatted = format(code, { 
  semi: true,
  singleQuote: false 
});
// Result: 'function hello () {\n  return "world";\n}\n'

// TypeScript/JSX formatting
const jsxCode = '<div className="test">Hello</div>';
const formattedJsx = format(jsxCode, { parser: 'babel' });

Check Function

Checks if source code is already formatted according to Standard rules without modifying it.

/**
 * Checks if source code is already formatted according to Standard rules
 * @param source - JavaScript source code to check
 * @param options - Optional formatting options merged with Standard defaults
 * @returns Boolean indicating if code is properly formatted
 */
function check(source, options);

Usage Examples:

const { check } = require('prettier-standard');

// Check properly formatted code
const goodCode = 'function hello () {\n  return \'world\'\n}\n';
const isValid = check(goodCode);
// Result: true

// Check improperly formatted code
const badCode = 'function hello(){return"world";}';
const isInvalid = check(badCode);
// Result: false

// Check with custom options
const semiCode = 'function hello () {\n  return "world";\n}\n';
const isValidSemi = check(semiCode, { semi: true, singleQuote: false });
// Result: true

Format With Cursor

Formats source code while maintaining cursor position information.

/**
 * Formats source code while maintaining cursor position
 * @param source - JavaScript source code to format
 * @param options - Formatting options including cursorOffset
 * @returns Object with formatted text and cursor position
 */
function formatWithCursor(source, options);

Usage Examples:

const { formatWithCursor } = require('prettier-standard');

const result = formatWithCursor('function hello(){return"world";}', {
  cursorOffset: 15 // Position in original code
});

console.log(result.formatted);
// Result: 'function hello () {\n  return \'world\'\n}\n'
console.log(result.cursorOffset);
// Result: New cursor position in formatted code

Format With Ranges

Formats specific character ranges within source code, useful for formatting only changed portions.

/**
 * Formats specific ranges within source code
 * @param source - JavaScript source code to format
 * @param ranges - Array of range objects with rangeStart and rangeEnd properties
 * @param options - Optional formatting options
 * @returns Formatted source code string
 */
function formatWithRanges(source, ranges, options);

Usage Examples:

const { formatWithRanges } = require('prettier-standard');

const code = 'function hello(){return"world";}\nfunction goodbye(){return"farewell";}';
const ranges = [
  { rangeStart: 0, rangeEnd: 30 } // Format only first function
];

const formatted = formatWithRanges(code, ranges);
// Only the first function gets formatted

Check With Ranges

Checks if specific character ranges within source code are properly formatted.

/**
 * Checks if specific ranges within source code are properly formatted
 * @param source - JavaScript source code to check
 * @param ranges - Array of range objects with rangeStart and rangeEnd properties
 * @param options - Optional formatting options
 * @returns Boolean indicating if all ranges are properly formatted
 */
function checkWithRanges(source, ranges, options);

Usage Examples:

const { checkWithRanges } = require('prettier-standard');

const code = 'function hello () {\n  return \'world\'\n}\nfunction bad(){return"bad";}';
const ranges = [
  { rangeStart: 0, rangeEnd: 35 } // Check only first function
];

const isValid = checkWithRanges(code, ranges);
// Result: true (only first function is checked)

Get File Info

Gets file information for determining parser and formatting capabilities.

/**
 * Gets file information for a given file path
 * @param filePath - Path to file for analysis
 * @param options - Optional file info options
 * @returns File information object with parser and formatting details
 */
function getFileInfo(filePath, options);

Usage Examples:

const { getFileInfo } = require('prettier-standard');

const fileInfo = getFileInfo('example.js');
console.log(fileInfo.inferredParser); // 'babel'
console.log(fileInfo.ignored); // false

const tsInfo = getFileInfo('example.ts');
console.log(tsInfo.inferredParser); // '@typescript-eslint/parser'

Resolve Config

Resolves Prettier configuration for a given file path.

/**
 * Resolves Prettier configuration for a given file path
 * @param filePath - Path to resolve configuration for
 * @param options - Configuration resolution options
 * @returns Promise resolving to configuration object or null
 */
resolveConfig.sync(filePath, options);
resolveConfig(filePath, options); // Promise version

Usage Examples:

const { resolveConfig } = require('prettier-standard');

// Synchronous config resolution
const config = resolveConfig.sync('/path/to/file.js', {
  editorconfig: true
});

// Asynchronous config resolution
const config = await resolveConfig('/path/to/file.js', {
  editorconfig: true
});

console.log(config); // { singleQuote: true, semi: false, ... }

Clear Config Cache

Clears the internal configuration cache.

/**
 * Clears the internal configuration cache
 */
function clearConfigCache();

Usage Examples:

const { clearConfigCache } = require('prettier-standard');

// Clear cache when configuration files change
clearConfigCache();

Get Support Info

Gets information about supported languages, parsers, and file extensions.

/**
 * Gets information about supported languages, parsers, and file extensions
 * @returns Object containing support information
 */
function getSupportInfo();

Usage Examples:

const { getSupportInfo } = require('prettier-standard');

const supportInfo = getSupportInfo();
console.log(supportInfo.languages); // Array of supported languages
console.log(supportInfo.languages[0].extensions); // File extensions for first language

Configuration Options

Standard Defaults

Prettier Standard applies these defaults to ensure Standard compliance:

const standardDefaults = {
  spaceBeforeFunctionParen: true,  // function () instead of function()
  generatorStarSpacing: true,      // function * gen() instead of function* gen()
  yieldStarSpacing: true,          // yield * expr instead of yield* expr
  singleQuote: true,               // 'hello' instead of "hello"
  semi: false,                     // no semicolons
  jsxSingleQuote: true             // <div className='test'> instead of <div className="test">
};

Parser Selection

If no parser is specified in options, the parser is automatically determined:

  • Default parser: 'babel' for most JavaScript files
  • For stdin without filepath: 'babel'
  • File extension-based detection for TypeScript, CSS, JSON, etc.

Error Handling

All formatting functions may throw errors for:

  • Syntax Errors: Unparseable code will throw with descriptive error messages
  • Unsupported File Types: Files with unsupported extensions or content
  • Invalid Options: Malformed or conflicting formatting options

Example error handling:

const { format } = require('prettier-standard');

try {
  const formatted = format('function broken( { // Invalid syntax');
} catch (error) {
  if (error.message.includes('SyntaxError')) {
    console.error('Code has syntax errors:', error.message);
  }
}

Install with Tessl CLI

npx tessl i tessl/npm-prettier-standard

docs

cli.md

file-processing.md

formatting.md

index.md

tile.json