Combines Prettier formatting with ESLint Standard linting into a unified command-line tool for JavaScript code quality.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
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.
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:
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' });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: trueFormats 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 codeFormats 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 formattedChecks 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)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'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 versionUsage 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, ... }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();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 languagePrettier 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">
};If no parser is specified in options, the parser is automatically determined:
'babel' for most JavaScript files'babel'All formatting functions may throw errors for:
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