Skip to content

Commit

Permalink
Fix lineNumber in last line without ending. (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed Mar 23, 2018
1 parent deb8b98 commit 62f5ba0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 56 deletions.
85 changes: 40 additions & 45 deletions lib/block-fixer.js → lib/parse-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,51 @@
const reNewLine = /(?:\r?\n|\r)/gm;
const getSyntax = require("./get-syntax");

function DocumentFixer (source) {
let match;
const lines = [];
reNewLine.lastIndex = 0;
while ((match = reNewLine.exec(source))) {
lines.push(match.index);
}
this.lines = lines;
}

DocumentFixer.prototype = {
block: function (style) {
return new LocalFixer(this.lines, style);
},
};

function LocalFixer (lines, style) {
let line = 0;
let column = style.startIndex;
lines.some((lineEndIndex, lineNumber) => {
if (lineEndIndex >= style.startIndex) {
line = lineNumber--;
if (lineNumber in lines) {
column = style.startIndex - lines[lineNumber] - 1;
class LocalFixer {
constructor (lines, style) {
let line = 0;
let column = style.startIndex;
lines.some((lineEndIndex, lineNumber) => {
if (lineEndIndex >= style.startIndex) {
line = lineNumber--;
if (lineNumber in lines) {
column = style.startIndex - lines[lineNumber] - 1;
}
return true;
}
return true;
}
});

this.line = line;
this.column = column;
this.style = style;
}
});

LocalFixer.prototype = {
object: function (object) {
this.line = line;
this.column = column;
this.style = style;
}
object (object) {
if (object) {
if (object.line === 1) {
object.column += this.column;
}
object.line += this.line;
}
},
node: function (node) {
}
node (node) {
this.object(node.source.start);
this.object(node.source.end);
},
root: function (root) {
}
root (root) {
this.object(root.source.start);
root.walk(node => {
this.node(node);
});
},
error: function (error) {
}
error (error) {
if (error && error.name === "CssSyntaxError") {
this.object(error);
this.object(error.input);
error.message = error.message.replace(/:\d+:\d+:/, ":" + error.line + ":" + error.column + ":");
}
return error;
},
parse: function (opts) {
}
parse (opts) {
const style = this.style;
const syntax = getSyntax(opts, style.lang);
let root;
Expand All @@ -84,7 +67,19 @@ LocalFixer.prototype = {
});

return root;
},
};
}
}

module.exports = DocumentFixer;
function docFixer (source, opts) {
let match;
const lines = [];
reNewLine.lastIndex = 0;
while ((match = reNewLine.exec(source))) {
lines.push(match.index);
}
lines.push(source.length);
return function parseStyle (style) {
return new LocalFixer(lines, style).parse(opts);
};
}
module.exports = docFixer;
7 changes: 3 additions & 4 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Document = require("./document");
const jsParser = require("./js-parser");
const htmlParser = require("./html-parser");
const mdParser = require("./md-parser");
const BlockFixer = require("./block-fixer");
const docFixer = require("./parse-style");

function parser (source, opts) {
function filenameMatch (reg) {
Expand All @@ -25,13 +25,12 @@ function parser (source, opts) {
return;
}
const document = new Document();
const documentLocalFixer = new BlockFixer(source);
const parseStyle = docFixer(source, opts);
let index = 0;
[].concat(styleJs, styleHtm, styleMd).filter(Boolean).sort(function (a, b) {
return a.startIndex - b.startIndex;
}).forEach(style => {
const localFixer = documentLocalFixer.block(style);
const root = localFixer.parse(opts);
const root = parseStyle(style);
root.raws.beforeStart = source.slice(index, style.startIndex);
index = style.startIndex + style.content.length;
document.nodes.push(root);
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-html",
"version": "0.16.0",
"version": "0.17.0",
"description": "PostCSS Syntax for parsing HTML / Markdown / Vue Component",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -66,7 +66,7 @@
"@types/babel-traverse": "^6.25.3",
"@types/babylon": "^6.16.2",
"@types/chai": "^4.1.2",
"@types/mocha": "^2.2.48",
"@types/mocha": "^5.0.0",
"@types/stylelint": "^7.11.0",
"autoprefixer": "^8.2.0",
"chai": "^4.1.2",
Expand All @@ -77,9 +77,9 @@
"eslint-plugin-node": "^6.0.1",
"eslint-plugin-promise": "^3.7.0",
"eslint-plugin-standard": "^3.0.1",
"mocha": "^5.0.4",
"mocha": "^5.0.5",
"nyc": "^11.6.0",
"postcss": "^6.0.20",
"postcss": "^6.0.21",
"postcss-less": "^1.1.5",
"postcss-parser-tests": "^6.2.1",
"postcss-safe-parser": "^3.0.1",
Expand Down
21 changes: 18 additions & 3 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ describe("html tests", () => {
"</html>",
].join("\n");
return postcss([
function (root) {
root => {
root.nodes.push(postcss.parse("b {}"));
},
]).process(css, {
Expand All @@ -451,15 +451,15 @@ describe("html tests", () => {
});
});

it("stringify for node aray", () => {
it("stringify for nodes array", () => {
const css = [
"<html>",
"<style>",
"</style>",
"</html>",
].join("\n");
return postcss([
function (root) {
root => {
root.nodes = [postcss.parse("b {}")];
},
]).process(css, {
Expand All @@ -470,4 +470,19 @@ describe("html tests", () => {
expect(result.content).to.be.ok;
});
});

it("<style> tag in last line", () => {
const css = [
"\n<style>b{}</style>",
].join("\n");
return postcss([
]).process(css, {
syntax: syntax,
from: "push.html",
}).then(result => {
expect(result.root.nodes).to.be.lengthOf(1);
expect(result.root.first.source.start.line).to.equal(2);
expect(result.root.first.source.start.column).to.equal(8);
});
});
});

0 comments on commit 62f5ba0

Please sign in to comment.