edvinh / node-sloc

A small tool written in node for counting source lines of code.
10 stars 3 forks source link

Suggestion for getCommentChars(exts) in utils.js #1

Closed eHammarstrom closed 7 years ago

eHammarstrom commented 7 years ago

Currently file-extensions.js merely contains a listing of files but could also contain the meta-info that is needed for getCommentChars().

Current state utils.js:

function getCommentChars (extension) {
    switch (extension) {
        case 'c':
            return { ... }
        case 'cc:
            ...
    }
}

suggestion, move meta-info such as multi-line and single-line data into file-extensions.js

Example file-extensions.js:

module.exports = [
    { lang: 'java', single: '//', multi: { start: '/*', end: '*/' } },
    { lang: 'rb', single: '#', multi: { start: '=begin', end: '=end' } }
]

Example usage:

// line 7 in bin/node-sloc.js
const allowedExtensions = require('./file-extensions.js').map(x => x.lang)

console.log(allowedExtensions)
    // [ 'java', 'rb' ]

// line ~158 in utils.js
function getCommentChars (extension) {
    let ext = allowedExtensions.find(x => x.lang === extension)

    return {
        line: ext.single,
        multi: ext.multi
    }
}

console.log(getCommentChars('java'))
    // { line: '//', multi: { start: '/*', end: '*/' } }

Sounds sane enough?