Skip to content

Instantly share code, notes, and snippets.

@jenya239
Created September 6, 2018 12:46
Show Gist options
  • Save jenya239/f9a8744f43b1b4da8a65fb0cb347f013 to your computer and use it in GitHub Desktop.
Save jenya239/f9a8744f43b1b4da8a65fb0cb347f013 to your computer and use it in GitHub Desktop.
simple semi es6 to coffee2
fs =require 'fs'
path =require 'path'
coffeelint =require 'coffeelint'
debug =console .log
file =process .argv[ 2 ]
content =fs .readFileSync file, 'utf8'
# $$ Inserts a "$".
# $& Inserts the matched substring.
# $` Inserts the portion of the string that precedes the matched substring.
# $' Inserts the portion of the string that follows the matched substring.
# $n Where n is a positive integer less than 100,
# inserts the nth parenthesized submatch string, provided the
# first argument was a RegExp object. Note that this is 1-indexed.
class Replacing
constructor: ( @target, @search, @value )->
@re =new RegExp @search .source, 'gm'
@result =@target .replace @re, @value
replaces =[
[ /this\s*\./, '@' ],
[ /\bthis\b/, '@' ],
[ /\s*{(\s*[^}])/, '$1' ],
[ /([^{])\s*}\w*/, '$1' ],
[ /;/, '' ],
[ /\blet\b\s*/, '' ],
[ /\bconst\b\s*/, '' ],
[ /\bvar\b\s*/, '' ]
[ /\bof\b/, 'in' ],
[ /===/, '==' ],
[ /\bof\b/, 'in' ],
[ /\bif\b\s*\(\s*([^\(\)]*)\s*\)\s*\n/, 'if $1\n' ],
[ /\bfunction\b\s*(\([\w\s\,\=\'\"\/\:]*\))/, '$1 ->' ],
[ /(\n\s*for)\s*\(([^\(\)]*)\)\s*\n/, '$1$2\n' ],
[ /(\n\s*\w+)\s*(\([\w\s\,\=\'\"]*\))\s*\n/, '$1: $2->\n' ],
[ /(\w+)\s*\(\s*([^\(\)]*\w+[^\(\)]*)\s*\)\s*\n/, '$1 $2\n' ]
]
res =content
for r in replaces
r =new Replacing res, r[ 0 ], r[ 1 ]
res =r .result
ext =path .extname file
base =path .basename file, ext
ignore =level: 'ignore'
lints =coffeelint .lint res,
no_tabs: ignore
no_trailing_whitespace: ignore
indentation: ignore
error =false
for lint in lints
if lint .level =='error'
error =true
if error
debug lints
#else
fs .writeFileSync base + '.coffee', res, 'utf8'
#debug res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment