Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ node_modules

.grunt
.DS_Store

.idea/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ ignored.
It can be installed via `npm` using:

``` bash
npm install -g jsinspect
npm install -g jsinspect-next
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion bin/jsinspect
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ if (program.ignore) {
ignorePatterns.push(program.ignore);
}

var extensions = ['.js', '.jsx'];
var extensions = ['.js', '.jsx', '.vue'];
try {
paths = filepaths.getSync(suppliedPaths, {
ext: extensions,
Expand Down
5 changes: 5 additions & 0 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var fs = require('fs');
var parse = require('./parser').parse;
var Match = require('./match');
var NodeUtils = require('./nodeUtils');
var parseVueFile = require('./vue-parser').parseVueFile;
var path = require('path');
var crypto = require('crypto');
var stable = require('stable');

Expand Down Expand Up @@ -54,6 +56,9 @@ class Inspector extends EventEmitter {
// File contents are split to allow for specific line extraction
this._filePaths.forEach((filePath) => {
var src = fs.readFileSync(filePath, {encoding: 'utf8'});
if (path.extname(filePath) === '.vue') {
src = parseVueFile(src);
}
this._fileContents[filePath] = src.split('\n');
try {
var syntaxTree = parse(src, filePath);
Expand Down
77 changes: 77 additions & 0 deletions lib/vue-parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
var parse5 = require('parse5');
var deindent = require('de-indent');

var splitRE = /\r?\n/g;
var emptyRE = /^\s*$/;
var commentSymbols = {
'iced': '#',
'iced-jsx': '#',
'iced-redux': '#',
'coffee': '#',
'coffee-jsx': '#',
'coffee-redux': '#',
'purs': '--',
'ulmus': '--'
};

exports.parseVueFile = function (content) {
var result = '';

var fragment = parse5.parseFragment(content, {
locationInfo: true
});

fragment.childNodes.forEach(function (node) {
var type = node.tagName;
var lang = getAttribute(node, 'lang');

// skip empty script tags
if (type === 'script') {
if (!node.childNodes || !node.childNodes.length) {
return;
}

// extract part
var start = node.childNodes[0].__location.startOffset;
var end = node.childNodes[node.childNodes.length - 1].__location.endOffset;

// preserve other parts as commenets so that linters
// and babel can output correct line numbers in warnings
result = commentScript(content.slice(0, start), lang) +
deindent(content.slice(start, end)) +
commentScript(content.slice(end), lang);
}
});

return result;
}

function commentScript(content, lang) {
var symbol = getCommentSymbol(lang);
var lines = content.split(splitRE);
return lines.map(function (line, index) {
// preserve EOL
if (index === lines.length - 1 && emptyRE.test(line)) {
return '';
} else {
return symbol + (emptyRE.test(line) ? '' : ' ' + line);
}
}).join('\n');
}

function getCommentSymbol(lang) {
return commentSymbols[lang] || '//';
}

function getAttribute(node, name) {
if (node.attrs) {
var i = node.attrs.length;
var attr;
while (i--) {
attr = node.attrs[i];
if (attr.name === name) {
return attr.value;
}
}
}
}
Loading