vue-jscodeshift-adapter
Advanced tools
Comparing version
# Change Log | ||
## [2.2.0] - 2020-10-02 | ||
- Preserve indent of `<script>` and `<style>` tags. Thanks to @SevInf | ||
## [2.1.0] - 2020-05-15 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "vue-jscodeshift-adapter", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Run jscodeshift on Vue single file components", | ||
@@ -33,5 +33,2 @@ "main": "src/index.js", | ||
"dependencies": { | ||
"cheerio": "^1.0.0-rc.2", | ||
"detect-indent": "^6.0.0", | ||
"indent-string": "^4.0.0", | ||
"vue-sfc-descriptor-to-string": "^1.0.0", | ||
@@ -38,0 +35,0 @@ "vue-template-compiler": "^2.5.13" |
@@ -12,3 +12,3 @@ const descriptorToString = require('vue-sfc-descriptor-to-string'); | ||
const sfcDescriptor = parseSfc(fileInfo.source); | ||
const { sfcDescriptor, indents } = parseSfc(fileInfo.source); | ||
const scriptBlock = sfcDescriptor.script; | ||
@@ -24,5 +24,3 @@ | ||
return descriptorToString(sfcDescriptor, { | ||
indents: { | ||
template: 0 | ||
} | ||
indents | ||
}); | ||
@@ -29,0 +27,0 @@ } else { |
const compiler = require('vue-template-compiler'); | ||
const cheerio = require('cheerio'); | ||
const detectIndent = require('detect-indent'); | ||
const indentString = require('indent-string'); | ||
module.exports = function parse(source) { | ||
const sfcDescriptor = compiler.parseComponent(source); | ||
const indents = {} | ||
if (sfcDescriptor.template) { | ||
sfcDescriptor.template.content = fixTemplateIndent(sfcDescriptor, source); | ||
indents.template = detectIndent(sfcDescriptor.template, source); | ||
} | ||
return sfcDescriptor; | ||
if (sfcDescriptor.script) { | ||
indents.script = detectIndent(sfcDescriptor.script, source); | ||
} | ||
if (sfcDescriptor.styles.length > 0) { | ||
indents.style = detectIndent(sfcDescriptor.styles[0], source); | ||
} | ||
return { sfcDescriptor, indents }; | ||
}; | ||
// assumes sfc has a <template> | ||
// returns content for <template> with correct indent | ||
function fixTemplateIndent(sfcDescriptor, source) { | ||
const $ = cheerio.load(source); | ||
function detectIndent(sfcBlock, source) { | ||
const nonEmptyPaddingsPerLine = source.substring(sfcBlock.start, sfcBlock.end) | ||
.split('\n') | ||
.filter(line => line !== '') | ||
.map(getLinePadding); | ||
// contents of template including outer <template> pair | ||
const fullTemplate = $.html($('template')); | ||
const templateIndent = detectIndent(fullTemplate); | ||
if (nonEmptyPaddingsPerLine.length === 0) { | ||
return 0; | ||
} | ||
return Math.min(...nonEmptyPaddingsPerLine) | ||
} | ||
const templateBlock = sfcDescriptor.template; | ||
return indentString(templateBlock.content, templateIndent.amount, templateIndent.indent); | ||
function getLinePadding(line) { | ||
const spacesMatch = line.match(/^ +/); | ||
if (!spacesMatch) { | ||
return 0; | ||
} | ||
return spacesMatch[0].length; | ||
} |
19148
6.14%2
-60%26
18.18%527
14.07%- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed