├── .gitignore ├── .travis.yml ├── bin.js ├── collaborators.md ├── index.js ├── package.json ├── rc └── esformatter.json ├── readme.md └── test ├── es6.js ├── failing ├── jsx.js ├── multiline.js ├── obfuscated-files │ └── standard-format-torture.js ├── obfuscated.js └── singleline.js ├── index.js ├── jsx.js ├── multiline.js ├── shebang.js ├── singleline.js ├── test-files └── test.js └── testFile.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '0.12' 4 | - '0.11' 5 | - '0.10' 6 | sudo: false # Enable docker based containers 7 | cache: 8 | directories: # Cache npm install 9 | - node_modules 10 | -------------------------------------------------------------------------------- /bin.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const fmt = require('./') 4 | const fs = require('fs') 5 | const stdin = require('stdin') 6 | const argv = require('minimist')(process.argv.slice(2), { 7 | boolean: ['help', 'stdin', 'version', 'write'], 8 | alias: { 9 | h: 'help', 10 | w: 'write', 11 | v: 'version' 12 | } 13 | }) 14 | 15 | // running `standard-format -` is equivalent to `standard-format --stdin` 16 | if (argv._[0] === '-') { 17 | argv.stdin = true 18 | argv._.shift() 19 | } 20 | 21 | if (argv.help) { 22 | console.log(function () { 23 | /* 24 | standard-format - Auto formatter for the easier cases in standard 25 | 26 | Usage: 27 | standard-format [FILES...] 28 | 29 | If FILES is omitted, then all JavaScript source files (*.js) in the current 30 | working directory are checked, recursively. 31 | 32 | These paths are automatically excluded: 33 | node_modules/, .git/, *.min.js, bundle.js 34 | 35 | Flags: 36 | --stdin Read file text from stdin. 37 | -w, --write Directly modify input files. 38 | -v, --version Show current version. 39 | -h, --help Show usage information. 40 | 41 | Readme: https://github.com/maxogden/standard-format 42 | 43 | */ 44 | }.toString().split(/\n/).slice(2, -2).join('\n')) 45 | process.exit(0) 46 | } 47 | 48 | if (argv.version) { 49 | console.log(require('./package.json').version) 50 | process.exit(0) 51 | } 52 | 53 | function processFile (transformed) { 54 | if (argv.write && transformed.name !== 'stdin') { 55 | fs.writeFileSync(transformed.name, transformed.data) 56 | } else { 57 | process.stdout.write(transformed.data) 58 | } 59 | } 60 | 61 | function getFiles (done) { 62 | const args = argv._ 63 | if (argv.stdin) { 64 | return stdin(function (file) { 65 | return done(null, [{ name: 'stdin', data: file }]) 66 | }) 67 | } else if (args.length === 0) { 68 | return fmt.load(done) 69 | } else { 70 | return done(null, args.map(function (file) { 71 | return { name: file, data: fs.readFileSync(file).toString() } 72 | })) 73 | } 74 | } 75 | 76 | getFiles(function (err, files) { 77 | if (err) return error(err) 78 | files.forEach(function (file) { 79 | try { 80 | file.data = fmt.transform(file.data) 81 | processFile(file) 82 | } catch (e) { error(file.name + ': ' + e) } 83 | }) 84 | }) 85 | 86 | function error (err) { 87 | console.error(err) 88 | process.exit(1) 89 | } 90 | -------------------------------------------------------------------------------- /collaborators.md: -------------------------------------------------------------------------------- 1 | ## Collaborators 2 | 3 | standard-format is only possible due to the excellent work of the following collaborators: 4 | 5 | 6 | 7 | 8 | 9 | 10 |
maxogdenGitHub/maxogden
jb55GitHub/jb55
ferossGitHub/feross
bcomnesGitHub/bcomnes
FletGitHub/Flet
11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const deglob = require('deglob') 2 | const fs = require('fs') 3 | const formatter = require('esformatter') 4 | 5 | const ESFORMATTER_CONFIG = require('./rc/esformatter.json') 6 | const DEFAULT_IGNORE = [ 7 | 'node_modules/**', 8 | '.git/**', 9 | '**/*.min.js', 10 | '**/bundle.js' 11 | ] 12 | 13 | const MULTI_NEWLINE_N = /((?:\n){3,})/g 14 | const MULTI_NEWLINE_RN = /((?:\r\n){3,})/g 15 | 16 | const EOL_SEMICOLON = /;(?=\r?\n)/g 17 | const EOL_SEMICOLON_WITH_COMMENT = /;(?=\s*\/[/*][\s\w*/]*\r?\n)/g 18 | const SOF_NEWLINES = /^(\r?\n)+/g 19 | 20 | module.exports.transform = function (file) { 21 | file = file 22 | .replace(MULTI_NEWLINE_N, '\n\n') 23 | .replace(MULTI_NEWLINE_RN, '\r\n\r\n') 24 | .replace(EOL_SEMICOLON, '') 25 | .replace(EOL_SEMICOLON_WITH_COMMENT, '') 26 | .replace(SOF_NEWLINES, '') 27 | 28 | const formatted = formatter.format(file, ESFORMATTER_CONFIG) 29 | .replace(EOL_SEMICOLON, '') 30 | // run replace again; esformatter-semicolon-first will re-add semicolon at EOL 31 | 32 | return formatted 33 | } 34 | 35 | module.exports.load = function (opts, cb) { 36 | if (typeof opts === 'function') { 37 | cb = opts 38 | opts = {} 39 | } 40 | if (!opts) opts = {} 41 | 42 | let ignore = [].concat(DEFAULT_IGNORE) // globs to ignore 43 | if (opts.ignore) ignore = ignore.concat(opts.ignore) 44 | 45 | const deglobOpts = { 46 | ignore, 47 | cwd: opts.cwd || process.cwd(), 48 | useGitIgnore: true, 49 | usePackageJson: true, 50 | configKey: 'standard' 51 | } 52 | 53 | deglob(['**/*.js', '**/*.jsx'], deglobOpts, function (err, files) { 54 | if (err) return cb(err) 55 | 56 | files = files.map(function (f) { 57 | return { name: f, data: fs.readFileSync(f).toString() } // assume utf8 58 | }) 59 | cb(null, files) 60 | }) 61 | } 62 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "standard-format", 3 | "version": "2.2.4", 4 | "description": "attempts to reformat javascript to comply with feross/standard style", 5 | "main": "index.js", 6 | "bin": "./bin.js", 7 | "scripts": { 8 | "test": "standard && tape test/*.js | tap-spec", 9 | "test-file": "=1", 62 | "CallExpression" : -1, 63 | "CallExpressionOpeningParentheses" : 0, 64 | "CallExpressionClosingParentheses" : -1, 65 | "ClassDeclaration" : ">=1", 66 | "ClassDeclarationOpeningBrace" : 0, 67 | "ClassDeclarationClosingBrace" : "<=1", 68 | "ConditionalExpression" : ">=1", 69 | "CatchOpeningBrace" : 0, 70 | "CatchClosingBrace" : "<=1", 71 | "CatchKeyword": 0, 72 | "DeleteOperator" : -1, 73 | "DoWhileStatement" : -1, 74 | "DoWhileStatementOpeningBrace" : 0, 75 | "DoWhileStatementClosingBrace" : "<=1", 76 | "EndOfFile" : 1, 77 | "EmptyStatement" : -1, 78 | "FinallyKeyword" : -1, 79 | "FinallyOpeningBrace" : 0, 80 | "FinallyClosingBrace" : "<=1", 81 | "ForInStatement" : -1, 82 | "ForInStatementExpressionOpening" : -1, 83 | "ForInStatementExpressionClosing" : -1, 84 | "ForInStatementOpeningBrace" : 0, 85 | "ForInStatementClosingBrace" : "<=1", 86 | "ForOfStatement" : ">=1", 87 | "ForOfStatementExpressionOpening" : 0, 88 | "ForOfStatementExpressionClosing" : 0, 89 | "ForOfStatementOpeningBrace" : 0, 90 | "ForOfStatementClosingBrace" : "<=1", 91 | "ForStatement" : -1, 92 | "ForStatementExpressionOpening" : -1, 93 | "ForStatementExpressionClosing" : -1, 94 | "ForStatementOpeningBrace" : 0, 95 | "ForStatementClosingBrace" : "<=1", 96 | "FunctionExpression" : -1, 97 | "FunctionExpressionOpeningBrace" : 0, 98 | "FunctionExpressionClosingBrace" : "<=1", 99 | "FunctionDeclaration" : -1, 100 | "FunctionDeclarationOpeningBrace" : 0, 101 | "FunctionDeclarationClosingBrace" : "<=1", 102 | "IIFEClosingParentheses" : 0, 103 | "IfStatement" : -1, 104 | "IfStatementOpeningBrace" : 0, 105 | "IfStatementClosingBrace" : "<=1", 106 | "ElseIfStatement" : -1, 107 | "ElseIfStatementOpeningBrace" : 0, 108 | "ElseIfStatementClosingBrace" : "<=1", 109 | "ElseStatement" : 0, 110 | "ElseStatementOpeningBrace" : 0, 111 | "ElseStatementClosingBrace" : "<=1", 112 | "LogicalExpression" : -1, 113 | "MethodDefinition": ">=1", 114 | "MemberExpressionOpening": 0, 115 | "MemberExpressionClosing": 0, 116 | "MemberExpressionPeriod": -1, 117 | "ObjectExpressionClosingBrace" : -1, 118 | "ObjectPatternOpeningBrace": 0, 119 | "ObjectPatternClosingBrace": 0, 120 | "ObjectPatternComma": 0, 121 | "Property" : -1, 122 | "PropertyValue" : 0, 123 | "ReturnStatement" : -1, 124 | "SwitchOpeningBrace" : 0, 125 | "SwitchClosingBrace" : -1, 126 | "ThisExpression" : -1, 127 | "ThrowStatement" : -1, 128 | "TryKeyword": -1, 129 | "TryOpeningBrace" : 0, 130 | "TryClosingBrace" : "<=1", 131 | "VariableName" : -1, 132 | "VariableValue" : -1, 133 | "VariableDeclaration" : -1, 134 | "VariableDeclarationSemiColon": -1, 135 | "VariableDeclarationWithoutInit" : -1, 136 | "WhileStatement" : -1, 137 | "WhileStatementOpeningBrace" : 0, 138 | "WhileStatementClosingBrace" : "<=1" 139 | }, 140 | 141 | "after" : { 142 | "AssignmentExpression" : -1, 143 | "AssignmentOperator" : -1, 144 | "AssignmentPattern" : 0, 145 | "ArrayPatternOpening": 0, 146 | "ArrayPatternClosing": 0, 147 | "ArrayPatternComma": 0, 148 | "ArrowFunctionExpressionArrow": 0, 149 | "ArrowFunctionExpressionOpeningBrace": ">=1", 150 | "ArrowFunctionExpressionClosingBrace": -1, 151 | "BlockStatement" : -1, 152 | "BreakKeyword": -1, 153 | "CallExpression" : -1, 154 | "CallExpressionOpeningParentheses" : -1, 155 | "CallExpressionClosingParentheses" : -1, 156 | "ClassDeclaration" : ">=1", 157 | "ClassDeclarationOpeningBrace" : ">=1", 158 | "ClassDeclarationClosingBrace" : ">=1", 159 | "CatchOpeningBrace" : "<=1", 160 | "CatchClosingBrace" : -1, 161 | "CatchKeyword": 0, 162 | "ConditionalExpression" : -1, 163 | "DeleteOperator" : -1, 164 | "DoWhileStatement" : -1, 165 | "DoWhileStatementOpeningBrace" : "<=1", 166 | "DoWhileStatementClosingBrace" : -1, 167 | "EmptyStatement" : -1, 168 | "FinallyKeyword" : -1, 169 | "FinallyOpeningBrace" : "<=2", 170 | "FinallyClosingBrace" : -1, 171 | "ForInStatement" : -1, 172 | "ForInStatementExpressionOpening" : -1, 173 | "ForInStatementExpressionClosing" : -1, 174 | "ForInStatementOpeningBrace" : "<=1", 175 | "ForInStatementClosingBrace" : -1, 176 | "ForOfStatement" : -1, 177 | "ForOfStatementExpressionOpening" : "<2", 178 | "ForOfStatementExpressionClosing" : -1, 179 | "ForOfStatementOpeningBrace" : ">=1", 180 | "ForOfStatementClosingBrace" : ">=1", 181 | "ForStatement" : -1, 182 | "ForStatementExpressionOpening" : -1, 183 | "ForStatementExpressionClosing" : -1, 184 | "ForStatementOpeningBrace" : "<=1", 185 | "ForStatementClosingBrace" : -1, 186 | "FunctionExpression" : -1, 187 | "FunctionExpressionOpeningBrace" : "<=1", 188 | "FunctionExpressionClosingBrace" : -1, 189 | "FunctionDeclaration" : -1, 190 | "FunctionDeclarationOpeningBrace" : "<=1", 191 | "FunctionDeclarationClosingBrace" : -1, 192 | "IIFEOpeningParentheses" : 0, 193 | "IfStatement" : -1, 194 | "IfStatementOpeningBrace" : "<=1", 195 | "IfStatementClosingBrace" : -1, 196 | "ElseIfStatement" : -1, 197 | "ElseIfStatementOpeningBrace" : "<=1", 198 | "ElseIfStatementClosingBrace" : -1, 199 | "ElseStatement" : -1, 200 | "ElseStatementOpeningBrace" : "<=1", 201 | "ElseStatementClosingBrace" : -1, 202 | "LogicalExpression" : -1, 203 | "MethodDefinition": ">=1", 204 | "MemberExpressionOpening": 0, 205 | "MemberExpressionClosing" : ">=0", 206 | "MemberExpressionPeriod": 0, 207 | "ObjectExpressionOpeningBrace" : "<=1", 208 | "ObjectPatternOpeningBrace": 0, 209 | "ObjectPatternClosingBrace": 0, 210 | "ObjectPatternComma": 0, 211 | "Property" : -1, 212 | "PropertyName" : 0, 213 | "ReturnStatement" : -1, 214 | "SwitchOpeningBrace" : "<=1", 215 | "SwitchClosingBrace" : -1, 216 | "SwitchCaseColon": ">=1", 217 | "ThisExpression" : -1, 218 | "ThrowStatement" : -1, 219 | "TryKeyword": -1, 220 | "TryOpeningBrace" : "<=1", 221 | "TryClosingBrace" : -1, 222 | "VariableValue" : -1, 223 | "VariableDeclaration" : -1, 224 | "VariableDeclarationSemiColon" : ">=1", 225 | "WhileStatement" : -1, 226 | "WhileStatementOpeningBrace" : "<=1", 227 | "WhileStatementClosingBrace" : -1 228 | } 229 | }, 230 | 231 | 232 | "whiteSpace" : { 233 | "value" : " ", 234 | "removeTrailing" : 1, 235 | 236 | "before" : { 237 | "AssignmentPattern" : 1, 238 | "ArrayExpressionOpening" : -1, 239 | "ArrayExpressionClosing" : -1, 240 | "ArrayExpressionComma" : -1, 241 | "ArrayPatternOpening": 1, 242 | "ArrayPatternClosing": 0, 243 | "ArrayPatternComma": 0, 244 | "ArrowFunctionExpressionArrow": 1, 245 | "ArrowFunctionExpressionOpeningBrace": 1, 246 | "ArrowFunctionExpressionClosingBrace": 0, 247 | "ArgumentComma" : -1, 248 | "ArgumentList" : 0, 249 | "AssignmentOperator" : 1, 250 | "BinaryExpression": -1, 251 | "BinaryExpressionOperator" : 1, 252 | "BlockComment" : 1, 253 | "CallExpression" : -1, 254 | "CallExpressionOpeningParentheses" : 0, 255 | "CallExpressionClosingParentheses" : -1, 256 | "CatchParameterList" : 0, 257 | "CatchOpeningBrace" : 1, 258 | "CatchClosingBrace" : 1, 259 | "CatchKeyword" : 1, 260 | "CommaOperator" : 0, 261 | "ClassDeclarationOpeningBrace" : 1, 262 | "ClassDeclarationClosingBrace" : 1, 263 | "ConditionalExpressionConsequent" : 1, 264 | "ConditionalExpressionAlternate" : 1, 265 | "DoWhileStatementOpeningBrace" : 1, 266 | "DoWhileStatementClosingBrace" : 1, 267 | "DoWhileStatementConditional" : 1, 268 | "EmptyStatement" : 0, 269 | "ExpressionClosingParentheses": 0, 270 | "FinallyKeyword" : -1, 271 | "FinallyOpeningBrace" : 1, 272 | "FinallyClosingBrace" : 1, 273 | "ForInStatement" : 1, 274 | "ForInStatementExpressionOpening" : 1, 275 | "ForInStatementExpressionClosing" : 0, 276 | "ForInStatementOpeningBrace" : 1, 277 | "ForInStatementClosingBrace" : 1, 278 | "ForOfStatement" : 1, 279 | "ForOfStatementExpressionOpening" : 1, 280 | "ForOfStatementExpressionClosing" : 0, 281 | "ForOfStatementOpeningBrace" : 1, 282 | "ForOfStatementClosingBrace" : 1, 283 | "ForStatement" : 1, 284 | "ForStatementExpressionOpening" : 1, 285 | "ForStatementExpressionClosing" : 0, 286 | "ForStatementOpeningBrace" : 1, 287 | "ForStatementClosingBrace" : -1, 288 | "ForStatementSemicolon" : 0, 289 | "FunctionDeclarationOpeningBrace" : 1, 290 | "FunctionDeclarationClosingBrace" : -1, 291 | "FunctionExpressionOpeningBrace" : 1, 292 | "FunctionExpressionClosingBrace" : -1, 293 | "FunctionGeneratorAsterisk": 1, 294 | "FunctionName" : 1, 295 | "IIFEClosingParentheses" : 0, 296 | "IfStatementConditionalOpening" : 1, 297 | "IfStatementConditionalClosing" : 0, 298 | "IfStatementOpeningBrace" : 1, 299 | "IfStatementClosingBrace" : -1, 300 | "ModuleSpecifierClosingBrace": 1, 301 | "ElseStatementOpeningBrace" : 1, 302 | "ElseStatementClosingBrace" : -1, 303 | "ElseIfStatementOpeningBrace" : 1, 304 | "ElseIfStatementClosingBrace" : -1, 305 | "LineComment" : 1, 306 | "LogicalExpressionOperator" : 1, 307 | "MemberExpressionOpening": 0, 308 | "MemberExpressionClosing" : -1, 309 | "MemberExpressionPeriod": 0, 310 | "ObjectExpressionOpeningBrace": -1, 311 | "ObjectExpressionClosingBrace": -1, 312 | "ObjectPatternOpeningBrace": -1, 313 | "ObjectPatternClosingBrace": -1, 314 | "ObjectPatternComma": 0, 315 | "Property" : -1, 316 | "PropertyValue" : 1, 317 | "ParameterComma" : -1, 318 | "ParameterList" : 0, 319 | "SwitchDiscriminantOpening" : 1, 320 | "SwitchDiscriminantClosing" : -1, 321 | "SwitchCaseColon": 0, 322 | "ThrowKeyword": -1, 323 | "TryKeyword": -1, 324 | "TryOpeningBrace" : 1, 325 | "TryClosingBrace" : -1, 326 | "UnaryExpressionOperator": -1, 327 | "VariableName" : -1, 328 | "VariableValue" : 1, 329 | "VariableDeclarationSemiColon" : 0, 330 | "WhileStatementConditionalOpening" : -1, 331 | "WhileStatementConditionalClosing" : -1, 332 | "WhileStatementOpeningBrace" : -1, 333 | "WhileStatementClosingBrace" : -1 334 | }, 335 | 336 | "after" : { 337 | "AssignmentPattern" : 1, 338 | "ArrayExpressionOpening" : -1, 339 | "ArrayExpressionClosing" : -1, 340 | "ArrayExpressionComma" : 1, 341 | "ArrayPatternOpening": 0, 342 | "ArrayPatternClosing": 1, 343 | "ArrayPatternComma": 1, 344 | "ArrowFunctionExpressionArrow": 1, 345 | "ArrowFunctionExpressionOpeningBrace": 0, 346 | "ArrowFunctionExpressionClosingBrace": 0, 347 | "ArgumentComma" : 1, 348 | "ArgumentList" : 0, 349 | "AssignmentOperator" : 1, 350 | "BinaryExpression": -1, 351 | "BinaryExpressionOperator" : 1, 352 | "BlockComment" : -1, 353 | "CallExpression" : -1, 354 | "CallExpressionOpeningParentheses" : -1, 355 | "CallExpressionClosingParentheses" : -1, 356 | "CatchParameterList" : -1, 357 | "CatchOpeningBrace" : -1, 358 | "CatchClosingBrace" : -1, 359 | "CatchKeyword" : -1, 360 | "ClassDeclarationOpeningBrace" : 1, 361 | "ClassDeclarationClosingBrace" : 1, 362 | "CommaOperator" : 1, 363 | "ConditionalExpressionConsequent" : 1, 364 | "ConditionalExpressionTest" : 1, 365 | "DoWhileStatementOpeningBrace" : -1, 366 | "DoWhileStatementClosingBrace" : -1, 367 | "DoWhileStatementBody" : -1, 368 | "EmptyStatement" : -1, 369 | "ExpressionOpeningParentheses" : 0, 370 | "FinallyKeyword" : -1, 371 | "FinallyOpeningBrace" : -1, 372 | "FinallyClosingBrace" : -1, 373 | "ForInStatement" : -1, 374 | "ForInStatementExpressionOpening" : -1, 375 | "ForInStatementExpressionClosing" : -1, 376 | "ForInStatementOpeningBrace" : -1, 377 | "ForInStatementClosingBrace" : -1, 378 | "ForStatement" : -1, 379 | "ForStatementExpressionOpening" : -1, 380 | "ForStatementExpressionClosing" : -1, 381 | "ForStatementClosingBrace" : -1, 382 | "ForStatementOpeningBrace" : -1, 383 | "ForStatementSemicolon" : -1, 384 | "FunctionReservedWord": 1, 385 | "FunctionName" : 1, 386 | "FunctionExpressionOpeningBrace" : -1, 387 | "FunctionExpressionClosingBrace" : -1, 388 | "FunctionDeclarationOpeningBrace" : -1, 389 | "FunctionDeclarationClosingBrace" : -1, 390 | "IIFEOpeningParentheses" : 0, 391 | "IfStatementConditionalOpening" : 0, 392 | "IfStatementConditionalClosing" : -1, 393 | "IfStatementOpeningBrace" : -1, 394 | "IfStatementClosingBrace" : -1, 395 | "ModuleSpecifierOpeningBrace": 1, 396 | "ElseStatementOpeningBrace" : -1, 397 | "ElseStatementClosingBrace" : -1, 398 | "ElseIfStatementOpeningBrace" : -1, 399 | "ElseIfStatementClosingBrace" : -1, 400 | "MemberExpressionOpening" : -1, 401 | "MemberExpressionClosing": 0, 402 | "MemberExpressionPeriod": 0, 403 | "MethodDefinitionName" : 1, 404 | "LogicalExpressionOperator" : 1, 405 | "ObjectExpressionOpeningBrace": -1, 406 | "ObjectExpressionClosingBrace": -1, 407 | "ObjectPatternOpeningBrace": -1, 408 | "ObjectPatternClosingBrace": -1, 409 | "ObjectPatternComma": 1, 410 | "PropertyName" : 0, 411 | "PropertyValue" : -1, 412 | "ParameterComma" : 1, 413 | "ParameterList" : 0, 414 | "SwitchDiscriminantOpening" : -1, 415 | "SwitchDiscriminantClosing" : 1, 416 | "ThrowKeyword": -1, 417 | "TryKeyword": -1, 418 | "TryOpeningBrace" : -1, 419 | "TryClosingBrace" : -1, 420 | "UnaryExpressionOperator": -1, 421 | "VariableName" : 1, 422 | "VariableValue" : 0, 423 | "VariableDeclarationSemiColon": -1, 424 | "WhileStatementConditionalOpening" : -1, 425 | "WhileStatementConditionalClosing" : -1, 426 | "WhileStatementOpeningBrace" : -1, 427 | "WhileStatementClosingBrace" : -1 428 | } 429 | }, 430 | 431 | "jsx": { 432 | "formatJSX": true, 433 | "attrsOnSameLineAsTag": false, 434 | "maxAttrsOnTag": 3, 435 | "firstAttributeOnSameLine": false, 436 | "alignWithFirstAttribute": false, 437 | "spaceInJSXExpressionContainers": "", 438 | "htmlOptions": { 439 | "brace_style": "collapse", 440 | "indent_char": " ", 441 | "indent_size": 2, 442 | "max_preserve_newlines": 2, 443 | "preserve_newlines": true 444 | } 445 | }, 446 | 447 | "plugins": [ 448 | "esformatter-quotes", 449 | "esformatter-literal-notation", 450 | "esformatter-remove-trailing-commas", 451 | "esformatter-semicolon-first", 452 | "esformatter-spaced-lined-comment", 453 | "esformatter-jsx" 454 | ], 455 | 456 | "quotes": { 457 | "type": "single", 458 | "avoidEscape": true 459 | } 460 | } 461 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # standard-format 2 | 3 | [![Build Status](https://travis-ci.org/maxogden/standard-format.svg)](https://travis-ci.org/maxogden/standard-format) 4 | [![Dependency Status](https://david-dm.org/maxogden/standard-format.svg?style=flat-square)](https://david-dm.org/maxogden/standard-format) 5 | 6 | **experimental** auto formatter for the easier cases in [standard](https://www.npmjs.com/package/standard) 7 | 8 | # DEPRECATED 9 | 10 | ### Use `standard --fix` instead of `standard-format`. 11 | 12 | This package is no longer maintained. 13 | 14 | `standard` v8.0.0 contains a new `--fix` command line flag to automatically fix 15 | problems. If you need ES2015+ support, consider using `standard --fix` instead 16 | of `standard-format` (this package) which may mangle your ES2015+ code. 17 | 18 | `standard --fix` is built into `standard` v8.0.0 for maximum convenience, it 19 | supports ES2015, and it's lightweight (no additional dependencies since it's part 20 | of ESLint which powers `standard`). Lots of problems are already fixable, and more 21 | are getting added with each ESLint release. 22 | 23 | `standard` also outputs a message ("Run `standard --fix` to automatically fix 24 | some problems.") when it detects problems that can be fixed automatically so you 25 | can save time! 26 | 27 | ## Installation 28 | 29 | Install with npm 30 | 31 | $ npm install -g standard-format 32 | 33 | ## Example Usage 34 | 35 | Output all formatted javascript in a directory and subdirectories to stdout 36 | 37 | $ standard-format 38 | 39 | Format all javascript files, overwriting them into standard format 40 | 41 | $ standard-format -w 42 | 43 | Format javascript over stdin 44 | 45 | $ standard-format < file.js > formatted-file.js 46 | 47 | Format and overwrite specific files 48 | 49 | $ standard-format -w file1.js file2.js 50 | 51 | ### Development tasks for task runners 52 | 53 | - Gulp 54 | - [gulp-standard-format](https://github.com/Andruj/gulp-standard-format/) 55 | - [gulp-standard-bundle](https://github.com/ggarciao/gulp-standard-bundle) 56 | - Grunt [grunt-standard](https://github.com/EasyAsABC123/grunt-standard) 57 | 58 | ### Editor plugins 59 | 60 | - Sublime Text: [sublime-standard-format](https://packagecontrol.io/packages/StandardFormat) 61 | - Atom: [atom-standard-formatter](https://atom.io/packages/standard-formatter) 62 | 63 | ### Science :mortar_board: 64 | 65 | > A new step should be added to the modification cycle: modifying the program to make it readable. 66 | 67 | [Elshoff & Marcotty, 1982](http://dl.acm.org/citation.cfm?id=358596) 68 | -------------------------------------------------------------------------------- /test/es6.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const fmt = require('../').transform 3 | 4 | const classes = [ 5 | { 6 | program: [ 7 | 'export class com extends Component {', 8 | ' render() {}', 9 | '}', 10 | '' 11 | ].join('\n'), 12 | 13 | expected: [ 14 | 'export class com extends Component {', 15 | ' render () {}', 16 | '}', 17 | '' 18 | ].join('\n'), 19 | 20 | msg: 'class methods should have correct spacing', 21 | issues: [ 22 | 'https://github.com/maxogden/standard-format/issues/126', 23 | 'https://github.com/millermedeiros/esformatter/issues/384', 24 | 'https://github.com/maxogden/standard-format/issues/111', 25 | 'https://github.com/maxogden/standard-format/issues/75' 26 | ] 27 | }, 28 | { 29 | program: [ 30 | 'array.map(el => ({', 31 | 'inc: el.val + 1,', 32 | 'dec: el.val - 1', 33 | '}))', 34 | '' 35 | ].join('\n'), 36 | 37 | expected: [ 38 | 'array.map(el => ({', 39 | ' inc: el.val + 1,', 40 | ' dec: el.val - 1', 41 | '}))', 42 | '' 43 | ].join('\n'), 44 | 45 | msg: 'arrow functions correctly indent', 46 | issues: [ 47 | 'https://github.com/maxogden/standard-format/issues/127' 48 | ] 49 | } 50 | ] 51 | 52 | test('ES6 Classes', function (t) { 53 | t.plan(classes.length) 54 | classes.forEach(function (obj) { 55 | t.equal(fmt(obj.program), obj.expected, obj.msg) 56 | console.log('issues:\n' + obj.issues.join('\n')) 57 | }) 58 | }) 59 | -------------------------------------------------------------------------------- /test/failing/jsx.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var fmt = require('../../').transform 3 | 4 | var noops = [] 5 | 6 | test.skip('JSX noops', function (t) { 7 | t.plan(noops.length) 8 | noops.forEach(function (obj) { 9 | var fmtd = fmt(obj.program) 10 | t.equal(fmtd, obj.program, obj.msg) 11 | console.log('issues:\n' + obj.issues.join('\n')) 12 | }) 13 | }) 14 | -------------------------------------------------------------------------------- /test/failing/multiline.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var fmt = require('../../').transform 3 | 4 | var noops = [ 5 | { 6 | program: [ 7 | 'var cool =', 8 | ' a +', 9 | ' b +', 10 | ' c', 11 | '' 12 | ].join('\n'), 13 | msg: 'Allow indendation following a newline after assignment operator', 14 | issues: ['https://github.com/maxogden/standard-format/issues/101'] 15 | } 16 | ] 17 | 18 | test('multiline noop', function (t) { 19 | t.plan(noops.length) 20 | noops.forEach(function (obj) { 21 | var fmtd = fmt(obj.program) 22 | t.equal(fmtd, obj.program, obj.msg) 23 | console.log('issues:\n' + obj.issues.join('\n')) 24 | }) 25 | }) 26 | 27 | var transforms = [ 28 | { 29 | program: [ 30 | 'function x()', 31 | '{', 32 | ' var i=0;', 33 | ' do {', 34 | ' i++', 35 | ' } while(i<10)', 36 | ' console.log(i);', 37 | '}' 38 | ].join('\n'), 39 | expected: [ 40 | 'function x () {', 41 | ' var i = 0', 42 | ' do {', 43 | ' i++', 44 | ' } while (i < 10)', 45 | ' console.log(i)', 46 | '}', 47 | '' 48 | ].join('\n'), 49 | msg: 'Indendation following a do-while loop', 50 | issues: [ 51 | 'https://github.com/maxogden/standard-format/pull/87', 52 | 'https://github.com/maxogden/standard-format/issues/86' 53 | ] 54 | } 55 | ] 56 | 57 | test('Failing Multiline Transforms', function (t) { 58 | t.plan(transforms.length) 59 | transforms.forEach(function (obj) { 60 | t.equal(fmt(obj.program), obj.expected, obj.msg) 61 | console.log('issues:\n' + obj.issues.join('\n')) 62 | }) 63 | }) 64 | -------------------------------------------------------------------------------- /test/failing/obfuscated-files/standard-format-torture.js: -------------------------------------------------------------------------------- 1 | this.gbar_ = this.gbar_ || {};(function(_){var window=this; 2 | try{ 3 | _.jb = function(a){var c=_.La;c.d? a(): c.b.push(a)};_.kb = function(){};_.lb = function(a){_.lb[" "](a);return a};_.lb[" "] = _.kb; 4 | } catch( e) {_._DumpException(e) } 5 | try{ 6 | var gh;gh = function(a){if(a.classList)return a.classList;a = a.className;return _.t(a) && a.match(/\S+/g) || []};_.Q = function(a,c){return a.classList? a.classList.contains(c): _.ra(gh(a),c)};_.R = function(a,c){a.classList? a.classList.add(c): _.Q(a,c) || (a.className += 0 < a.className.length? " " + c: c)}; 7 | _.hh = function(a,c){if(a.classList)(0, _.ma)(c,function(c){_.R(a,c)}); else{var d={};(0, _.ma)(gh(a),function(a){d[a] = !0});(0, _.ma)(c,function(a){d[a] = !0});a.className = ""; for (var e in d)a.className += 0 < a.className.length? " " + e: e}};_.S = function(a,c){a.classList? a.classList.remove(c): _.Q(a,c) && (a.className = (0, _.na)(gh(a),function(a){return a != c}).join(" "))};_.ih = function(a,c){a.classList? (0, _.ma)(c,function(c){_.S(a,c)}): a.className = (0, _.na)(gh(a),function(a){return !_.ra(c,a)}).join(" ")}; 8 | 9 | } catch( e) {_._DumpException(e) } 10 | try{ 11 | var ak,ik,lk,kk;_.Vj = function(a){_.A(this,a,0,null)};_.w(_.Vj,_.z);_.Wj = function(){return _.D(_.J(),_.Vj,11)};_.Xj = function(a){_.A(this,a,0,null)};_.w(_.Xj,_.z);_.Zj = function(){var a=_.Yj();return _.B(a,9)};ak = function(a){_.A(this,a,0,null)};_.w(ak,_.z);_.bk = function(a){return null != _.B(a,2)? _.B(a,2): .001};_.ck = function(a){_.A(this,a,0,null)};_.w(_.ck,_.z);var dk=function(a){return null != _.B(a,3)? _.B(a,3): 1},ek=function(a){return null != _.B(a,2)? _.B(a,2): 1E-4},fk=function(a){_.A(this,a,0,null)}; 12 | _.w(fk,_.z);_.gk = function(a){return _.B(a,10)};_.hk = function(a){return _.B(a,5)};_.Yj = function(){return _.D(_.J(),ak,4) || new ak};_.jk = function(a){var c="//www.google.com/gen_204?",c=c + a.d(2040 - c.length);ik(c)};ik = function(a){var c=new window.Image,d=kk;c.onerror = c.onload = c.onabort = function(){d in lk && delete lk[d]};lk[kk++] = c;c.src = a};lk = [];kk = 0; 13 | _.mk = function(){this.data = {}};_.mk.prototype.b = function(){window.console && window.console.log && window.console.log("Log data: ",this.data)};_.mk.prototype.d = function(a){var c=[],d; for (d in this.data)c.push((0, window.encodeURIComponent)(d) + "=" + (0, window.encodeURIComponent)(String(this.data[d])));return ("atyp=i&zx=" + (new Date).getTime() + "&" + c.join("&")).substr(0,a)}; 14 | var nk=function(a){this.b = a};nk.prototype.log = function(a,c){try{if(this.A(a)){var d=this.k(a,c);this.d(d)}} catch( e) {}};nk.prototype.d = function(a){this.b? a.b(): _.jk(a)};var ok=function(a,c){this.data = {};var d=_.D(a,_.wa,8) || new _.wa;this.data.ei = _.G(_.gk(a));this.data.ogf = _.G(_.B(d,3));var e;e = window.google && window.google.sn? /.*hp$/.test(window.google.sn)? !1: !0: _.F(_.B(a,7));this.data.ogrp = e? "1": "";this.data.ogv = _.G(_.B(d,6)) + "." + _.G(_.B(d,7));this.data.ogd = _.G(_.B(a,21));this.data.ogc = _.G(_.B(a,20));this.data.ogl = _.G(_.hk(a));c && (this.data.oggv = c)};_.w(ok,_.mk); 15 | _.pk = function(a,c,d,e,f){ok.call(this,a,c);_.ua(this.data,{jexpid:_.G(_.B(a,9)),srcpg:"prop=" + _.G(_.B(a,6)),jsr:Math.round(1 / e),emsg:d.name + ":" + d.message});if(f){f._sn && (f._sn = "og." + f._sn); for (var g in f)this.data[(0, window.encodeURIComponent)(g)] = f[g]}};_.w(_.pk,ok); 16 | var qk=[1, 2, 3, 4, 5, 6, 9, 10, 11, 13, 14, 28, 29, 30, 34, 35, 37, 38, 39, 40, 41, 42, 43, 48, 49, 50, 51, 52, 53, 500],tk=function(a,c,d,e,f,g){ok.call(this,a,c);_.ua(this.data,{oge:e,ogex:_.G(_.B(a,9)),ogp:_.G(_.B(a,6)),ogsr:Math.round(1 / (rk(e)? _.H(dk(d)): _.H(ek(d)))),ogus:f});if(g){"ogw" in g && (this.data.ogw = g.ogw, delete g.ogw);"ved" in g && (this.data.ved = g.ved, delete g.ved);a = []; for (var h in g)0 != a.length && a.push(","), a.push(sk(h)), a.push("."), a.push(sk(g[h]));g = a.join("");"" != g && (this.data.ogad = g)}};_.w(tk,ok); var sk=function(a){return (a + "").replace(".","%2E").replace(",","%2C")},uk=null,rk=function(a){if(!uk){uk = {}; for (var c=0;c < qk.length;c++)uk[qk[c]] = !0}return !!uk[a]}; 17 | var vk=function(a,c,d,e,f){this.b = f;this.F = a;this.O = c;this.ea = e;this.B = _.H(ek(a),1E-4);this.w = _.H(dk(a),1);c = Math.random();this.C = _.F(_.B(a,1)) && c < this.B;this.o = _.F(_.B(a,1)) && c < this.w;a = 0;_.F(_.B(d,1)) && (a |= 1);_.F(_.B(d,2)) && (a |= 2);_.F(_.B(d,3)) && (a |= 4);this.J = a};_.w(vk,nk);vk.prototype.A = function(a){return this.b || (rk(a)? this.o: this.C)};vk.prototype.k = function(a,c){return new tk(this.O,this.ea,this.F,a,this.J,c)}; 18 | var wk=function(a,c,d,e){this.b = e;this.F = c;this.ea = d;this.w = _.H(_.bk(a),.001);this.O = _.F(_.B(a,1)) && Math.random() < this.w;c = null != _.B(a,3)? _.B(a,3): 1;this.C = _.H(c,1);this.o = 0;a = null != _.B(a,4)? _.B(a,4): !0;this.B = _.F(a,!0)};_.w(wk,nk);wk.prototype.log = function(a,c){wk.G.log.call(this,a,c);if(this.b && this.B)throw a;};wk.prototype.A = function(){return this.b || this.O && this.o < this.C};wk.prototype.k = function(a,c){try{return _.za(_.ya.N(),"lm").uf(a,c)} catch( d) {return new _.pk(this.F,this.ea,a,this.w,c) }}; wk.prototype.d = function(a){wk.G.d.call(this,a);this.o++}; 19 | var xk;xk = null;_.yk = function(){if(!xk){var a=_.D(_.J(),_.ck,13) || new _.ck,c=_.Na(),d=_.Zj();xk = new wk(a,c,d,_.Ja)}return xk};_.I = function(a,c){_.yk().log(a,c)};_.zk = function(a,c){return function(){try{return a.apply(c,arguments)} catch( d) {_.I(d) }}};var Ak;Ak = null;_.Bk = function(){if(!Ak){var a=_.D(_.J(),fk,12) || new fk,c=_.Na(),d=_.Wj() || new _.Vj,e=_.Zj();Ak = new vk(a,c,d,e,_.Ja)}return Ak};_.U = function(a,c){_.Bk().log(a,c)};_.U(8,{m:"BackCompat" == window.document.compatMode? "q": "s"}); 20 | } catch( e) {_._DumpException(e) } 21 | try{ 22 | var Ck,Gk,Ik;Ck = [3, 5];_.Dk = function(a){_.A(this,a,0,Ck)};_.w(_.Dk,_.z);var Ek=function(a){_.A(this,a,0,null)};_.w(Ek,_.z);_.Fk = function(a){_.A(this,a,0,null)};_.w(_.Fk,_.z);_.Fk.prototype.Qa = function(){return _.B(this,6)}; 23 | _.Hk = function(a,c,d,e,f,g){_.y.call(this);this.F = c;this.J = f;this.o = g;this.S = !1;this.k = {"":!0};this.H = {"":!0};this.A = [];this.w = [];this.R = ["//" + _.G(_.B(a,2)), "og/_/js", "k=" + _.G(_.B(a,3)), "rt=j"];this.O = "" == _.G(_.B(a,14))? null: _.B(a,14);this.K = ["//" + _.G(_.B(a,2)), "og/_/ss", "k=" + _.G(_.B(a,13))];this.B = "" == _.G(_.B(a,15))? null: _.B(a,15);this.U = _.F(_.B(a,1))? "?host=www.gstatic.com&bust=" + _.G(_.B(a,16)): "";this.T = _.F(_.B(a,1))? "?host=www.gstatic.com&bust=" + 1E11 * Math.random(): "";this.d = d;_.B(a,19);a = null != 24 | _.B(a,17)? _.B(a,17): 1;this.b = _.H(a,1);a = 0; for (c = e[a];a < e.length;a++, c = e[a])Gk(this,c,!0)};_.w(_.Hk,_.y);_.Aa(_.Hk,"m");Gk = function(a,c,d){if(!a.k[c] && (a.k[c] = !0, d && a.d[c])) for (var e=0;e < a.d[c].length;e++)Gk(a,a.d[c][e],d)};Ik = function(a,c){ for (var d=[],e=0;e < c.length;e++) {var f=c[e];if(!a.k[f]){var g=a.d[f];g && (g = Ik(a,g), d = d.concat(g));d.push(f);a.k[f] = !0}}return d}; 25 | _.Kk = function(a,c,d){c = Ik(a,c);0 < c.length && (c = a.R.join("/") + "/" + ("m=" + c.join(",")), a.O && (c += "/rs=" + a.O), c = c + a.U, Jk(a,c,(0, _.u)(a.P,a,d)), a.A.push(c))};_.Hk.prototype.P = function(a){_.E("api").Ta(); for (var c=0;c < this.w.length;c++)this.w[c].call(null);a && a.call(null)}; 26 | var Jk=function(a,c,d,e){var f=window.document.createElement("SCRIPT");f.async = !0;f.type = "text/javascript";f.charset = "UTF-8";f.src = c;var g=!0,h=e || 1;e = (0, _.u)(function(){g = !1;this.o.log(47,{att:h,max:a.b,url:c});h < a.b? Jk(a,c,d,h + 1): this.J.log(Error("V`" + h + "`" + a.b),{url:c})},a);var l=(0, _.u)(function(){g && (this.o.log(46,{att:h,max:a.b,url:c}), g = !1, d && d.call(null))},a),q=function(a){"loaded" == a.readyState || "complete" == a.readyState? l(): g && window.setTimeout(function(){q(a)},100)};"undefined" !== typeof f.addEventListener? 27 | f.onload = function(){l()}: f.onreadystatechange = function(){f.onreadystatechange = null;q(f)};f.onerror = e;a.o.log(45,{att:h,max:a.b,url:c});window.document.getElementsByTagName("HEAD")[0].appendChild(f)};_.Hk.prototype.Jd = function(a,c){ for (var d=[],e=0,f=a[e];e < a.length;e++, f = a[e])this.H[f] || (d.push(f), this.H[f] = !0);0 < d.length && (d = this.K.join("/") + "/" + ("m=" + d.join(",")), this.B && (d += "/rs=" + this.B), d += this.T, Lk(d,c))}; 28 | var Lk=function(a,c){var d=window.document.createElement("LINK");d.setAttribute("rel","stylesheet");d.setAttribute("type","text/css");d.setAttribute("href",a);d.onload = d.onreadystatechange = function(){d.readyState && "loaded" != d.readyState && "complete" != d.readyState || c && c.call(null)};window.document.getElementsByTagName("HEAD")[0].appendChild(d)}; 29 | _.Hk.prototype.C = function(a){this.S || (void 0 != a? window.setTimeout((0, _.u)(this.C,this,void 0),a): (_.Kk(this,_.B(this.F,1),(0, _.u)(this.Q,this)), this.Jd(_.B(this.F,2)), this.S = !0))};_.Hk.prototype.Q = function(){_.v("gbar.qm",(0, _.u)(function(a){try{a()} catch( c) {this.J.log(c) }},this))}; 30 | var Mk=function(a,c){var d={};d._sn = ["v.gas", c].join(".");_.I(a,d)};var Nk=["gbq1", "gbq2", "gbqfbwa"],Ok=function(a){var c=window.document.getElementById("gbqld");c && (c.style.display = a? "none": "block", c = window.document.getElementById("gbql")) && (c.style.display = a? "block": "none")};var Pk=function(){};var Qk=function(a,c,d){this.d = a;this.k = c;this.b = d || _.m};var Rk=function(){this.b = []};Rk.prototype.A = function(a,c,d){this.F(a,c,d);this.b.push(new Qk(a,c,d))};Rk.prototype.F = function(a,c,d){d = d || _.m; for (var e=0,f=this.b.length;e < f;e++) {var g=this.b[e];if(g.d == a && g.k == c && g.b == d){this.b.splice(e,1);break}}};Rk.prototype.w = function(a){ for (var c=0,d=this.b.length;c < d;c++) {var e=this.b[c];"hrc" == e.d && e.k.call(e.b,a)}}; 31 | var Sk,Uk,Vk,Wk,Xk;Sk = null;_.Tk = function(){if(null != Sk)return Sk;var a=window.document.body.style;if(!(a = "flexGrow" in a || "webkitFlexGrow" in a))a:{if(a = window.navigator.userAgent){var c=/Trident\/(\d+)/.exec(a);if(c && 7 <= Number(c[1])){a = /\bMSIE (\d+)/.exec(a);a = !a || "10" == a[1];break a}}a = !1}return Sk = a}; 32 | Uk = function(a,c,d){var e=window.NaN;window.getComputedStyle && (a = window.getComputedStyle(a,null).getPropertyValue(c)) && "px" == a.substr(a.length - 2) && (e = d? (0, window.parseFloat)(a.substr(0,a.length - 2)): (0, window.parseInt)(a.substr(0,a.length - 2),10));return e}; 33 | Vk = function(a){var c=a.offsetWidth,d=Uk(a,"width");if(!(0, window.isNaN)(d))return c - d;var e=a.style.padding,f=a.style.paddingLeft,g=a.style.paddingRight;a.style.padding = a.style.paddingLeft = a.style.paddingRight = 0;d = a.clientWidth;a.style.padding = e;a.style.paddingLeft = f;a.style.paddingRight = g;return c - d}; 34 | Wk = function(a){var c=Uk(a,"min-width");if(!(0, window.isNaN)(c))return c;var d=a.style.width,e=a.style.padding,f=a.style.paddingLeft,g=a.style.paddingRight;a.style.width = a.style.padding = a.style.paddingLeft = a.style.paddingRight = 0;c = a.clientWidth;a.style.width = d;a.style.padding = e;a.style.paddingLeft = f;a.style.paddingRight = g;return c};Xk = function(a,c){c || -.5 != a - Math.round(a) || (a -= .5);return Math.round(a)}; _.Yk = function(a){if(a){var c=a.style.opacity;a.style.opacity = ".99";_.lb(a.offsetWidth);a.style.opacity = c}}; 35 | var Zk=function(a){_.y.call(this);this.b = a;this.d = [];this.k = []};_.w(Zk,_.y);Zk.prototype.L = function(){Zk.G.L.call(this);this.b = null; for (var a=0;a < this.d.length;a++)this.d[a].Y(); for (a = 0;a < this.k.length;a++)this.k[a].Y();this.d = this.k = null}; 36 | Zk.prototype.La = function(a){void 0 == a && (a = this.b.offsetWidth); for (var c=Vk(this.b),d=[],e=0,f=0,g=0,h=0,l=0;l < this.d.length;l++) {var q=this.d[l],r=$k(q),x=Vk(q.b);d.push({item:q,gb:r,sh:x,tc:0});e += r.Gc;f += r.Vc;g += r.Sb;h += x}a = a - h - c - g;e = 0 < a? e: f;f = a;c = d;do {g = !0;h = []; for (l = q = 0;l < c.length;l++) {var r=c[l],x=0 < f? r.gb.Gc: r.gb.Vc,C=0 == e? 0: x / e * f + r.tc,C=Xk(C,g),g=!g;r.tc = al(r.item,C,r.sh,r.gb.Sb);0 < x && C == r.tc && (h.push(r), q += x)}c = h;f = a - (0, _.pa)(d,function(a,c){return a + c.tc},0);e = q } while (0 != f && 0 != c.length); 37 | for (l = 0;l < this.k.length;l++)this.k[l].La()};var cl=function(a){var c={};c.items = (0, _.oa)(a.d,function(a){return bl(a)});c.children = (0, _.oa)(a.k,function(a){return cl(a)});return c},dl=function(a,c){ for (var d=0;d < a.d.length;d++)a.d[d].b.style.width = c.items[d]; for (d = 0;d < a.k.length;d++)dl(a.k[d],c.children[d])};Zk.prototype.M = function(){return this.b}; 38 | var el=function(a,c,d,e){Zk.call(this,a);this.w = c;this.A = d;this.o = e};_.w(el,Zk); 39 | var $k=function(a,c){var d=a.w,e=a.A,f;if(-1 == a.o){var g=c;void 0 == g && (g = Vk(a.b));f = bl(a);var h=cl(a),l=Uk(a.b,"width",!0);(0, window.isNaN)(l) && (l = a.b.offsetWidth - g);g = Math.ceil(l);a.b.style.width = f;dl(a,h);f = g}else f = a.o;return {Gc:d,Vc:e,Sb:f}},al=function(a,c,d,e){void 0 == d && (d = Vk(a.b));void 0 == e && (e = $k(a,d).Sb);c = e + c;0 > c && (c = 0);a.b.style.width = c + "px";d = a.b.offsetWidth - d;a.b.style.width = d + "px";return d - e},bl=function(a){var c=a.b.style.width;a.b.style.width = "";return c}; 40 | var fl=function(a,c,d){var e;void 0 == e && (e = -1);return {className:a,gb:{Gc:c || 0,Vc:d || 0,Sb:e}}},gl={className:"gb_3b",items:[fl("gb_Ua"), fl("gb_ic"), fl("gb_Mb",0,2), fl("gb_jc"), fl("gb_ea",1,1)],eb:[{className:"gb_ea",items:[fl("gb_Lc",0,1), fl("gb_Kc",0,1)],eb:[function(a){a = a.gb_Lc;var c;if(a)c = a.M(); else{c = window.document.querySelector(".gb_Lc");if(!c)return null;a = new Zk(c)}c = c.querySelectorAll(".gb_m"); for (var d=0;d < c.length;d++) {var e;if(_.Q(c[d],"gb_o")){e = new el(c[d],0,1,-1);var f=c[d].querySelector(".gb_l"); 41 | f && (f = new el(f,0,1,-1), e.d.push(f), a.k.push(e))}else e = new el(c[d],0,0,-1);a.d.push(e)}return a}, {className:"gb_Kc",items:[fl("gb_J"), fl("gb_4a"), fl("gb_Zb"), fl("gb_ma",0,1), fl("gb_Mc"), fl("gb_ia",0,1), fl("gb_Nc"), fl("gb_lc")],eb:[{className:"gb_ma",items:[fl("gb_oa",0,1)],eb:[{className:"gb_oa",items:[fl("gb_ka",0,1)],eb:[]}]}]}]}, {className:"gb_fc",items:[fl("gbqff",1,1), fl("gb_ec")],eb:[]}]},hl=function(a,c){var d=c;if(!d){d = window.document.querySelector("." + a.className);if(!d)return null;d = new Zk(d)} for (var e= 42 | {},f=0;f < a.items.length;f++) {var g=a.items[f],h;h = g;var l=window.document.querySelector("." + h.className);if(h = l? new el(l,h.gb.Gc,h.gb.Vc,h.gb.Sb): null)d.d.push(h), e[g.className] = h} for (f = 0;f < a.eb.length;f++) {var g=a.eb[f],q;"function" == typeof g? q = g(e): q = hl(g,e[g.className]);q && d.k.push(q)}return d}; 43 | _.kl = function(a){_.y.call(this);this.B = new Rk;this.d = window.document.getElementById("gb");this.O = (this.b = window.document.querySelector(".gb_ea"))? this.b.querySelector(".gb_Kc"): null;this.C = [];this.he = 60;this.J = _.B(a,4);this.Bh = _.H(_.B(a,2),152);this.If = _.H(_.B(a,1),30);this.k = null;this.Ke = _.F(_.B(a,3),!0);this.o = 1;this.d && this.J && (this.d.style.minWidth = this.J + "px");_.il(this);this.Ke && (this.d && (jl(this), _.R(this.d,"gb_p"), this.b && _.R(this.b,"gb_p"), _.Tk() || (this.k = hl(gl))), this.La(), window.setTimeout((0, _.u)(this.La, 44 | this),0));_.v("gbar.elc",(0, _.u)(this.K,this));_.v("gbar.ela",_.kb);_.v("gbar.elh",(0, _.u)(this.S,this))};_.w(_.kl,_.y);_.Aa(_.kl,"el");var ll=function(){var a=_.kl.Lh();return {es:a? {f:a.Bh,h:a.he,m:a.If}: {f:152,h:60,m:30},mo:"md",vh:window.innerHeight || 0,vw:window.innerWidth || 0}};_.kl.prototype.L = function(){_.kl.G.L.call(this)};_.kl.prototype.La = function(a){a && jl(this);this.k && this.k.La(Math.max(window.document.documentElement.clientWidth,Wk(this.d)));_.Yk(this.b)}; 45 | _.kl.prototype.H = function(){try{var a=window.document.getElementById("gb"),c=a.querySelector(".gb_ea");_.S(a,"gb_3c");c && _.S(c,"gb_3c"); for (var a=0,d;d = Nk[a];a++) {var e=window.document.getElementById(d);e && _.S(e,"gbqfh")}Ok(!1)} catch( f) {Mk(f,"rhcc") }this.La(!0)};_.kl.prototype.T = function(){try{var a=window.document.getElementById("gb"),c=a.querySelector(".gb_ea");_.R(a,"gb_3c");c && _.R(c,"gb_3c"); for (var a=0,d;d = Nk[a];a++)_.R(window.document.getElementById(d),"gbqfh");Ok(!0)} catch( e) {Mk(e,"ahcc") }this.La(!0)}; 46 | _.il = function(a){if(a.d){var c=a.d.offsetWidth;0 == a.o? 900 <= c && (a.o = 1, a.w(new Pk)): 900 > c && (a.o = 0, a.w(new Pk))}};_.kl.prototype.K = function(a){this.C.push(a)};_.kl.prototype.S = function(a){var c=ll().es.h;this.he = c + a; for (a = 0;a < this.C.length;a++)try{this.C[a](ll())} catch( d) {_.I(d) }};var jl=function(a){if(a.b){var c;a.k && (c = cl(a.k));_.R(a.b,"gb_s");a.b.style.minWidth = a.b.offsetWidth - Vk(a.b) + "px";a.O.style.minWidth = a.O.offsetWidth - Vk(a.O) + "px";_.S(a.b,"gb_s");c && dl(a.k,c)}}; _.kl.prototype.A = function(a,c,d){this.B.A(a,c,d)};_.kl.prototype.F = function(a,c){this.B.F(a,c)};_.kl.prototype.w = function(a){this.B.w(a)}; 47 | _.jb(function(){var a=_.D(_.J(),Ek,21) || new Ek,a=new _.kl(a);_.Ca("el",a);_.v("gbar.gpca",(0, _.u)(a.T,a));_.v("gbar.gpcr",(0, _.u)(a.H,a))});_.v("gbar.elr",ll);_.ml = function(a){this.k = _.kl.N();this.d = a};_.ml.prototype.b = function(a,c){0 == this.k.o? (_.R(a,"gb_r"), c? (_.S(a,"gb_la"), _.R(a,"gb_Oc")): (_.S(a,"gb_Oc"), _.R(a,"gb_la"))): _.ih(a,["gb_r", "gb_la", "gb_Oc"])};_.v("gbar.sos",function(){return window.document.querySelectorAll(".gb_hc")});_.v("gbar.si",function(){return window.document.querySelector(".gb_gc")}); 48 | _.jb(function(){if(_.D(_.J(),_.Dk,16)){var a=window.document.querySelector(".gb_ea"),c=_.D(_.J(),_.Dk,16) || new _.Dk,c=_.F(_.B(c,1),!1),c=new _.ml(c);a && c.d && c.b(a,!1)}}); 49 | } catch( e) {_._DumpException(e) } 50 | try{ 51 | var nl=function(){_.La.k(_.I)};var ol=function(a,c){var d=_.zk(nl);a.addEventListener? a.addEventListener(c,d): a.attachEvent("on" + c,d)};var pl=[1, 2],ql=function(a,c){a.w.push(c)},rl=function(a){_.A(this,a,0,pl)};_.w(rl,_.z);var sl=function(){_.y.call(this);this.o = this.b = null;this.d = {};this.w = {};this.k = {}};_.w(sl,_.y);_.k = sl.prototype;_.k.Ze = function(a){a && this.b && a != this.b && this.b.close();this.b = a};_.k.Me = function(a){a = this.k[a] || a;return this.b == a};_.k.Fh = function(a){this.o = a}; 52 | _.k.Le = function(a){return this.o == a};_.k.hd = function(){this.b && this.b.close();this.b = null};_.k.tf = function(a){this.b && this.b.getId() == a && this.hd()};_.k.Pb = function(a,c,d){this.d[a] = this.d[a] || {};this.d[a][c] = this.d[a][c] || [];this.d[a][c].push(d)};_.k.fd = function(a,c){var d=c.getId();if(this.d[a] && this.d[a][d]) for (var e=0;e < this.d[a][d].length;e++)try{this.d[a][d][e]()} catch( f) {_.I(f) }};_.k.Hh = function(a,c){this.w[a] = c};_.k.rf = function(a){return !this.w[a.getId()]}; 53 | _.k.Qg = function(){return !!this.b && this.b.V};_.k.qf = function(){return !!this.b};_.k.Re = function(){this.b && this.b.la()};_.k.cf = function(a){this.k[a] && (this.b && this.b.getId() == a || this.k[a].open())};_.k.jh = function(a){this.k[a.getId()] = a};var tl;window.gbar && window.gbar._DPG? tl = window.gbar._DPG[0] || {}: tl = {};var ul;window.gbar && window.gbar._LDD? ul = window.gbar._LDD: ul = [];var vl=_.Na(),wl=new _.Hk(vl,_.D(_.J(),rl,17) || new rl,tl,ul,_.yk(),_.Bk());_.Ca("m",wl); if(_.F(_.B(vl,18),!0))wl.C(); else{var xl=_.H(_.B(vl,19),200),yl=(0, _.u)(wl.C,wl,xl);_.jb(yl)}ol(window.document,"DOMContentLoaded");ol(window,"load"); 54 | _.v("gbar.ldb",(0, _.u)(_.La.k,_.La));_.v("gbar.mls",function(){});var zl=function(){_.y.call(this);this.k = this.b = null;this.w = 0;this.o = {};this.d = !1;var a=window.navigator.userAgent;0 <= a.indexOf("MSIE") && 0 <= a.indexOf("Trident") && (a = /\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(a)) && a[1] && 9 > (0, window.parseFloat)(a[1]) && (this.d = !0)};_.w(zl,_.y); 55 | var Al=function(a,c,d){if(!a.d)if(d instanceof Array) for (var e in d)Al(a,c,d[e]); else{e = (0, _.u)(a.A,a,c);var f=a.w + d;a.w++;c.setAttribute("data-eqid",f);a.o[f] = e;c && c.addEventListener? c.addEventListener(d,e,!1): c && c.attachEvent? c.attachEvent("on" + d,e): _.I(Error("W`" + c))}}; 56 | zl.prototype.Ne = function(a,c){if(this.d)return null;if(c instanceof Array){var d=null,e; for (e in c) {var f=this.Ne(a,c[e]);f && (d = f) }return d}d = null;this.b && this.b.type == c && this.k == a && (d = this.b, this.b = null);if(e = a.getAttribute("data-eqid"))a.removeAttribute("data-eqid"), (e = this.o[e])? a.removeEventListener? a.removeEventListener(c,e,!1): a.detachEvent && a.detachEvent("on" + c,e): _.I(Error("X`" + a));return d};zl.prototype.A = function(a,c){this.b = c;this.k = a;c.preventDefault? c.preventDefault(): c.returnValue = !1}; 57 | _.Ca("eq",new zl);var Bl=function(){_.y.call(this);this.ge = [];this.jd = []};_.w(Bl,_.y);Bl.prototype.b = function(a,c){this.ge.push({uc:a,options:c})};Bl.prototype.init = function(){window.gapi = {};var a=_.Yj(),c=window.___jsl = {};c.h = _.G(_.B(a,1));c.ms = _.G(_.B(a,2));c.m = _.G(_.B(a,3));c.l = [];a = _.D(_.J(),_.Xj,5) || new _.Xj;_.B(a,1) && (a = _.B(a,3)) && this.jd.push(a);a = _.D(_.J(),_.Fk,6) || new _.Fk;_.B(a,1) && (a = _.B(a,2)) && this.jd.push(a);_.v("gapi.load",(0, _.u)(this.b,this));return this}; 58 | var Cl=window,Dl,El=_.Yj();Dl = _.B(El,7);Cl.__PVT = _.G(Dl);_.Ca("gs",(new Bl).init());(function(){ for (var a=function(a){return function(){_.U(44,{n:a})}},c=0;c < _.Qa.length;c++) {var d="gbar." + _.Qa[c];_.v(d,a(d))}var e=_.ya.N();_.za(e,"api").Ta();ql(_.za(e,"m"),function(){_.za(e,"api").Ta()})})();var Fl=function(a){_.jb(function(){var c=window.document.querySelector("." + a);c && (c = c.querySelector(".gb_K")) && Al(_.E("eq"),c,"click")})};var Gl=window.document.querySelector(".gb_J"),Hl=/(\s+|^)gb_dc(\s+|$)/;Gl && !Hl.test(Gl.className) && Fl("gb_J");var Il=new sl;_.Ca("dd",Il);_.v("gbar.close",(0, _.u)(Il.hd,Il));_.v("gbar.cls",(0, _.u)(Il.tf,Il));_.v("gbar.abh",(0, _.u)(Il.Pb,Il,0));_.v("gbar.adh",(0, _.u)(Il.Pb,Il,1));_.v("gbar.ach",(0, _.u)(Il.Pb,Il,2));_.v("gbar.aeh",(0, _.u)(Il.Hh,Il));_.v("gbar.bsy",(0, _.u)(Il.Qg,Il));_.v("gbar.op",(0, _.u)(Il.qf,Il)); 59 | Fl("gb_ma");_.jb(function(){var a=window.document.querySelector(".gb_Xa");a && Al(_.E("eq"),a,"click")});Fl("gb_4a");_.v("gbar.qfgw",(0, _.u)(window.document.getElementById,window.document,"gbqfqw"));_.v("gbar.qfgq",(0, _.u)(window.document.getElementById,window.document,"gbqfq"));_.v("gbar.qfgf",(0, _.u)(window.document.getElementById,window.document,"gbqf"));_.v("gbar.qfsb",(0, _.u)(window.document.getElementById,window.document,"gbqfb")); 60 | Fl("gb_Zb");Fl("gb_lc"); 61 | } catch( e) {_._DumpException(e) } 62 | })(this.gbar_); 63 | // Google Inc. 64 | -------------------------------------------------------------------------------- /test/failing/obfuscated.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var path = require('path') 3 | var testFile = require('../testFile') 4 | 5 | var files = [ 6 | {path: path.resolve(path.join(__dirname, '/obfuscated-files/standard-format-torture.js')), 7 | issues: ['https://github.com/maxogden/standard-format/issues/27']} 8 | ] 9 | 10 | files.forEach(function (fileObj) { 11 | var basename = path.basename(fileObj.path) 12 | test('deobfuscate and lint ' + basename, testFile(fileObj.path, 1)) 13 | }) 14 | -------------------------------------------------------------------------------- /test/failing/singleline.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var fmt = require('../../').transform 3 | 4 | var transforms = [ 5 | { 6 | str: 'var x = {key:123,more:456}\n', 7 | expect: 'var x = {key: 123, more: 456}\n', 8 | msg: 'Space after comma in keys', 9 | issues: ['https://github.com/maxogden/standard-format/issues/54'] 10 | } 11 | ] 12 | 13 | test('singleline transforms', function (t) { 14 | t.plan(transforms.length) 15 | transforms.forEach(function (obj) { 16 | t.equal(fmt(obj.str), obj.expect, obj.msg) 17 | console.log('issues:\n' + obj.issues.join('\n')) 18 | }) 19 | }) 20 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const join = require('path').join 3 | const testFile = require('./testFile') 4 | const TARGET_FILE = join(__dirname, './test-files/test.js') 5 | 6 | test('test.js formatted and linted without error', testFile(TARGET_FILE)) 7 | -------------------------------------------------------------------------------- /test/jsx.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const fmt = require('../').transform 3 | 4 | const noops = [ 5 | { 6 | program: [ 7 | 'export default class Foo extends Component {', 8 | ' renderPartial () {', 9 | ' return this.props.bar.map((item) => {', 10 | ' return ', 11 | ' })', 12 | ' }', 13 | '}', 14 | '' 15 | ].join('\n'), 16 | 17 | msg: 'Keep indentation for multiple return statements with JSX' 18 | }, 19 | { 20 | program: [ 21 | 'export class Foo extends React.Component {', 22 | ' render () {', 23 | ' return (', 24 | '
', 25 | ' )', 26 | ' }', 27 | '}', 28 | '' 29 | ].join('\n'), 30 | msg: 'Preserve indendation on JSX blocks', 31 | issues: ['https://github.com/maxogden/standard-format/issues/99'] 32 | }, 33 | { 34 | program: [ 35 | 'export class Foo extends React.Component {', 36 | ' render () {', 37 | ' return (', 38 | '
', 39 | ' ', 40 | '
', 41 | ' )', 42 | ' }', 43 | '}', 44 | '' 45 | ].join('\n'), 46 | msg: 'Preserve indendation on JSX blocks with parameters', 47 | issues: ['https://github.com/maxogden/standard-format/issues/99'] 48 | }, 49 | { 50 | program: [ 51 | 'export class Foo extends React.Component {', 52 | ' render () {', 53 | ' return (', 54 | '
', 55 | ' ', 60 | '
', 61 | ' )', 62 | ' }', 63 | '}', 64 | '' 65 | ].join('\n'), 66 | msg: '4+ jsx args are multilined and aligned', 67 | issues: ['https://github.com/maxogden/standard-format/issues/99'] 68 | } 69 | ] 70 | 71 | test('jsx noop', function (t) { 72 | t.plan(noops.length) 73 | noops.forEach(function (obj) { 74 | t.equal(fmt(obj.program), obj.program, obj.msg) 75 | }) 76 | }) 77 | -------------------------------------------------------------------------------- /test/multiline.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const fmt = require('../').transform 3 | 4 | const cr = /\n/g 5 | const crlf = '\r\n' 6 | 7 | const collapse = [ 8 | { 9 | program: [ 10 | 'var x = 1', 11 | '', 12 | '', 13 | 'var z = 2', 14 | '' 15 | ].join('\n'), 16 | 17 | expected: [ 18 | 'var x = 1', 19 | '', 20 | 'var z = 2', 21 | '' 22 | ].join('\n'), 23 | 24 | msg: 'two empty lines should collapse to one' 25 | }, 26 | { 27 | program: [ 28 | 'var x = 1', 29 | '', '', '', '', '', 30 | '', '', '', '', '', 31 | 'var z = 2', '' 32 | ].join('\n'), 33 | 34 | expected: [ 35 | 'var x = 1', 36 | '', 37 | 'var z = 2', '' 38 | ].join('\n'), 39 | 40 | msg: 'ten empty lines should collapse to one' 41 | }, 42 | { 43 | program: [ 44 | 'var foo = function () {', 45 | '', 46 | ' bar()', 47 | '}', 48 | '' 49 | ].join('\n'), 50 | 51 | expected: [ 52 | 'var foo = function () {', 53 | ' bar()', 54 | '}', 55 | '' 56 | ].join('\n'), 57 | 58 | msg: 'Remove padding newlines after curly braces' 59 | }, 60 | { 61 | program: [ 62 | 'var x = 123; /* Useful comment ', 63 | 'that spans two lines */', 64 | '' 65 | ].join('\n'), 66 | 67 | expected: [ 68 | 'var x = 123 /* Useful comment ', 69 | 'that spans two lines */', 70 | '' 71 | ].join('\n'), 72 | 73 | msg: 'Remove semicolon from multiline comment' 74 | } 75 | ] 76 | 77 | test('multiline collapse', function (t) { 78 | t.plan(collapse.length) 79 | collapse.forEach(function (obj) { 80 | t.equal(fmt(obj.program), obj.expected, obj.msg) 81 | }) 82 | }) 83 | 84 | test('multiline collapse CRLF', function (t) { 85 | t.plan(collapse.length) 86 | collapse.forEach(function (obj) { 87 | obj.program = obj.program.replace(cr, crlf) 88 | obj.expected = obj.expected.replace(cr, crlf) 89 | t.equal(fmt(obj.program), obj.expected, obj.msg) 90 | }) 91 | }) 92 | 93 | const noops = [ 94 | { 95 | program: [ 96 | 'var x = 1', 97 | '', 98 | 'var z = 2', 99 | '' 100 | ].join('\n'), 101 | 102 | msg: 'single empty line should be unmodified' 103 | }, 104 | { 105 | program: [ 106 | 'function getRequests (cb) {', 107 | ' nets({', 108 | " url: binUrl + '/api/v1/bins/' + bin.name + '/requests',", 109 | ' json: true,', 110 | ' headers: headers', 111 | ' }, function (err, resp, body) {', 112 | ' cb(err, resp, body)', 113 | ' })', 114 | '}', 115 | '' 116 | ].join('\n'), 117 | 118 | msg: "Don't mess with function tabbing" 119 | 120 | }, 121 | { 122 | program: [ 123 | 'var obj = {', 124 | " 'standard': {", 125 | " 'ignore': ['test.js', '**test/failing/**']", 126 | ' }', 127 | '}', 128 | '' 129 | ].join('\n'), 130 | 131 | msg: 'allow single line object arrays' 132 | }, 133 | { 134 | program: [ 135 | '/*global localStorage*/', 136 | ';(function () { // IIFE to ensure no global leakage!', 137 | '}())', 138 | '' 139 | ].join('\n'), 140 | 141 | msg: 'IIFEs are not messed with' 142 | }, 143 | { 144 | program: [ 145 | "console.log('meh')", 146 | ';(function a () {', 147 | " console.log('hiya')", 148 | '}())', 149 | '' 150 | ].join('\n'), 151 | 152 | msg: 'IIFEs are not messed with' 153 | } 154 | ] 155 | 156 | test('multiline noop', function (t) { 157 | t.plan(noops.length) 158 | noops.forEach(function (obj) { 159 | t.equal(fmt(obj.program), obj.program, obj.msg) 160 | }) 161 | }) 162 | 163 | test('multiline noop CRLF', function (t) { 164 | t.plan(noops.length) 165 | noops.forEach(function (obj) { 166 | obj.program = obj.program.replace(cr, crlf) 167 | t.equal(fmt(obj.program), obj.program, obj.msg) 168 | }) 169 | }) 170 | 171 | const semicolons = [ 172 | { 173 | program: [ 174 | 'var x = 2', 175 | '[1, 2, 3].map(function () {})', 176 | '', 177 | 'var y = 8', 178 | '(function () {', 179 | ' bar()', 180 | '}())', 181 | '' 182 | ].join('\n'), 183 | 184 | expected: [ 185 | 'var x = 2', 186 | ';[1, 2, 3].map(function () {})', 187 | '', 188 | 'var y = 8', 189 | ';(function () {', 190 | ' bar()', 191 | '}())', 192 | '' 193 | ].join('\n'), 194 | 195 | msg: 'Add semicolon before `[` and `(` if they are the first things on the line' 196 | }, 197 | { 198 | program: [ 199 | "console.log('meh');", 200 | '(function a() {', 201 | "console.log('hiya');", 202 | '}());', 203 | '' 204 | ].join('\n'), 205 | 206 | expected: [ 207 | "console.log('meh')", 208 | ';(function a () {', 209 | " console.log('hiya')", 210 | '}())', 211 | '' 212 | ].join('\n'), 213 | 214 | msg: 'IIFEs are not messed with' 215 | } 216 | ] 217 | 218 | test('multiline semicolons', function (t) { 219 | t.plan(semicolons.length) 220 | semicolons.forEach(function (obj) { 221 | t.equal(fmt(obj.program), obj.expected, obj.msg) 222 | }) 223 | }) 224 | -------------------------------------------------------------------------------- /test/shebang.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const fmt = require('../').transform 3 | 4 | test('deal with shebang line', function (t) { 5 | t.plan(2) 6 | 7 | const program = "#!/usr/bin/env node\nconsole.log('badaboom')\n" 8 | let formatted 9 | 10 | let msg = 'Expect formatter to not explode with shebang' 11 | t.ok(formatted = fmt(program), msg) 12 | 13 | msg = 'Expect program to be still have shebang' 14 | t.equal(formatted, program, msg) 15 | }) 16 | -------------------------------------------------------------------------------- /test/singleline.js: -------------------------------------------------------------------------------- 1 | const test = require('tape') 2 | const fmt = require('../').transform 3 | 4 | const noops = [ 5 | { 6 | str: 'if (!opts) opts = {}\n', 7 | msg: 'Noop on single line conditional assignment' 8 | }, 9 | 10 | { 11 | str: 'var g = { name: f, data: fs.readFileSync(f).toString() }\n', 12 | msg: 'Noop on single line object assignment' 13 | }, 14 | { 15 | str: "{foo: 'bar'}\n", 16 | msg: 'Dont add padding to object braces' 17 | }, 18 | { 19 | str: "var x = ['test.js', '**test/failing/**']\n", 20 | msg: 'Noop on singleline arrays' 21 | }, 22 | { 23 | str: 'function x () {}\n', 24 | msg: 'Noop on named functions correctly spaced' 25 | }, 26 | { 27 | str: 'window.wrapFunctionsUntil(1)\n', 28 | msg: 'Noop non-functions with function in the name' 29 | }, 30 | { 31 | str: "import * as lib from 'lib'\n", 32 | msg: 'Noop ES2015 import' 33 | }, 34 | { 35 | str: 'function * blarg (foo) {yield foo}\n', 36 | msg: 'Noop ES2015 generator' 37 | }, 38 | { 39 | str: 'console.log(1 === 2 ? 3 : 4)\n', 40 | msg: 'Noop infix' 41 | }, 42 | { 43 | str: 'test[0]\ntest\n', 44 | msg: 'allow newline after member accessor', 45 | issues: ['https://github.com/maxogden/standard-format/pull/93'] 46 | }, 47 | { 48 | str: 'test(test[0])\n', 49 | msg: "don't force newline on mid-expression member accessor", 50 | issues: ['https://github.com/maxogden/standard-format/pull/93'] 51 | }, 52 | { 53 | str: '// good comment\n', 54 | msg: 'Expect good comments to be unchanged' 55 | } 56 | ] 57 | 58 | test('singleline noop expressions', function (t) { 59 | t.plan(noops.length) 60 | noops.forEach(function (obj) { 61 | t.equal(fmt(obj.str), obj.str, obj.msg) 62 | }) 63 | }) 64 | 65 | const transforms = [ 66 | { 67 | str: 'var x = function() {}\n', 68 | expect: 'var x = function () {}\n', 69 | msg: 'Anonymous function spacing between keyword and arguments' 70 | }, 71 | { 72 | str: 'var x = function (y){}\n', 73 | expect: 'var x = function (y) {}\n', 74 | msg: 'Anonymous function spacing between arguments and opening brace' 75 | }, 76 | { 77 | str: 'function xx() {}\n', 78 | expect: 'function xx () {}\n', 79 | msg: 'Named function spacing between keyword and arguments' 80 | }, 81 | { 82 | str: 'function xx (y){}\n', 83 | expect: 'function xx (y) {}\n', 84 | msg: 'Named function spacing between arguments and opening brace' 85 | }, 86 | { 87 | str: 'var hi = 1\n', 88 | expect: 'var hi = 1\n', 89 | msg: 'Squash spaces around variable value' 90 | }, 91 | { 92 | str: 'var hi = 1\n', 93 | expect: 'var hi = 1\n', 94 | msg: 'Space after variable name' 95 | }, 96 | { 97 | str: 'var hi\n hi = 1\n', 98 | expect: 'var hi\nhi = 1\n', 99 | msg: 'Squash spaces around assignment operator' 100 | }, 101 | { 102 | str: 'function foo (x,y,z) {}\n', 103 | expect: 'function foo (x, y, z) {}\n', 104 | msg: 'Space after commas in function parameters' 105 | }, 106 | { 107 | str: 'var array = [1,2,3]\n', 108 | expect: 'var array = [1, 2, 3]\n', 109 | msg: 'Space after commas in array' 110 | }, 111 | { 112 | str: 'var x = 1;\n', 113 | expect: 'var x = 1\n', 114 | msg: 'Remove semicolons' 115 | }, 116 | { 117 | str: 'var x = {key:123}\n', 118 | expect: 'var x = {key: 123}\n', 119 | msg: 'Space after colon (key-spacing)' 120 | }, 121 | { 122 | str: 'var x = {key : 123}\n', 123 | expect: 'var x = {key: 123}\n', 124 | msg: 'No Space before colon (key-spacing)' 125 | }, 126 | { 127 | str: 'if(true){}\n', 128 | expect: 'if (true) {}\n', 129 | msg: 'Space after if' 130 | }, 131 | { 132 | str: 'if ( true ) {}\n', 133 | expect: 'if (true) {}\n', 134 | msg: 'Remove spaces inside conditional' 135 | }, 136 | { 137 | str: 'var condition = ( x === y )\n', 138 | expect: 'var condition = (x === y)\n', 139 | msg: 'Remove spaces inside expression parentheses' 140 | }, 141 | { 142 | str: 'function doStuff ( x, y ) {}\n', 143 | expect: 'function doStuff (x, y) {}\n', 144 | msg: 'Remove spaces inside parameter list parentheses' 145 | }, 146 | { 147 | str: 'var x = 123; // Useful comment\n', 148 | expect: 'var x = 123 // Useful comment\n', 149 | msg: 'Remove unneeded trailing semicolons that are followed by a comment' 150 | }, 151 | { 152 | str: 'var x = 123; /* Useful comment */\n', 153 | expect: 'var x = 123 /* Useful comment */\n', 154 | msg: 'Remove unneeded trailing semicolons that are followed by a multiline comment' 155 | }, 156 | { 157 | str: 'console.log(1===2?3:4)\n', 158 | expect: 'console.log(1 === 2 ? 3 : 4)\n', 159 | msg: 'infix' 160 | }, 161 | { 162 | str: 'const { message, rollup, line, col, type } = origMessage\n', 163 | expect: 'const { message, rollup, line, col, type } = origMessage\n', 164 | msg: 'No space before comma in keys in destructuring assignment' 165 | }, 166 | { 167 | str: '//bad comment\n', 168 | expect: '// bad comment\n', 169 | msg: 'Expect space or tab after // in comment' 170 | } 171 | ] 172 | 173 | test('singleline transforms', function (t) { 174 | t.plan(transforms.length) 175 | transforms.forEach(function (obj) { 176 | t.equal(fmt(obj.str), obj.expect, obj.msg) 177 | }) 178 | }) 179 | 180 | const cr = /\n/g 181 | const crlf = '\r\n' 182 | 183 | test('singleline transforms CRLF', function (t) { 184 | t.plan(transforms.length) 185 | transforms.forEach(function (obj) { 186 | obj.str = obj.str.replace(cr, crlf) 187 | obj.expect = obj.expect.replace(cr, crlf) 188 | t.equal(fmt(obj.str), obj.expect, obj.msg) 189 | }) 190 | }) 191 | -------------------------------------------------------------------------------- /test/test-files/test.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | // This file is automatically ran through standard-format 4 | // and checked by standard. Add test cases for the formatter by adding 5 | // to this file 6 | var noop = require('noop') 7 | 8 | // eol semicolons 9 | var x = 1; 10 | 11 | // eol whitespace 12 | x = 2 13 | 14 | // standard-format has nothing to say about unused vars 15 | // so this is here to prevent invalid test cases 16 | console.log(x) 17 | 18 | //bad comment -- needs a space after slashes 19 | var test = "what"; 20 | 21 | if(test) { 22 | ["a","b","c"].forEach(function (x) { 23 | // test: infix commas 24 | console.log(x*2); 25 | }) 26 | } 27 | 28 | var obj = {val: 2} 29 | var another = { foo: 'bar' } 30 | 31 | // space after function name and arg paren 32 | ;[1].forEach(function(){}) 33 | 34 | // space after argument paren 35 | function f2 (x,y,z){} 36 | function fooz() {} 37 | function foox () {} 38 | function foos () {} 39 | 40 | var anon = function() {} 41 | 42 | f2( obj) 43 | f2(obj ) 44 | f2( obj ) 45 | f2( obj, obj ) 46 | f2( obj,obj ) 47 | fooz() 48 | foox() 49 | foos() 50 | anon(another) 51 | 52 | function foo(){} 53 | function bar() {} 54 | function quux() {} 55 | 56 | 57 | foo() 58 | bar() 59 | quux() 60 | 61 | 62 | function food (){} 63 | function foot () {} 64 | 65 | 66 | food() 67 | foot() 68 | 69 | 70 | // test: no block padding 71 | var lessThanThreeNewlines = function () { 72 | 73 | return 2; 74 | } 75 | lessThanThreeNewlines() 76 | 77 | // at most one newline after opening brace 78 | function noExtraNewlines() { 79 | 80 | 81 | return 2; 82 | } 83 | noExtraNewlines() 84 | 85 | // at most one newline after opening brace 86 | function noExtraSingle() { return 2 } 87 | noExtraSingle() 88 | 89 | // at most one newline after opening brace 90 | // at most one newline before closing brace 91 | function noExtraBraces() { 92 | 93 | 94 | if (noExtraBraces != null) 95 | 96 | { 97 | 98 | console.log(42) 99 | 100 | } 101 | 102 | else 103 | 104 | { 105 | 106 | console.log(42) 107 | 108 | } 109 | 110 | switch(noExtraBraces) 111 | 112 | { 113 | 114 | case null: 115 | console.log(42) 116 | 117 | } 118 | 119 | try 120 | 121 | { 122 | 123 | console.log(42) 124 | 125 | } 126 | catch (e) 127 | 128 | { 129 | } 130 | 131 | for (var i in noExtraBraces) { 132 | 133 | return i 134 | 135 | } 136 | 137 | } 138 | noExtraBraces() 139 | 140 | 141 | // weird bug function 142 | for (var i = 0 ; i < 42; i++ ) { 143 | } 144 | 145 | function getRequests (cb) { 146 | foo({ 147 | }, function (err, resp, body) { 148 | cb(err, resp, body) 149 | }) 150 | } 151 | getRequests() 152 | 153 | // jsx 154 | var React = require('react') 155 | 156 | var testClass = React.createClass({ 157 | render: function () { 158 | return
159 | } 160 | }) 161 | 162 | module.exports = testClass 163 | 164 | // spacing around Property names (key-spacing) 165 | void { 166 | testing :123 167 | } 168 | 169 | // Test start of line semicolins 170 | var gloopy = 12 171 | [1,2,3].map(function () {}) 172 | console.log(gloopy) 173 | 174 | // Test member accessors 175 | var array = [1,2,3] 176 | var val = array[0] 177 | var val2 = array[1] 178 | noop(val, val2) 179 | -------------------------------------------------------------------------------- /test/testFile.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const fs = require('fs') 3 | const fmt = require('../').transform 4 | const standard = require('standard') 5 | const inspect = require('util').inspect 6 | 7 | function testFile (filePath, depth) { 8 | // Reads a file, formats its contents then lints it with standard 9 | // Test fails if there are linting errors or warnings 10 | // Inspect depth is optional 11 | const basename = path.basename(filePath) 12 | function test (t) { 13 | fs.readFile(filePath, { encoding: 'utf8' }, function (err, data) { 14 | t.error(err, 'read ' + basename + ' file without error ') 15 | 16 | let formatted 17 | 18 | try { 19 | formatted = fmt(data) 20 | } catch (e) { 21 | t.error(e, 'format ' + basename + ' without error') 22 | } 23 | 24 | standard.lintText(formatted, function (err, result) { 25 | t.error(err, 'linting ' + basename + ' should be error free') 26 | t.equal(result.errorCount, 0, basename + ' error free after formatting') 27 | t.equal(result.warningCount, 0, basename + ' warning free after formatting') 28 | if (result.errorCount || result.warningCount !== 0) { 29 | // If there is an issue, print the details 30 | console.log(inspect(result, { depth: depth || null })) 31 | } 32 | t.end() 33 | }) 34 | }) 35 | } 36 | return test 37 | } 38 | 39 | module.exports = testFile 40 | --------------------------------------------------------------------------------