vue-eslint-parser
Advanced tools
Comparing version 0.1.0 to 0.1.1
88
index.js
@@ -20,3 +20,49 @@ /** | ||
const LINE_TERMINATORS = /\r\n|\r|\n|\u2028|\u2029/g | ||
/** | ||
* Calculates the end location. | ||
* | ||
* @param {string} raw - The text of the target token. | ||
* @param {number} startLine - The start line of the target token. | ||
* @param {number} startColumn - The start column of the target token. | ||
* @returns {{line: number, column: number}} The end location. | ||
* @private | ||
*/ | ||
function calcLocEnd(raw, startLine, startColumn) { | ||
const lines = raw.split(LINE_TERMINATORS) | ||
const line = startLine + lines.length - 1 | ||
const column = (lines.length === 1) | ||
? startColumn + raw.length | ||
: lines[lines.length - 1].length | ||
return {line, column} | ||
} | ||
/** | ||
* Creates the token with the given parameters. | ||
* | ||
* @param {string} value - The token value to create. | ||
* @param {string} text - The whole text. | ||
* @param {object} location - The location object of `parse5` module. | ||
* @returns {object} The created token object. | ||
* @private | ||
*/ | ||
function createToken(value, text, location) { | ||
const type = "Punctuator" | ||
const start = location.startOffset | ||
const end = location.endOffset | ||
const line = location.line | ||
const column = location.col - 1 | ||
const range = [start, end] | ||
const raw = text.slice(start, end) | ||
const loc = { | ||
start: {line, column}, | ||
end: calcLocEnd(raw, line, column), | ||
} | ||
return {type, value, raw, start, end, range, loc} | ||
} | ||
/** | ||
* Extracts the text of the 1st script element in the given text. | ||
@@ -26,23 +72,37 @@ * | ||
* @returns {{text: string, offset: number}} The information of the 1st script. | ||
* @private | ||
*/ | ||
function extractFirstScript(originalText) { | ||
const parser = new SAXParser({locationInfo: true}) | ||
let inScript = false | ||
let inTemplate = 0 | ||
let startToken = null | ||
let endToken = null | ||
let text = "" | ||
let offset = 0 | ||
parser.on("startTag", (name, attrs, selfClosing) => { | ||
if (name === "script" && !selfClosing) { | ||
inScript = true | ||
parser.on("startTag", (name, attrs, selfClosing, location) => { | ||
if (selfClosing) { | ||
return | ||
} | ||
if (name === "template") { | ||
inTemplate += 1 | ||
} | ||
if (inTemplate === 0 && name === "script") { | ||
startToken = createToken("<script>", originalText, location) | ||
} | ||
}) | ||
parser.on("endTag", (name) => { | ||
if (name === "script") { | ||
inScript = false | ||
parser.on("endTag", (name, location) => { | ||
if (inTemplate > 0 && name === "template") { | ||
inTemplate -= 1 | ||
} | ||
if (startToken != null && name === "script") { | ||
endToken = createToken("</script>", originalText, location) | ||
parser.stop() | ||
} | ||
}) | ||
parser.on("text", (scriptText, location) => { | ||
if (inScript && text === "") { | ||
const lineTerminators = "\n".repeat(location.line - 1) | ||
const spaces = " ".repeat(location.startOffset - location.line + 1) | ||
if (startToken != null) { | ||
const countLines = location.line - 1 | ||
const lineTerminators = "\n".repeat(countLines) | ||
const spaces = " ".repeat(location.startOffset - countLines) | ||
text = `${spaces}${lineTerminators}${scriptText}` | ||
@@ -54,3 +114,3 @@ offset = location.startOffset | ||
return {text, offset} | ||
return {startToken, endToken, text, offset} | ||
} | ||
@@ -83,4 +143,10 @@ | ||
ast.start = script.offset | ||
if (script.startToken) { | ||
ast.tokens.unshift(script.startToken) | ||
} | ||
if (script.endToken) { | ||
ast.tokens.push(script.endToken) | ||
} | ||
return ast | ||
} |
{ | ||
"name": "vue-eslint-parser", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "The ESLint custom parser for `.vue` files.", | ||
@@ -5,0 +5,0 @@ "engines": { |
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
9199
132