vue-jscodeshift-adapter
Advanced tools
Comparing version 2.1.0 to 2.2.0
# 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; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19148
2
26
527
- Removedcheerio@^1.0.0-rc.2
- Removeddetect-indent@^6.0.0
- Removedindent-string@^4.0.0
- Removedboolbase@1.0.0(transitive)
- Removedcheerio@1.0.0(transitive)
- Removedcheerio-select@2.1.0(transitive)
- Removedcss-select@5.1.0(transitive)
- Removedcss-what@6.1.0(transitive)
- Removeddetect-indent@6.1.0(transitive)
- Removeddom-serializer@2.0.0(transitive)
- Removeddomelementtype@2.3.0(transitive)
- Removeddomhandler@5.0.3(transitive)
- Removeddomutils@3.1.0(transitive)
- Removedencoding-sniffer@0.2.0(transitive)
- Removedentities@4.5.0(transitive)
- Removedhtmlparser2@9.1.0(transitive)
- Removediconv-lite@0.6.3(transitive)
- Removedindent-string@4.0.0(transitive)
- Removednth-check@2.1.1(transitive)
- Removedparse5@7.2.1(transitive)
- Removedparse5-htmlparser2-tree-adapter@7.1.0(transitive)
- Removedparse5-parser-stream@7.1.2(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedundici@6.21.0(transitive)
- Removedwhatwg-encoding@3.1.1(transitive)
- Removedwhatwg-mimetype@4.0.0(transitive)