Socket
Socket
Sign inDemoInstall

eslint-mdx

Package Overview
Dependencies
Maintainers
4
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-mdx - npm Package Compare versions

Comparing version 1.11.0 to 1.11.1

11

CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

## [1.11.1](https://github.com/mdx-js/eslint-mdx/compare/v1.11.0...v1.11.1) (2021-03-21)
### Bug Fixes
* add helper getPositionAt to fix incorrect loc ([#293](https://github.com/mdx-js/eslint-mdx/issues/293)) ([260a07e](https://github.com/mdx-js/eslint-mdx/commit/260a07e8bccf76508f19df9501b78a8a402e8a34)), closes [#292](https://github.com/mdx-js/eslint-mdx/issues/292)
# [1.11.0](https://github.com/mdx-js/eslint-mdx/compare/v1.10.0...v1.11.0) (2021-03-18)

@@ -8,0 +19,0 @@

52

lib/cjs.js

@@ -83,7 +83,24 @@ 'use strict';

};
var restoreNodeLocation = function (node, startLine, offset) {
// fix #292
var getPositionAt = function (code, offset) {
var currOffset = 0;
var lines = code.split('\n');
// eslint-disable-next-line unicorn/no-for-loop
for (var index = 0; index < lines.length; index++) {
var line = index + 1;
var nextOffset = currOffset + lines[index].length;
if (nextOffset >= offset) {
return {
line: line,
column: offset - currOffset,
};
}
currOffset = nextOffset + 1; // add a line break `\n` offset
}
};
var restoreNodeLocation = function (node, point) {
if (node && typeof node === 'object') {
for (var _i = 0, _a = Object.values(node); _i < _a.length; _i++) {
var value = _a[_i];
restoreNodeLocation(value, startLine, offset);
restoreNodeLocation(value, point);
}

@@ -94,19 +111,16 @@ }

}
var _b = node.loc, startLoc = _b.start, endLoc = _b.end, range = node.range;
var start = range[0] + offset;
var end = range[1] + offset;
var restoredStartLine = startLine + startLoc.line;
var restoredEndLine = startLine + endLoc.line;
var _b = node.loc, startLoc = _b.start, endLoc = _b.end, _c = node.range, start = _c[0], end = _c[1];
var range = [(start += point.offset), (end += point.offset)];
return Object.assign(node, {
start: start,
end: end,
range: [start, end],
range: range,
loc: {
start: {
line: restoredStartLine,
column: startLoc.column + (restoredStartLine === 1 ? offset : 0),
line: point.line + startLoc.line,
column: startLoc.column + (startLoc.line === 1 ? point.column : 0),
},
end: {
line: restoredEndLine,
column: endLoc.column + (restoredEndLine === 1 ? offset : 0),
line: point.line + endLoc.line,
column: endLoc.column + (endLoc.line === 1 ? point.column : 0),
},

@@ -404,3 +418,3 @@ },

var normalizedNode = _a[_i];
_this._nodeToAst(normalizedNode, options);
_this._nodeToAst(code, normalizedNode, options);
}

@@ -513,3 +527,3 @@ },

// @internal
Parser.prototype._nodeToAst = function (node, options) {
Parser.prototype._nodeToAst = function (code, node, options) {
var _a;

@@ -547,7 +561,12 @@ if (node.data && node.data.jsxType === 'JSXElementWithHTMLComments') {

}
var offset = start - program.range[0];
var startPoint = {
line: startLine,
// #279 related
column: getPositionAt(code, start).column,
offset: start,
};
for (var _i = 0, AST_PROPS_1 = AST_PROPS; _i < AST_PROPS_1.length; _i++) {
var prop = AST_PROPS_1[_i];
(_a = this._ast[prop]).push.apply(_a, program[prop].map(function (item) {
return restoreNodeLocation(item, startLine, offset);
return restoreNodeLocation(item, startPoint);
}));

@@ -586,2 +605,3 @@ }

exports.first = first;
exports.getPositionAt = getPositionAt;
exports.hasProperties = hasProperties;

@@ -588,0 +608,0 @@ exports.isCloseTag = isCloseTag;

@@ -1,1 +0,1 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("path"),r=require("remark-mdx"),n=require("remark-parse"),s=require("unified");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=o(t),a=o(r),p=o(n),u=o(s),l=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],c=["JSXElement","JSXFragment"],f=function(e){return c.includes(e.type)},m=function(e){if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError("Invalid custom parser for `eslint-mdx`: "+e);return[e]}for(var t=[],r=0,n=l;r<n.length;r++){var s=n[r];try{var o=require(s),i="parseForESLint"in o?o.parseForESLint:o.parse;i&&t.push(i)}catch(e){}}return t},x=function(e){var t=e.start.offset,r=e.end.offset;return{range:[t,r],loc:e,start:t,end:r}},h=function(e,t){return"object"==typeof e&&e&&t.every((function(t){return t in e}))},d=function(e,t,r){if(e&&"object"==typeof e)for(var n=0,s=Object.values(e);n<s.length;n++){var o=s[n];d(o,t,r)}if(!h(e,["loc","range"]))return e;var i=e.loc,a=i.start,p=i.end,u=e.range,l=u[0]+r,c=u[1]+r,f=t+a.line,m=t+p.line;return Object.assign(e,{start:l,end:c,range:[l,c],loc:{start:{line:f,column:a.column+(1===f?r:0)},end:{line:m,column:p.column+(1===m?r:0)}}})},_=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e.reduce((function(e,t){return e.push.apply(e,Array.isArray(t)?t:null==t?[]:[t]),e}),[])},v=function(e){return e&&e[e.length-1]},E="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",g="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+E+"*\\s*>",y="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",S="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+E+"*\\s*\\/?>",T="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",N=new RegExp("^(?:"+g+")$"),b=new RegExp("^(?:"+y+")$"),O=new RegExp("^(?:"+g+"[^<]*"+y+")$"),L=new RegExp("^(?:"+S+")$"),A=new RegExp("^(?:"+T+")$"),P=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),R=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),C=function(e){return N.test(e.trim())},J=function(e){return b.test(e.trim())},j=function(e){return A.test(e.trim())},w=function(e){return O.test(e.trim())},z=function(e){return L.test(e.trim())},F=function(){function t(e){var t=e.code,r=e.enter;this.code=t,this._enter=r}return t.prototype.combineLeftJsxNodes=function(t,r){var n,s=t[0].position.start,o=e.__assign({},v(t).position.end);return r&&(null===(n=r.position.indent)||void 0===n?void 0:n.length)>0&&(o.offset+=r.position.indent.reduce((function(e,t,r){return e+(r?t+1:0)}),0)),{type:"jsx",data:t[0].data,value:this.code.slice(s.offset,o.offset),position:{start:s,end:o}}},t.prototype.combineJsxNodes=function(t,r){var n=this,s=0,o=!1,i=[],a=t.length;return t.reduce((function(t,p,u){if("jsx"===p.type){var l=p.value;if(C(l))s++,o=!0,i.push(p);else{if(J(l))s--,i.push(p);else if(j(l)||z(l)||w(l))i.push(p);else{u||(s++,o=!0);try{i.push.apply(i,_(B.normalizeJsxNode(p,r)))}catch(e){if(!s){var c=p.position.start;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(l)),{lineNumber:c.line,column:c.column,index:c.offset})}i.push(p)}}if(!s){var f=i.findIndex((function(e){return"string"==typeof e.value&&C(e.value)}));-1===f?o?t.push(n.combineLeftJsxNodes(i,r)):t.push.apply(t,i):t.push.apply(t,e.__spreadArray(e.__spreadArray([],i.slice(0,f)),[n.combineLeftJsxNodes(i.slice(f),r)])),i.length=0}}}else s?i.push(p):t.push(p);return u===a-1&&i.length>0&&t.push(n.combineLeftJsxNodes(i,r)),t}),[])},t.prototype.traverse=function(e,t){if(e){var r=e.children;if(r)for(var n=e,s=0,o=r=e.children=this.combineJsxNodes(r,n);s<o.length;s++){var i=o[s];this.traverse(i,n)}this._enter(e,t)}},t}(),X=function(e,t){return new F(t).traverse(e)},G=u.default().use(p.default).freeze(),M=G().use(a.default).freeze(),k=["body","comments","tokens"],Z=["export","import","jsx"],$=["column","lineNumber"],q=[".mdx"],I=[".md"],D={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},W="<$>".length,H=function(){function t(){this._options=D,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}return t.prototype.normalizeJsxNode=function(t,r,n){void 0===n&&(n=this._options);var s=t.value;if("jsx"!==t.type||j(s))return t;if(P.exec(s)){var o=[],i=t.position.start,a=i.line,p=i.column,u=i.offset,l=t.data;Object.assign(t,{data:e.__assign(e.__assign({},l),{jsxType:"JSXElementWithHTMLComments",comments:o,inline:!!r&&"root"!==r.type}),value:s.replace(R,(function(e,t,r,n,i){var l=i+e.length,c=s.slice(0,i).split("\n"),f=s.slice(0,l).split("\n"),m="{/"+"*".repeat(t.length-2)+r+"*".repeat(n.length-2)+"/}",x=c.length-1,h=f.length-1;return o.push({fixed:m,loc:{start:{line:a+x,column:v(c).length+(x?0:p-1),offset:u+i},end:{line:a+h,column:v(f).length+(h?0:p-1),offset:u+l}},origin:e}),m}))})}return this._normalizeJsxNodes(t,n)},t.prototype.parse=function(e,t){return this.parseForESLint(e,t).ast},t.prototype.parseForESLint=function(t,r){var n=this,s=i.default.extname(r.filePath),o=q.concat(r.extensions||[]).includes(s),a=I.concat(r.markdownExtensions||[]).includes(s);if(!o&&!a)return this._eslintParse(t,r);var p=(o?M:G).parse(t);return this._ast=e.__assign(e.__assign({},x(p.position)),{type:"Program",sourceType:r.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},o&&X(p,{code:t,enter:function(e,t){if(Z.includes(e.type))for(var s=0,o=_(n.normalizeJsxNode(e,t,r));s<o.length;s++){var i=o[s];n._nodeToAst(i,r)}}}),{ast:this._ast,services:this._services}},t.prototype._eslintParse=function(e,t){var r,n;this._parsers&&t.parser===this._options.parser||(this._parsers=m(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(var s=0,o=this._parsers;s<o.length;s++){var i=o[s];try{r=i(e,this._options);break}catch(e){n||(n=e)}}if(!r&&n)throw n;return"ast"in r&&r.ast?r:{ast:r}},t.prototype._normalizeJsxNodes=function(e,t){var r,n=e.value;try{r=this._eslintParse("<$>"+n+"</$>",t).ast}catch(t){if(h(t,$)){var s=e.position.start;throw"index"in t?t.index+=s.offset-W:"pos"in t&&(t.pos+=s.offset-W),t.column=t.lineNumber>1?t.column:t.column+s.column-W,t.lineNumber+=s.line-1,t}return e}var o=r.body[0].expression;if(!f(o)||o.children.length<=1)return e;var i=e.position.start,a=i.line,p=i.offset,u=e.data;return o.children.reduce((function(e,t){if(!f(t))return e;var r=t.start,s=t.end,o=t.loc,i=void 0===o?{start:{column:r,line:1},end:{column:s,line:1}}:o,l=i.start,c=i.end,m=t.range,x=void 0===m?[r,s]:m,h=a+l.line-1,d=a+c.line-1,_=x[0]-W,v=x[1]-W;return e.push({type:"jsx",data:e.length>0?null:u,value:n.slice(_,v),position:{start:{line:h,column:a===h?l.column-W:l.column,offset:p+_},end:{line:d,column:a===h?c.column-W:c.column,offset:p+v}}}),e}),[])},t.prototype._nodeToAst=function(e,t){var r;e.data&&"JSXElementWithHTMLComments"===e.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(e);var n=e.value,s=x(e.position),o=s.loc,i=s.start,a=s.end;if(j(n)){var p=P.exec(n)[2];this._ast.comments.push({type:"Block",value:p,loc:o,range:[i,a]})}else{var u,l=o.start.line-1;try{u=this._eslintParse(n,t).ast}catch(e){throw h(e,$)&&(e.index+=i,e.column=e.lineNumber>1?e.column:e.column+o.start.column,e.lineNumber+=l),e}for(var c=i-u.range[0],f=0,m=k;f<m.length;f++){var _=m[f];(r=this._ast[_]).push.apply(r,u[_].map((function(e){return d(e,l,c)})))}}},t}(),B=new H,U=B.parse,Y=B.parseForESLint;exports.AST_PROPS=k,exports.CLOSE_TAG_REGEX=b,exports.COMMENT_CONTENT_REGEX=P,exports.COMMENT_CONTENT_REGEX_GLOBAL=R,exports.COMMENT_REGEX=A,exports.DEFAULT_EXTENSIONS=q,exports.DEFAULT_PARSER_OPTIONS=D,exports.ES_NODE_TYPES=Z,exports.FALLBACK_PARSERS=l,exports.JSX_TYPES=c,exports.LOC_ERROR_PROPERTIES=$,exports.MARKDOWN_EXTENSIONS=I,exports.OPEN_CLOSE_TAG_REGEX=O,exports.OPEN_TAG_REGEX=N,exports.Parser=H,exports.SELF_CLOSING_TAG_REGEX=L,exports.Traverse=F,exports.arrayify=_,exports.closeTag=y,exports.comment=T,exports.commentClose="(-*--\x3e)",exports.commentContent="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",exports.commentOpen="(\x3c!---*)",exports.first=function(e){return e&&e[0]},exports.hasProperties=h,exports.isCloseTag=J,exports.isComment=j,exports.isJsxNode=f,exports.isOpenCloseTag=w,exports.isOpenTag=C,exports.isSelfClosingTag=z,exports.last=v,exports.mdProcessor=G,exports.mdxProcessor=M,exports.normalizeParser=m,exports.normalizePosition=x,exports.openTag=g,exports.parse=U,exports.parseForESLint=Y,exports.parser=B,exports.restoreNodeLocation=d,exports.selfClosingTag=S,exports.traverse=X;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("path"),r=require("remark-mdx"),n=require("remark-parse"),s=require("unified");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var i=o(t),a=o(r),p=o(n),l=o(s),u=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],c=["JSXElement","JSXFragment"],f=function(e){return c.includes(e.type)},m=function(e){if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError("Invalid custom parser for `eslint-mdx`: "+e);return[e]}for(var t=[],r=0,n=u;r<n.length;r++){var s=n[r];try{var o=require(s),i="parseForESLint"in o?o.parseForESLint:o.parse;i&&t.push(i)}catch(e){}}return t},x=function(e){var t=e.start.offset,r=e.end.offset;return{range:[t,r],loc:e,start:t,end:r}},h=function(e,t){return"object"==typeof e&&e&&t.every((function(t){return t in e}))},d=function(e,t){for(var r=0,n=e.split("\n"),s=0;s<n.length;s++){var o=s+1,i=r+n[s].length;if(i>=t)return{line:o,column:t-r};r=i+1}},_=function(e,t){if(e&&"object"==typeof e)for(var r=0,n=Object.values(e);r<n.length;r++){var s=n[r];_(s,t)}if(!h(e,["loc","range"]))return e;var o=e.loc,i=o.start,a=o.end,p=e.range,l=p[0],u=p[1],c=[l+=t.offset,u+=t.offset];return Object.assign(e,{start:l,end:u,range:c,loc:{start:{line:t.line+i.line,column:i.column+(1===i.line?t.column:0)},end:{line:t.line+a.line,column:a.column+(1===a.line?t.column:0)}}})},v=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e.reduce((function(e,t){return e.push.apply(e,Array.isArray(t)?t:null==t?[]:[t]),e}),[])},g=function(e){return e&&e[e.length-1]},E="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",y="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+E+"*\\s*>",S="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",T="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+E+"*\\s*\\/?>",N="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",b=new RegExp("^(?:"+y+")$"),O=new RegExp("^(?:"+S+")$"),L=new RegExp("^(?:"+y+"[^<]*"+S+")$"),A=new RegExp("^(?:"+T+")$"),P=new RegExp("^(?:"+N+")$"),R=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),C=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),J=function(e){return b.test(e.trim())},j=function(e){return O.test(e.trim())},w=function(e){return P.test(e.trim())},z=function(e){return L.test(e.trim())},F=function(e){return A.test(e.trim())},X=function(){function t(e){var t=e.code,r=e.enter;this.code=t,this._enter=r}return t.prototype.combineLeftJsxNodes=function(t,r){var n,s=t[0].position.start,o=e.__assign({},g(t).position.end);return r&&(null===(n=r.position.indent)||void 0===n?void 0:n.length)>0&&(o.offset+=r.position.indent.reduce((function(e,t,r){return e+(r?t+1:0)}),0)),{type:"jsx",data:t[0].data,value:this.code.slice(s.offset,o.offset),position:{start:s,end:o}}},t.prototype.combineJsxNodes=function(t,r){var n=this,s=0,o=!1,i=[],a=t.length;return t.reduce((function(t,p,l){if("jsx"===p.type){var u=p.value;if(J(u))s++,o=!0,i.push(p);else{if(j(u))s--,i.push(p);else if(w(u)||F(u)||z(u))i.push(p);else{l||(s++,o=!0);try{i.push.apply(i,v(U.normalizeJsxNode(p,r)))}catch(e){if(!s){var c=p.position.start;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(u)),{lineNumber:c.line,column:c.column,index:c.offset})}i.push(p)}}if(!s){var f=i.findIndex((function(e){return"string"==typeof e.value&&J(e.value)}));-1===f?o?t.push(n.combineLeftJsxNodes(i,r)):t.push.apply(t,i):t.push.apply(t,e.__spreadArray(e.__spreadArray([],i.slice(0,f)),[n.combineLeftJsxNodes(i.slice(f),r)])),i.length=0}}}else s?i.push(p):t.push(p);return l===a-1&&i.length>0&&t.push(n.combineLeftJsxNodes(i,r)),t}),[])},t.prototype.traverse=function(e,t){if(e){var r=e.children;if(r)for(var n=e,s=0,o=r=e.children=this.combineJsxNodes(r,n);s<o.length;s++){var i=o[s];this.traverse(i,n)}this._enter(e,t)}},t}(),G=function(e,t){return new X(t).traverse(e)},M=l.default().use(p.default).freeze(),k=M().use(a.default).freeze(),Z=["body","comments","tokens"],$=["export","import","jsx"],q=["column","lineNumber"],I=[".mdx"],D=[".md"],W={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},H="<$>".length,B=function(){function t(){this._options=W,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}return t.prototype.normalizeJsxNode=function(t,r,n){void 0===n&&(n=this._options);var s=t.value;if("jsx"!==t.type||w(s))return t;if(R.exec(s)){var o=[],i=t.position.start,a=i.line,p=i.column,l=i.offset,u=t.data;Object.assign(t,{data:e.__assign(e.__assign({},u),{jsxType:"JSXElementWithHTMLComments",comments:o,inline:!!r&&"root"!==r.type}),value:s.replace(C,(function(e,t,r,n,i){var u=i+e.length,c=s.slice(0,i).split("\n"),f=s.slice(0,u).split("\n"),m="{/"+"*".repeat(t.length-2)+r+"*".repeat(n.length-2)+"/}",x=c.length-1,h=f.length-1;return o.push({fixed:m,loc:{start:{line:a+x,column:g(c).length+(x?0:p-1),offset:l+i},end:{line:a+h,column:g(f).length+(h?0:p-1),offset:l+u}},origin:e}),m}))})}return this._normalizeJsxNodes(t,n)},t.prototype.parse=function(e,t){return this.parseForESLint(e,t).ast},t.prototype.parseForESLint=function(t,r){var n=this,s=i.default.extname(r.filePath),o=I.concat(r.extensions||[]).includes(s),a=D.concat(r.markdownExtensions||[]).includes(s);if(!o&&!a)return this._eslintParse(t,r);var p=(o?k:M).parse(t);return this._ast=e.__assign(e.__assign({},x(p.position)),{type:"Program",sourceType:r.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},o&&G(p,{code:t,enter:function(e,s){if($.includes(e.type))for(var o=0,i=v(n.normalizeJsxNode(e,s,r));o<i.length;o++){var a=i[o];n._nodeToAst(t,a,r)}}}),{ast:this._ast,services:this._services}},t.prototype._eslintParse=function(e,t){var r,n;this._parsers&&t.parser===this._options.parser||(this._parsers=m(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(var s=0,o=this._parsers;s<o.length;s++){var i=o[s];try{r=i(e,this._options);break}catch(e){n||(n=e)}}if(!r&&n)throw n;return"ast"in r&&r.ast?r:{ast:r}},t.prototype._normalizeJsxNodes=function(e,t){var r,n=e.value;try{r=this._eslintParse("<$>"+n+"</$>",t).ast}catch(t){if(h(t,q)){var s=e.position.start;throw"index"in t?t.index+=s.offset-H:"pos"in t&&(t.pos+=s.offset-H),t.column=t.lineNumber>1?t.column:t.column+s.column-H,t.lineNumber+=s.line-1,t}return e}var o=r.body[0].expression;if(!f(o)||o.children.length<=1)return e;var i=e.position.start,a=i.line,p=i.offset,l=e.data;return o.children.reduce((function(e,t){if(!f(t))return e;var r=t.start,s=t.end,o=t.loc,i=void 0===o?{start:{column:r,line:1},end:{column:s,line:1}}:o,u=i.start,c=i.end,m=t.range,x=void 0===m?[r,s]:m,h=a+u.line-1,d=a+c.line-1,_=x[0]-H,v=x[1]-H;return e.push({type:"jsx",data:e.length>0?null:l,value:n.slice(_,v),position:{start:{line:h,column:a===h?u.column-H:u.column,offset:p+_},end:{line:d,column:a===h?c.column-H:c.column,offset:p+v}}}),e}),[])},t.prototype._nodeToAst=function(e,t,r){var n;t.data&&"JSXElementWithHTMLComments"===t.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(t);var s=t.value,o=x(t.position),i=o.loc,a=o.start,p=o.end;if(w(s)){var l=R.exec(s)[2];this._ast.comments.push({type:"Block",value:l,loc:i,range:[a,p]})}else{var u,c=i.start.line-1;try{u=this._eslintParse(s,r).ast}catch(e){throw h(e,q)&&(e.index+=a,e.column=e.lineNumber>1?e.column:e.column+i.start.column,e.lineNumber+=c),e}for(var f={line:c,column:d(e,a).column,offset:a},m=0,v=Z;m<v.length;m++){var g=v[m];(n=this._ast[g]).push.apply(n,u[g].map((function(e){return _(e,f)})))}}},t}(),U=new B,Y=U.parse,K=U.parseForESLint;exports.AST_PROPS=Z,exports.CLOSE_TAG_REGEX=O,exports.COMMENT_CONTENT_REGEX=R,exports.COMMENT_CONTENT_REGEX_GLOBAL=C,exports.COMMENT_REGEX=P,exports.DEFAULT_EXTENSIONS=I,exports.DEFAULT_PARSER_OPTIONS=W,exports.ES_NODE_TYPES=$,exports.FALLBACK_PARSERS=u,exports.JSX_TYPES=c,exports.LOC_ERROR_PROPERTIES=q,exports.MARKDOWN_EXTENSIONS=D,exports.OPEN_CLOSE_TAG_REGEX=L,exports.OPEN_TAG_REGEX=b,exports.Parser=B,exports.SELF_CLOSING_TAG_REGEX=A,exports.Traverse=X,exports.arrayify=v,exports.closeTag=S,exports.comment=N,exports.commentClose="(-*--\x3e)",exports.commentContent="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",exports.commentOpen="(\x3c!---*)",exports.first=function(e){return e&&e[0]},exports.getPositionAt=d,exports.hasProperties=h,exports.isCloseTag=j,exports.isComment=w,exports.isJsxNode=f,exports.isOpenCloseTag=z,exports.isOpenTag=J,exports.isSelfClosingTag=F,exports.last=g,exports.mdProcessor=M,exports.mdxProcessor=k,exports.normalizeParser=m,exports.normalizePosition=x,exports.openTag=y,exports.parse=Y,exports.parseForESLint=K,exports.parser=U,exports.restoreNodeLocation=_,exports.selfClosingTag=T,exports.traverse=G;

@@ -66,6 +66,23 @@ import path from 'path';

properties.every(property => property in obj);
const restoreNodeLocation = (node, startLine, offset) => {
// fix #292
const getPositionAt = (code, offset) => {
let currOffset = 0;
const lines = code.split('\n');
// eslint-disable-next-line unicorn/no-for-loop
for (let index = 0; index < lines.length; index++) {
const line = index + 1;
const nextOffset = currOffset + lines[index].length;
if (nextOffset >= offset) {
return {
line,
column: offset - currOffset,
};
}
currOffset = nextOffset + 1; // add a line break `\n` offset
}
};
const restoreNodeLocation = (node, point) => {
if (node && typeof node === 'object') {
for (const value of Object.values(node)) {
restoreNodeLocation(value, startLine, offset);
restoreNodeLocation(value, point);
}

@@ -76,19 +93,16 @@ }

}
const { loc: { start: startLoc, end: endLoc }, range, } = node;
const start = range[0] + offset;
const end = range[1] + offset;
const restoredStartLine = startLine + startLoc.line;
const restoredEndLine = startLine + endLoc.line;
let { loc: { start: startLoc, end: endLoc }, range: [start, end], } = node;
const range = [(start += point.offset), (end += point.offset)];
return Object.assign(node, {
start,
end,
range: [start, end],
range,
loc: {
start: {
line: restoredStartLine,
column: startLoc.column + (restoredStartLine === 1 ? offset : 0),
line: point.line + startLoc.line,
column: startLoc.column + (startLoc.line === 1 ? point.column : 0),
},
end: {
line: restoredEndLine,
column: endLoc.column + (restoredEndLine === 1 ? offset : 0),
line: point.line + endLoc.line,
column: endLoc.column + (endLoc.line === 1 ? point.column : 0),
},

@@ -365,3 +379,3 @@ },

for (const normalizedNode of arrayify(this.normalizeJsxNode(node, parent, options))) {
this._nodeToAst(normalizedNode, options);
this._nodeToAst(code, normalizedNode, options);
}

@@ -473,3 +487,3 @@ },

// @internal
_nodeToAst(node, options) {
_nodeToAst(code, node, options) {
if (node.data && node.data.jsxType === 'JSXElementWithHTMLComments') {

@@ -506,7 +520,12 @@ this._services.JSXElementsWithHTMLComments.push(node);

}
const offset = start - program.range[0];
const startPoint = {
line: startLine,
// #279 related
column: getPositionAt(code, start).column,
offset: start,
};
for (const prop of AST_PROPS)
this._ast[prop].push(
// ts doesn't understand the mixed type
...program[prop].map((item) => restoreNodeLocation(item, startLine, offset)));
...program[prop].map((item) => restoreNodeLocation(item, startPoint)));
}

@@ -518,2 +537,2 @@ }

export { AST_PROPS, CLOSE_TAG_REGEX, COMMENT_CONTENT_REGEX, COMMENT_CONTENT_REGEX_GLOBAL, COMMENT_REGEX, DEFAULT_EXTENSIONS, DEFAULT_PARSER_OPTIONS, ES_NODE_TYPES, FALLBACK_PARSERS, JSX_TYPES, LOC_ERROR_PROPERTIES, MARKDOWN_EXTENSIONS, OPEN_CLOSE_TAG_REGEX, OPEN_TAG_REGEX, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, mdProcessor, mdxProcessor, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, restoreNodeLocation, selfClosingTag, traverse };
export { AST_PROPS, CLOSE_TAG_REGEX, COMMENT_CONTENT_REGEX, COMMENT_CONTENT_REGEX_GLOBAL, COMMENT_REGEX, DEFAULT_EXTENSIONS, DEFAULT_PARSER_OPTIONS, ES_NODE_TYPES, FALLBACK_PARSERS, JSX_TYPES, LOC_ERROR_PROPERTIES, MARKDOWN_EXTENSIONS, OPEN_CLOSE_TAG_REGEX, OPEN_TAG_REGEX, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, getPositionAt, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, mdProcessor, mdxProcessor, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, restoreNodeLocation, selfClosingTag, traverse };

@@ -1,1 +0,1 @@

import e from"path";import t from"remark-mdx";import s from"remark-parse";import n from"unified";const o=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],r=["JSXElement","JSXFragment"],i=e=>r.includes(e.type),a=e=>{if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError(`Invalid custom parser for \`eslint-mdx\`: ${e}`);return[e]}const t=[];for(const e of o)try{const s=require(e),n="parseForESLint"in s?s.parseForESLint:s.parse;n&&t.push(n)}catch(e){}return t},c=e=>{const t=e.start.offset,s=e.end.offset;return{range:[t,s],loc:e,start:t,end:s}},l=(e,t)=>"object"==typeof e&&e&&t.every((t=>t in e)),p=(e,t,s)=>{if(e&&"object"==typeof e)for(const n of Object.values(e))p(n,t,s);if(!l(e,["loc","range"]))return e;const{loc:{start:n,end:o},range:r}=e,i=r[0]+s,a=r[1]+s,c=t+n.line,u=t+o.line;return Object.assign(e,{start:i,end:a,range:[i,a],loc:{start:{line:c,column:n.column+(1===c?s:0)},end:{line:u,column:o.column+(1===u?s:0)}}})},u=(...e)=>e.reduce(((e,t)=>(e.push(...Array.isArray(t)?t:null==t?[]:[t]),e)),[]),m=e=>e&&e[0],h=e=>e&&e[e.length-1],f="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",d="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+f+"*\\s*>",x="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",g="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+f+"*\\s*\\/?>",b="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",y="(\x3c!---*)",_="(-*--\x3e)",v="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",E=new RegExp(`^(?:${d})$`),j=new RegExp(`^(?:${x})$`),S=new RegExp(`^(?:${d+"[^<]*"+x})$`),w=new RegExp(`^(?:${g})$`),J=new RegExp(`^(?:${b})$`),N=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),$=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),L=e=>E.test(e.trim()),z=e=>j.test(e.trim()),T=e=>J.test(e.trim()),A=e=>S.test(e.trim()),F=e=>w.test(e.trim());class O{constructor({code:e,enter:t}){this.code=e,this._enter=t}combineLeftJsxNodes(e,t){var s;const n=e[0].position.start,o=Object.assign({},h(e).position.end);return t&&(null===(s=t.position.indent)||void 0===s?void 0:s.length)>0&&(o.offset+=t.position.indent.reduce(((e,t,s)=>e+(s?t+1:0)),0)),{type:"jsx",data:e[0].data,value:this.code.slice(n.offset,o.offset),position:{start:n,end:o}}}combineJsxNodes(e,t){let s=0,n=!1;const o=[],{length:r}=e;return e.reduce(((e,i,a)=>{if("jsx"===i.type){const r=i.value;if(L(r))s++,n=!0,o.push(i);else{if(z(r))s--,o.push(i);else if(T(r)||F(r)||A(r))o.push(i);else{a||(s++,n=!0);try{o.push(...u(B.normalizeJsxNode(i,t)))}catch(e){if(!s){const{start:e}=i.position;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(r)),{lineNumber:e.line,column:e.column,index:e.offset})}o.push(i)}}if(!s){const s=o.findIndex((e=>"string"==typeof e.value&&L(e.value)));-1===s?n?e.push(this.combineLeftJsxNodes(o,t)):e.push(...o):e.push(...o.slice(0,s),this.combineLeftJsxNodes(o.slice(s),t)),o.length=0}}}else s?o.push(i):e.push(i);return a===r-1&&o.length>0&&e.push(this.combineLeftJsxNodes(o,t)),e}),[])}traverse(e,t){if(!e)return;let s=e.children;if(s){const t=e;s=e.children=this.combineJsxNodes(s,t);for(const e of s)this.traverse(e,t)}this._enter(e,t)}}const k=(e,t)=>new O(t).traverse(e),P=n().use(s).freeze(),Z=P().use(t).freeze(),R=["body","comments","tokens"],X=["export","import","jsx"],C=["column","lineNumber"],H=[".mdx"],M=[".md"],W={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},q="<$>".length;class I{constructor(){this._options=W,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}normalizeJsxNode(e,t,s=this._options){const n=e.value;if("jsx"!==e.type||T(n))return e;if(N.exec(n)){const s=[],{position:{start:{line:o,column:r,offset:i}},data:a}=e;Object.assign(e,{data:Object.assign(Object.assign({},a),{jsxType:"JSXElementWithHTMLComments",comments:s,inline:!!t&&"root"!==t.type}),value:n.replace($,((e,t,a,c,l)=>{const p=l+e.length,u=n.slice(0,l).split("\n"),m=n.slice(0,p).split("\n"),f=`{/${"*".repeat(t.length-2)}${a}${"*".repeat(c.length-2)}/}`,d=u.length-1,x=m.length-1;return s.push({fixed:f,loc:{start:{line:o+d,column:h(u).length+(d?0:r-1),offset:i+l},end:{line:o+x,column:h(m).length+(x?0:r-1),offset:i+p}},origin:e}),f}))})}return this._normalizeJsxNodes(e,s)}parse(e,t){return this.parseForESLint(e,t).ast}parseForESLint(t,s){const n=e.extname(s.filePath),o=H.concat(s.extensions||[]).includes(n),r=M.concat(s.markdownExtensions||[]).includes(n);if(!o&&!r)return this._eslintParse(t,s);const i=(o?Z:P).parse(t);return this._ast=Object.assign(Object.assign({},c(i.position)),{type:"Program",sourceType:s.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},o&&k(i,{code:t,enter:(e,t)=>{if(X.includes(e.type))for(const n of u(this.normalizeJsxNode(e,t,s)))this._nodeToAst(n,s)}}),{ast:this._ast,services:this._services}}_eslintParse(e,t){let s,n;this._parsers&&t.parser===this._options.parser||(this._parsers=a(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(const t of this._parsers)try{s=t(e,this._options);break}catch(e){n||(n=e)}if(!s&&n)throw n;return"ast"in s&&s.ast?s:{ast:s}}_normalizeJsxNodes(e,t){const s=e.value;let n;try{n=this._eslintParse(`<$>${s}</$>`,t).ast}catch(t){if(l(t,C)){const{position:{start:s}}=e;throw"index"in t?t.index+=s.offset-q:"pos"in t&&(t.pos+=s.offset-q),t.column=t.lineNumber>1?t.column:t.column+s.column-q,t.lineNumber+=s.line-1,t}return e}const{expression:o}=n.body[0];if(!i(o)||o.children.length<=1)return e;const{position:{start:{line:r,offset:a}},data:c}=e;return o.children.reduce(((e,t)=>{if(!i(t))return e;const{start:n,end:o,loc:{start:l,end:p}={start:{column:n,line:1},end:{column:o,line:1}},range:u=[n,o]}=t,m=r+l.line-1,h=r+p.line-1,f=u[0]-q,d=u[1]-q;return e.push({type:"jsx",data:e.length>0?null:c,value:s.slice(f,d),position:{start:{line:m,column:r===m?l.column-q:l.column,offset:a+f},end:{line:h,column:r===m?p.column-q:p.column,offset:a+d}}}),e}),[])}_nodeToAst(e,t){e.data&&"JSXElementWithHTMLComments"===e.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(e);const s=e.value,{loc:n,start:o,end:r}=c(e.position);if(T(s)){const e=N.exec(s)[2];return void this._ast.comments.push({type:"Block",value:e,loc:n,range:[o,r]})}const i=n.start.line-1;let a;try{a=this._eslintParse(s,t).ast}catch(e){throw l(e,C)&&(e.index+=o,e.column=e.lineNumber>1?e.column:e.column+n.start.column,e.lineNumber+=i),e}const u=o-a.range[0];for(const e of R)this._ast[e].push(...a[e].map((e=>p(e,i,u))))}}const B=new I,{parse:D,parseForESLint:U}=B;export{R as AST_PROPS,j as CLOSE_TAG_REGEX,N as COMMENT_CONTENT_REGEX,$ as COMMENT_CONTENT_REGEX_GLOBAL,J as COMMENT_REGEX,H as DEFAULT_EXTENSIONS,W as DEFAULT_PARSER_OPTIONS,X as ES_NODE_TYPES,o as FALLBACK_PARSERS,r as JSX_TYPES,C as LOC_ERROR_PROPERTIES,M as MARKDOWN_EXTENSIONS,S as OPEN_CLOSE_TAG_REGEX,E as OPEN_TAG_REGEX,I as Parser,w as SELF_CLOSING_TAG_REGEX,O as Traverse,u as arrayify,x as closeTag,b as comment,_ as commentClose,v as commentContent,y as commentOpen,m as first,l as hasProperties,z as isCloseTag,T as isComment,i as isJsxNode,A as isOpenCloseTag,L as isOpenTag,F as isSelfClosingTag,h as last,P as mdProcessor,Z as mdxProcessor,a as normalizeParser,c as normalizePosition,d as openTag,D as parse,U as parseForESLint,B as parser,p as restoreNodeLocation,g as selfClosingTag,k as traverse};
import e from"path";import t from"remark-mdx";import s from"remark-parse";import n from"unified";const o=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],r=["JSXElement","JSXFragment"],i=e=>r.includes(e.type),l=e=>{if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError(`Invalid custom parser for \`eslint-mdx\`: ${e}`);return[e]}const t=[];for(const e of o)try{const s=require(e),n="parseForESLint"in s?s.parseForESLint:s.parse;n&&t.push(n)}catch(e){}return t},c=e=>{const t=e.start.offset,s=e.end.offset;return{range:[t,s],loc:e,start:t,end:s}},a=(e,t)=>"object"==typeof e&&e&&t.every((t=>t in e)),u=(e,t)=>{let s=0;const n=e.split("\n");for(let e=0;e<n.length;e++){const o=e+1,r=s+n[e].length;if(r>=t)return{line:o,column:t-s};s=r+1}},p=(e,t)=>{if(e&&"object"==typeof e)for(const s of Object.values(e))p(s,t);if(!a(e,["loc","range"]))return e;let{loc:{start:s,end:n},range:[o,r]}=e;const i=[o+=t.offset,r+=t.offset];return Object.assign(e,{start:o,end:r,range:i,loc:{start:{line:t.line+s.line,column:s.column+(1===s.line?t.column:0)},end:{line:t.line+n.line,column:n.column+(1===n.line?t.column:0)}}})},m=(...e)=>e.reduce(((e,t)=>(e.push(...Array.isArray(t)?t:null==t?[]:[t]),e)),[]),h=e=>e&&e[0],f=e=>e&&e[e.length-1],d="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",x="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+d+"*\\s*>",g="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",b="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+d+"*\\s*\\/?>",y="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",_="(\x3c!---*)",v="(-*--\x3e)",E="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",j=new RegExp(`^(?:${x})$`),S=new RegExp(`^(?:${g})$`),w=new RegExp(`^(?:${x+"[^<]*"+g})$`),J=new RegExp(`^(?:${b})$`),N=new RegExp(`^(?:${y})$`),$=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),L=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),z=e=>j.test(e.trim()),T=e=>S.test(e.trim()),A=e=>N.test(e.trim()),F=e=>w.test(e.trim()),O=e=>J.test(e.trim());class k{constructor({code:e,enter:t}){this.code=e,this._enter=t}combineLeftJsxNodes(e,t){var s;const n=e[0].position.start,o=Object.assign({},f(e).position.end);return t&&(null===(s=t.position.indent)||void 0===s?void 0:s.length)>0&&(o.offset+=t.position.indent.reduce(((e,t,s)=>e+(s?t+1:0)),0)),{type:"jsx",data:e[0].data,value:this.code.slice(n.offset,o.offset),position:{start:n,end:o}}}combineJsxNodes(e,t){let s=0,n=!1;const o=[],{length:r}=e;return e.reduce(((e,i,l)=>{if("jsx"===i.type){const r=i.value;if(z(r))s++,n=!0,o.push(i);else{if(T(r))s--,o.push(i);else if(A(r)||O(r)||F(r))o.push(i);else{l||(s++,n=!0);try{o.push(...m(D.normalizeJsxNode(i,t)))}catch(e){if(!s){const{start:e}=i.position;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(r)),{lineNumber:e.line,column:e.column,index:e.offset})}o.push(i)}}if(!s){const s=o.findIndex((e=>"string"==typeof e.value&&z(e.value)));-1===s?n?e.push(this.combineLeftJsxNodes(o,t)):e.push(...o):e.push(...o.slice(0,s),this.combineLeftJsxNodes(o.slice(s),t)),o.length=0}}}else s?o.push(i):e.push(i);return l===r-1&&o.length>0&&e.push(this.combineLeftJsxNodes(o,t)),e}),[])}traverse(e,t){if(!e)return;let s=e.children;if(s){const t=e;s=e.children=this.combineJsxNodes(s,t);for(const e of s)this.traverse(e,t)}this._enter(e,t)}}const P=(e,t)=>new k(t).traverse(e),Z=n().use(s).freeze(),R=Z().use(t).freeze(),X=["body","comments","tokens"],C=["export","import","jsx"],H=["column","lineNumber"],M=[".mdx"],W=[".md"],q={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},I="<$>".length;class B{constructor(){this._options=q,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}normalizeJsxNode(e,t,s=this._options){const n=e.value;if("jsx"!==e.type||A(n))return e;if($.exec(n)){const s=[],{position:{start:{line:o,column:r,offset:i}},data:l}=e;Object.assign(e,{data:Object.assign(Object.assign({},l),{jsxType:"JSXElementWithHTMLComments",comments:s,inline:!!t&&"root"!==t.type}),value:n.replace(L,((e,t,l,c,a)=>{const u=a+e.length,p=n.slice(0,a).split("\n"),m=n.slice(0,u).split("\n"),h=`{/${"*".repeat(t.length-2)}${l}${"*".repeat(c.length-2)}/}`,d=p.length-1,x=m.length-1;return s.push({fixed:h,loc:{start:{line:o+d,column:f(p).length+(d?0:r-1),offset:i+a},end:{line:o+x,column:f(m).length+(x?0:r-1),offset:i+u}},origin:e}),h}))})}return this._normalizeJsxNodes(e,s)}parse(e,t){return this.parseForESLint(e,t).ast}parseForESLint(t,s){const n=e.extname(s.filePath),o=M.concat(s.extensions||[]).includes(n),r=W.concat(s.markdownExtensions||[]).includes(n);if(!o&&!r)return this._eslintParse(t,s);const i=(o?R:Z).parse(t);return this._ast=Object.assign(Object.assign({},c(i.position)),{type:"Program",sourceType:s.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},o&&P(i,{code:t,enter:(e,n)=>{if(C.includes(e.type))for(const o of m(this.normalizeJsxNode(e,n,s)))this._nodeToAst(t,o,s)}}),{ast:this._ast,services:this._services}}_eslintParse(e,t){let s,n;this._parsers&&t.parser===this._options.parser||(this._parsers=l(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(const t of this._parsers)try{s=t(e,this._options);break}catch(e){n||(n=e)}if(!s&&n)throw n;return"ast"in s&&s.ast?s:{ast:s}}_normalizeJsxNodes(e,t){const s=e.value;let n;try{n=this._eslintParse(`<$>${s}</$>`,t).ast}catch(t){if(a(t,H)){const{position:{start:s}}=e;throw"index"in t?t.index+=s.offset-I:"pos"in t&&(t.pos+=s.offset-I),t.column=t.lineNumber>1?t.column:t.column+s.column-I,t.lineNumber+=s.line-1,t}return e}const{expression:o}=n.body[0];if(!i(o)||o.children.length<=1)return e;const{position:{start:{line:r,offset:l}},data:c}=e;return o.children.reduce(((e,t)=>{if(!i(t))return e;const{start:n,end:o,loc:{start:a,end:u}={start:{column:n,line:1},end:{column:o,line:1}},range:p=[n,o]}=t,m=r+a.line-1,h=r+u.line-1,f=p[0]-I,d=p[1]-I;return e.push({type:"jsx",data:e.length>0?null:c,value:s.slice(f,d),position:{start:{line:m,column:r===m?a.column-I:a.column,offset:l+f},end:{line:h,column:r===m?u.column-I:u.column,offset:l+d}}}),e}),[])}_nodeToAst(e,t,s){t.data&&"JSXElementWithHTMLComments"===t.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(t);const n=t.value,{loc:o,start:r,end:i}=c(t.position);if(A(n)){const e=$.exec(n)[2];return void this._ast.comments.push({type:"Block",value:e,loc:o,range:[r,i]})}const l=o.start.line-1;let m;try{m=this._eslintParse(n,s).ast}catch(e){throw a(e,H)&&(e.index+=r,e.column=e.lineNumber>1?e.column:e.column+o.start.column,e.lineNumber+=l),e}const h={line:l,column:u(e,r).column,offset:r};for(const e of X)this._ast[e].push(...m[e].map((e=>p(e,h))))}}const D=new B,{parse:U,parseForESLint:V}=D;export{X as AST_PROPS,S as CLOSE_TAG_REGEX,$ as COMMENT_CONTENT_REGEX,L as COMMENT_CONTENT_REGEX_GLOBAL,N as COMMENT_REGEX,M as DEFAULT_EXTENSIONS,q as DEFAULT_PARSER_OPTIONS,C as ES_NODE_TYPES,o as FALLBACK_PARSERS,r as JSX_TYPES,H as LOC_ERROR_PROPERTIES,W as MARKDOWN_EXTENSIONS,w as OPEN_CLOSE_TAG_REGEX,j as OPEN_TAG_REGEX,B as Parser,J as SELF_CLOSING_TAG_REGEX,k as Traverse,m as arrayify,g as closeTag,y as comment,v as commentClose,E as commentContent,_ as commentOpen,h as first,u as getPositionAt,a as hasProperties,T as isCloseTag,A as isComment,i as isJsxNode,F as isOpenCloseTag,z as isOpenTag,O as isSelfClosingTag,f as last,Z as mdProcessor,R as mdxProcessor,l as normalizeParser,c as normalizePosition,x as openTag,U as parse,V as parseForESLint,D as parser,p as restoreNodeLocation,b as selfClosingTag,P as traverse};

@@ -72,7 +72,24 @@ import { __assign, __spreadArray } from 'tslib';

};
var restoreNodeLocation = function (node, startLine, offset) {
// fix #292
var getPositionAt = function (code, offset) {
var currOffset = 0;
var lines = code.split('\n');
// eslint-disable-next-line unicorn/no-for-loop
for (var index = 0; index < lines.length; index++) {
var line = index + 1;
var nextOffset = currOffset + lines[index].length;
if (nextOffset >= offset) {
return {
line: line,
column: offset - currOffset,
};
}
currOffset = nextOffset + 1; // add a line break `\n` offset
}
};
var restoreNodeLocation = function (node, point) {
if (node && typeof node === 'object') {
for (var _i = 0, _a = Object.values(node); _i < _a.length; _i++) {
var value = _a[_i];
restoreNodeLocation(value, startLine, offset);
restoreNodeLocation(value, point);
}

@@ -83,19 +100,16 @@ }

}
var _b = node.loc, startLoc = _b.start, endLoc = _b.end, range = node.range;
var start = range[0] + offset;
var end = range[1] + offset;
var restoredStartLine = startLine + startLoc.line;
var restoredEndLine = startLine + endLoc.line;
var _b = node.loc, startLoc = _b.start, endLoc = _b.end, _c = node.range, start = _c[0], end = _c[1];
var range = [(start += point.offset), (end += point.offset)];
return Object.assign(node, {
start: start,
end: end,
range: [start, end],
range: range,
loc: {
start: {
line: restoredStartLine,
column: startLoc.column + (restoredStartLine === 1 ? offset : 0),
line: point.line + startLoc.line,
column: startLoc.column + (startLoc.line === 1 ? point.column : 0),
},
end: {
line: restoredEndLine,
column: endLoc.column + (restoredEndLine === 1 ? offset : 0),
line: point.line + endLoc.line,
column: endLoc.column + (endLoc.line === 1 ? point.column : 0),
},

@@ -393,3 +407,3 @@ },

var normalizedNode = _a[_i];
_this._nodeToAst(normalizedNode, options);
_this._nodeToAst(code, normalizedNode, options);
}

@@ -502,3 +516,3 @@ },

// @internal
Parser.prototype._nodeToAst = function (node, options) {
Parser.prototype._nodeToAst = function (code, node, options) {
var _a;

@@ -536,7 +550,12 @@ if (node.data && node.data.jsxType === 'JSXElementWithHTMLComments') {

}
var offset = start - program.range[0];
var startPoint = {
line: startLine,
// #279 related
column: getPositionAt(code, start).column,
offset: start,
};
for (var _i = 0, AST_PROPS_1 = AST_PROPS; _i < AST_PROPS_1.length; _i++) {
var prop = AST_PROPS_1[_i];
(_a = this._ast[prop]).push.apply(_a, program[prop].map(function (item) {
return restoreNodeLocation(item, startLine, offset);
return restoreNodeLocation(item, startPoint);
}));

@@ -551,2 +570,2 @@ }

export { AST_PROPS, CLOSE_TAG_REGEX, COMMENT_CONTENT_REGEX, COMMENT_CONTENT_REGEX_GLOBAL, COMMENT_REGEX, DEFAULT_EXTENSIONS, DEFAULT_PARSER_OPTIONS, ES_NODE_TYPES, FALLBACK_PARSERS, JSX_TYPES, LOC_ERROR_PROPERTIES, MARKDOWN_EXTENSIONS, OPEN_CLOSE_TAG_REGEX, OPEN_TAG_REGEX, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, mdProcessor, mdxProcessor, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, restoreNodeLocation, selfClosingTag, traverse };
export { AST_PROPS, CLOSE_TAG_REGEX, COMMENT_CONTENT_REGEX, COMMENT_CONTENT_REGEX_GLOBAL, COMMENT_REGEX, DEFAULT_EXTENSIONS, DEFAULT_PARSER_OPTIONS, ES_NODE_TYPES, FALLBACK_PARSERS, JSX_TYPES, LOC_ERROR_PROPERTIES, MARKDOWN_EXTENSIONS, OPEN_CLOSE_TAG_REGEX, OPEN_TAG_REGEX, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, getPositionAt, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, mdProcessor, mdxProcessor, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, restoreNodeLocation, selfClosingTag, traverse };

@@ -1,1 +0,1 @@

import{__assign as e,__spreadArray as t}from"tslib";import n from"path";import r from"remark-mdx";import s from"remark-parse";import o from"unified";var i=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],a=["JSXElement","JSXFragment"],u=function(e){return a.includes(e.type)},l=function(e){if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError("Invalid custom parser for `eslint-mdx`: "+e);return[e]}for(var t=[],n=0,r=i;n<r.length;n++){var s=r[n];try{var o=require(s),a="parseForESLint"in o?o.parseForESLint:o.parse;a&&t.push(a)}catch(e){}}return t},c=function(e){var t=e.start.offset,n=e.end.offset;return{range:[t,n],loc:e,start:t,end:n}},p=function(e,t){return"object"==typeof e&&e&&t.every((function(t){return t in e}))},f=function(e,t,n){if(e&&"object"==typeof e)for(var r=0,s=Object.values(e);r<s.length;r++){var o=s[r];f(o,t,n)}if(!p(e,["loc","range"]))return e;var i=e.loc,a=i.start,u=i.end,l=e.range,c=l[0]+n,m=l[1]+n,h=t+a.line,d=t+u.line;return Object.assign(e,{start:c,end:m,range:[c,m],loc:{start:{line:h,column:a.column+(1===h?n:0)},end:{line:d,column:u.column+(1===d?n:0)}}})},m=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e.reduce((function(e,t){return e.push.apply(e,Array.isArray(t)?t:null==t?[]:[t]),e}),[])},h=function(e){return e&&e[0]},d=function(e){return e&&e[e.length-1]},v="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",x="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+v+"*\\s*>",g="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",y="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+v+"*\\s*\\/?>",_="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",b="(\x3c!---*)",E="(-*--\x3e)",S="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",w=new RegExp("^(?:"+x+")$"),J=new RegExp("^(?:"+g+")$"),N=new RegExp("^(?:"+x+"[^<]*"+g+")$"),j=new RegExp("^(?:"+y+")$"),L=new RegExp("^(?:"+_+")$"),z=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),T=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),A=function(e){return w.test(e.trim())},F=function(e){return J.test(e.trim())},k=function(e){return L.test(e.trim())},P=function(e){return N.test(e.trim())},Z=function(e){return j.test(e.trim())},$=function(){function n(e){var t=e.code,n=e.enter;this.code=t,this._enter=n}return n.prototype.combineLeftJsxNodes=function(t,n){var r,s=t[0].position.start,o=e({},d(t).position.end);return n&&(null===(r=n.position.indent)||void 0===r?void 0:r.length)>0&&(o.offset+=n.position.indent.reduce((function(e,t,n){return e+(n?t+1:0)}),0)),{type:"jsx",data:t[0].data,value:this.code.slice(s.offset,o.offset),position:{start:s,end:o}}},n.prototype.combineJsxNodes=function(e,n){var r=this,s=0,o=!1,i=[],a=e.length;return e.reduce((function(e,u,l){if("jsx"===u.type){var c=u.value;if(A(c))s++,o=!0,i.push(u);else{if(F(c))s--,i.push(u);else if(k(c)||Z(c)||P(c))i.push(u);else{l||(s++,o=!0);try{i.push.apply(i,m(U.normalizeJsxNode(u,n)))}catch(e){if(!s){var p=u.position.start;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(c)),{lineNumber:p.line,column:p.column,index:p.offset})}i.push(u)}}if(!s){var f=i.findIndex((function(e){return"string"==typeof e.value&&A(e.value)}));-1===f?o?e.push(r.combineLeftJsxNodes(i,n)):e.push.apply(e,i):e.push.apply(e,t(t([],i.slice(0,f)),[r.combineLeftJsxNodes(i.slice(f),n)])),i.length=0}}}else s?i.push(u):e.push(u);return l===a-1&&i.length>0&&e.push(r.combineLeftJsxNodes(i,n)),e}),[])},n.prototype.traverse=function(e,t){if(e){var n=e.children;if(n)for(var r=e,s=0,o=n=e.children=this.combineJsxNodes(n,r);s<o.length;s++){var i=o[s];this.traverse(i,r)}this._enter(e,t)}},n}(),R=function(e,t){return new $(t).traverse(e)},O=o().use(s).freeze(),X=O().use(r).freeze(),C=["body","comments","tokens"],H=["export","import","jsx"],M=["column","lineNumber"],W=[".mdx"],q=[".md"],I={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},B="<$>".length,D=function(){function t(){this._options=I,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}return t.prototype.normalizeJsxNode=function(t,n,r){void 0===r&&(r=this._options);var s=t.value;if("jsx"!==t.type||k(s))return t;if(z.exec(s)){var o=[],i=t.position.start,a=i.line,u=i.column,l=i.offset,c=t.data;Object.assign(t,{data:e(e({},c),{jsxType:"JSXElementWithHTMLComments",comments:o,inline:!!n&&"root"!==n.type}),value:s.replace(T,(function(e,t,n,r,i){var c=i+e.length,p=s.slice(0,i).split("\n"),f=s.slice(0,c).split("\n"),m="{/"+"*".repeat(t.length-2)+n+"*".repeat(r.length-2)+"/}",h=p.length-1,v=f.length-1;return o.push({fixed:m,loc:{start:{line:a+h,column:d(p).length+(h?0:u-1),offset:l+i},end:{line:a+v,column:d(f).length+(v?0:u-1),offset:l+c}},origin:e}),m}))})}return this._normalizeJsxNodes(t,r)},t.prototype.parse=function(e,t){return this.parseForESLint(e,t).ast},t.prototype.parseForESLint=function(t,r){var s=this,o=n.extname(r.filePath),i=W.concat(r.extensions||[]).includes(o),a=q.concat(r.markdownExtensions||[]).includes(o);if(!i&&!a)return this._eslintParse(t,r);var u=(i?X:O).parse(t);return this._ast=e(e({},c(u.position)),{type:"Program",sourceType:r.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},i&&R(u,{code:t,enter:function(e,t){if(H.includes(e.type))for(var n=0,o=m(s.normalizeJsxNode(e,t,r));n<o.length;n++){var i=o[n];s._nodeToAst(i,r)}}}),{ast:this._ast,services:this._services}},t.prototype._eslintParse=function(e,t){var n,r;this._parsers&&t.parser===this._options.parser||(this._parsers=l(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(var s=0,o=this._parsers;s<o.length;s++){var i=o[s];try{n=i(e,this._options);break}catch(e){r||(r=e)}}if(!n&&r)throw r;return"ast"in n&&n.ast?n:{ast:n}},t.prototype._normalizeJsxNodes=function(e,t){var n,r=e.value;try{n=this._eslintParse("<$>"+r+"</$>",t).ast}catch(t){if(p(t,M)){var s=e.position.start;throw"index"in t?t.index+=s.offset-B:"pos"in t&&(t.pos+=s.offset-B),t.column=t.lineNumber>1?t.column:t.column+s.column-B,t.lineNumber+=s.line-1,t}return e}var o=n.body[0].expression;if(!u(o)||o.children.length<=1)return e;var i=e.position.start,a=i.line,l=i.offset,c=e.data;return o.children.reduce((function(e,t){if(!u(t))return e;var n=t.start,s=t.end,o=t.loc,i=void 0===o?{start:{column:n,line:1},end:{column:s,line:1}}:o,p=i.start,f=i.end,m=t.range,h=void 0===m?[n,s]:m,d=a+p.line-1,v=a+f.line-1,x=h[0]-B,g=h[1]-B;return e.push({type:"jsx",data:e.length>0?null:c,value:r.slice(x,g),position:{start:{line:d,column:a===d?p.column-B:p.column,offset:l+x},end:{line:v,column:a===d?f.column-B:f.column,offset:l+g}}}),e}),[])},t.prototype._nodeToAst=function(e,t){var n;e.data&&"JSXElementWithHTMLComments"===e.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(e);var r=e.value,s=c(e.position),o=s.loc,i=s.start,a=s.end;if(k(r)){var u=z.exec(r)[2];this._ast.comments.push({type:"Block",value:u,loc:o,range:[i,a]})}else{var l,m=o.start.line-1;try{l=this._eslintParse(r,t).ast}catch(e){throw p(e,M)&&(e.index+=i,e.column=e.lineNumber>1?e.column:e.column+o.start.column,e.lineNumber+=m),e}for(var h=i-l.range[0],d=0,v=C;d<v.length;d++){var x=v[d];(n=this._ast[x]).push.apply(n,l[x].map((function(e){return f(e,m,h)})))}}},t}(),U=new D,V=U.parse,Y=U.parseForESLint;export{C as AST_PROPS,J as CLOSE_TAG_REGEX,z as COMMENT_CONTENT_REGEX,T as COMMENT_CONTENT_REGEX_GLOBAL,L as COMMENT_REGEX,W as DEFAULT_EXTENSIONS,I as DEFAULT_PARSER_OPTIONS,H as ES_NODE_TYPES,i as FALLBACK_PARSERS,a as JSX_TYPES,M as LOC_ERROR_PROPERTIES,q as MARKDOWN_EXTENSIONS,N as OPEN_CLOSE_TAG_REGEX,w as OPEN_TAG_REGEX,D as Parser,j as SELF_CLOSING_TAG_REGEX,$ as Traverse,m as arrayify,g as closeTag,_ as comment,E as commentClose,S as commentContent,b as commentOpen,h as first,p as hasProperties,F as isCloseTag,k as isComment,u as isJsxNode,P as isOpenCloseTag,A as isOpenTag,Z as isSelfClosingTag,d as last,O as mdProcessor,X as mdxProcessor,l as normalizeParser,c as normalizePosition,x as openTag,V as parse,Y as parseForESLint,U as parser,f as restoreNodeLocation,y as selfClosingTag,R as traverse};
import{__assign as e,__spreadArray as t}from"tslib";import n from"path";import r from"remark-mdx";import s from"remark-parse";import o from"unified";var i=["@typescript-eslint/parser","@babel/eslint-parser","babel-eslint","espree"],a=["JSXElement","JSXFragment"],l=function(e){return a.includes(e.type)},u=function(e){if(e){if("string"==typeof e&&(e=require(e)),"object"==typeof e&&(e="parseForESLint"in e&&e.parseForESLint||"parse"in e&&e.parse),"function"!=typeof e)throw new TypeError("Invalid custom parser for `eslint-mdx`: "+e);return[e]}for(var t=[],n=0,r=i;n<r.length;n++){var s=r[n];try{var o=require(s),a="parseForESLint"in o?o.parseForESLint:o.parse;a&&t.push(a)}catch(e){}}return t},c=function(e){var t=e.start.offset,n=e.end.offset;return{range:[t,n],loc:e,start:t,end:n}},p=function(e,t){return"object"==typeof e&&e&&t.every((function(t){return t in e}))},f=function(e,t){for(var n=0,r=e.split("\n"),s=0;s<r.length;s++){var o=s+1,i=n+r[s].length;if(i>=t)return{line:o,column:t-n};n=i+1}},m=function(e,t){if(e&&"object"==typeof e)for(var n=0,r=Object.values(e);n<r.length;n++){var s=r[n];m(s,t)}if(!p(e,["loc","range"]))return e;var o=e.loc,i=o.start,a=o.end,l=e.range,u=l[0],c=l[1],f=[u+=t.offset,c+=t.offset];return Object.assign(e,{start:u,end:c,range:f,loc:{start:{line:t.line+i.line,column:i.column+(1===i.line?t.column:0)},end:{line:t.line+a.line,column:a.column+(1===a.line?t.column:0)}}})},h=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return e.reduce((function(e,t){return e.push.apply(e,Array.isArray(t)?t:null==t?[]:[t]),e}),[])},d=function(e){return e&&e[0]},v=function(e){return e&&e[e.length-1]},x="(?:\\s+[a-zA-Z_:][a-zA-Z0-9:._-]*(?:\\s*=\\s*"+("(?:[^\"'=<>`\\u0000-\\u0020]+|'[^']*'|\"[^\"]*\"|"+"{.*}".replace(".","[\0-￿]")+")")+")?)",g="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+x+"*\\s*>",y="<\\s*\\/[A-Za-z]*[A-Za-z0-9\\.\\-]*\\s*>",_="<[A-Za-z]*[A-Za-z0-9\\.\\-]*"+x+"*\\s*\\/?>",b="\x3c!----\x3e|\x3c!--(?:-?[^>-])(?:-?[^-])*--\x3e",E="(\x3c!---*)",S="(-*--\x3e)",w="(\x3c!---*)([\\s\\S]*?)(-*--\x3e)",J=new RegExp("^(?:"+g+")$"),N=new RegExp("^(?:"+y+")$"),j=new RegExp("^(?:"+g+"[^<]*"+y+")$"),L=new RegExp("^(?:"+_+")$"),z=new RegExp("^(?:"+b+")$"),T=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)"),A=new RegExp("(\x3c!---*)([\\s\\S]*?)(-*--\x3e)","g"),F=function(e){return J.test(e.trim())},k=function(e){return N.test(e.trim())},P=function(e){return z.test(e.trim())},Z=function(e){return j.test(e.trim())},$=function(e){return L.test(e.trim())},R=function(){function n(e){var t=e.code,n=e.enter;this.code=t,this._enter=n}return n.prototype.combineLeftJsxNodes=function(t,n){var r,s=t[0].position.start,o=e({},v(t).position.end);return n&&(null===(r=n.position.indent)||void 0===r?void 0:r.length)>0&&(o.offset+=n.position.indent.reduce((function(e,t,n){return e+(n?t+1:0)}),0)),{type:"jsx",data:t[0].data,value:this.code.slice(s.offset,o.offset),position:{start:s,end:o}}},n.prototype.combineJsxNodes=function(e,n){var r=this,s=0,o=!1,i=[],a=e.length;return e.reduce((function(e,l,u){if("jsx"===l.type){var c=l.value;if(F(c))s++,o=!0,i.push(l);else{if(k(c))s--,i.push(l);else if(P(c)||$(c)||Z(c))i.push(l);else{u||(s++,o=!0);try{i.push.apply(i,h(V.normalizeJsxNode(l,n)))}catch(e){if(!s){var p=l.position.start;throw Object.assign(new SyntaxError("unknown jsx node: "+JSON.stringify(c)),{lineNumber:p.line,column:p.column,index:p.offset})}i.push(l)}}if(!s){var f=i.findIndex((function(e){return"string"==typeof e.value&&F(e.value)}));-1===f?o?e.push(r.combineLeftJsxNodes(i,n)):e.push.apply(e,i):e.push.apply(e,t(t([],i.slice(0,f)),[r.combineLeftJsxNodes(i.slice(f),n)])),i.length=0}}}else s?i.push(l):e.push(l);return u===a-1&&i.length>0&&e.push(r.combineLeftJsxNodes(i,n)),e}),[])},n.prototype.traverse=function(e,t){if(e){var n=e.children;if(n)for(var r=e,s=0,o=n=e.children=this.combineJsxNodes(n,r);s<o.length;s++){var i=o[s];this.traverse(i,r)}this._enter(e,t)}},n}(),O=function(e,t){return new R(t).traverse(e)},X=o().use(s).freeze(),C=X().use(r).freeze(),H=["body","comments","tokens"],M=["export","import","jsx"],W=["column","lineNumber"],q=[".mdx"],I=[".md"],B={comment:!0,ecmaFeatures:{jsx:!0},ecmaVersion:(new Date).getUTCFullYear(),sourceType:"module",tokens:!0,filePath:"__placeholder__.mdx",loc:!0,range:!0},D="<$>".length,U=function(){function t(){this._options=B,this.parse=this.parse.bind(this),this.parseForESLint=this.parseForESLint.bind(this)}return t.prototype.normalizeJsxNode=function(t,n,r){void 0===r&&(r=this._options);var s=t.value;if("jsx"!==t.type||P(s))return t;if(T.exec(s)){var o=[],i=t.position.start,a=i.line,l=i.column,u=i.offset,c=t.data;Object.assign(t,{data:e(e({},c),{jsxType:"JSXElementWithHTMLComments",comments:o,inline:!!n&&"root"!==n.type}),value:s.replace(A,(function(e,t,n,r,i){var c=i+e.length,p=s.slice(0,i).split("\n"),f=s.slice(0,c).split("\n"),m="{/"+"*".repeat(t.length-2)+n+"*".repeat(r.length-2)+"/}",h=p.length-1,d=f.length-1;return o.push({fixed:m,loc:{start:{line:a+h,column:v(p).length+(h?0:l-1),offset:u+i},end:{line:a+d,column:v(f).length+(d?0:l-1),offset:u+c}},origin:e}),m}))})}return this._normalizeJsxNodes(t,r)},t.prototype.parse=function(e,t){return this.parseForESLint(e,t).ast},t.prototype.parseForESLint=function(t,r){var s=this,o=n.extname(r.filePath),i=q.concat(r.extensions||[]).includes(o),a=I.concat(r.markdownExtensions||[]).includes(o);if(!i&&!a)return this._eslintParse(t,r);var l=(i?C:X).parse(t);return this._ast=e(e({},c(l.position)),{type:"Program",sourceType:r.sourceType||"module",body:[],comments:[],tokens:[]}),this._services={JSXElementsWithHTMLComments:[]},i&&O(l,{code:t,enter:function(e,n){if(M.includes(e.type))for(var o=0,i=h(s.normalizeJsxNode(e,n,r));o<i.length;o++){var a=i[o];s._nodeToAst(t,a,r)}}}),{ast:this._ast,services:this._services}},t.prototype._eslintParse=function(e,t){var n,r;this._parsers&&t.parser===this._options.parser||(this._parsers=u(t.parser)),t.filePath&&this._options!==t&&Object.assign(this._options,t);for(var s=0,o=this._parsers;s<o.length;s++){var i=o[s];try{n=i(e,this._options);break}catch(e){r||(r=e)}}if(!n&&r)throw r;return"ast"in n&&n.ast?n:{ast:n}},t.prototype._normalizeJsxNodes=function(e,t){var n,r=e.value;try{n=this._eslintParse("<$>"+r+"</$>",t).ast}catch(t){if(p(t,W)){var s=e.position.start;throw"index"in t?t.index+=s.offset-D:"pos"in t&&(t.pos+=s.offset-D),t.column=t.lineNumber>1?t.column:t.column+s.column-D,t.lineNumber+=s.line-1,t}return e}var o=n.body[0].expression;if(!l(o)||o.children.length<=1)return e;var i=e.position.start,a=i.line,u=i.offset,c=e.data;return o.children.reduce((function(e,t){if(!l(t))return e;var n=t.start,s=t.end,o=t.loc,i=void 0===o?{start:{column:n,line:1},end:{column:s,line:1}}:o,p=i.start,f=i.end,m=t.range,h=void 0===m?[n,s]:m,d=a+p.line-1,v=a+f.line-1,x=h[0]-D,g=h[1]-D;return e.push({type:"jsx",data:e.length>0?null:c,value:r.slice(x,g),position:{start:{line:d,column:a===d?p.column-D:p.column,offset:u+x},end:{line:v,column:a===d?f.column-D:f.column,offset:u+g}}}),e}),[])},t.prototype._nodeToAst=function(e,t,n){var r;t.data&&"JSXElementWithHTMLComments"===t.data.jsxType&&this._services.JSXElementsWithHTMLComments.push(t);var s=t.value,o=c(t.position),i=o.loc,a=o.start,l=o.end;if(P(s)){var u=T.exec(s)[2];this._ast.comments.push({type:"Block",value:u,loc:i,range:[a,l]})}else{var h,d=i.start.line-1;try{h=this._eslintParse(s,n).ast}catch(e){throw p(e,W)&&(e.index+=a,e.column=e.lineNumber>1?e.column:e.column+i.start.column,e.lineNumber+=d),e}for(var v={line:d,column:f(e,a).column,offset:a},x=0,g=H;x<g.length;x++){var y=g[x];(r=this._ast[y]).push.apply(r,h[y].map((function(e){return m(e,v)})))}}},t}(),V=new U,Y=V.parse,G=V.parseForESLint;export{H as AST_PROPS,N as CLOSE_TAG_REGEX,T as COMMENT_CONTENT_REGEX,A as COMMENT_CONTENT_REGEX_GLOBAL,z as COMMENT_REGEX,q as DEFAULT_EXTENSIONS,B as DEFAULT_PARSER_OPTIONS,M as ES_NODE_TYPES,i as FALLBACK_PARSERS,a as JSX_TYPES,W as LOC_ERROR_PROPERTIES,I as MARKDOWN_EXTENSIONS,j as OPEN_CLOSE_TAG_REGEX,J as OPEN_TAG_REGEX,U as Parser,L as SELF_CLOSING_TAG_REGEX,R as Traverse,h as arrayify,y as closeTag,b as comment,S as commentClose,w as commentContent,E as commentOpen,d as first,f as getPositionAt,p as hasProperties,k as isCloseTag,P as isComment,l as isJsxNode,Z as isOpenCloseTag,F as isOpenTag,$ as isSelfClosingTag,v as last,X as mdProcessor,C as mdxProcessor,u as normalizeParser,c as normalizePosition,g as openTag,Y as parse,G as parseForESLint,V as parser,m as restoreNodeLocation,_ as selfClosingTag,O as traverse};
/// <reference path="../typings.d.ts" />
import type { SourceLocation } from 'estree';
import type { Position } from 'unist';
import type { Position as ESPosition, SourceLocation } from 'estree';
import type { Point, Position } from 'unist';
import type { Arrayable, JsxNode, ParserFn, ParserOptions } from './types';

@@ -20,5 +20,6 @@ export declare const FALLBACK_PARSERS: readonly ["@typescript-eslint/parser", "@babel/eslint-parser", "babel-eslint", "espree"];

export declare const hasProperties: <T, P extends keyof T = keyof T>(obj: unknown, properties: Arrayable<P>) => obj is T;
export declare const restoreNodeLocation: <T>(node: T, startLine: number, offset: number) => T;
export declare const getPositionAt: (code: string, offset: number) => ESPosition;
export declare const restoreNodeLocation: <T>(node: T, point: Point) => T;
export declare const arrayify: <T, R = T extends (infer S)[] ? S : T>(...args: T[]) => R[];
export declare const first: <T>(items: T[] | readonly T[]) => T;
export declare const last: <T>(items: T[] | readonly T[]) => T;

@@ -61,6 +61,23 @@ /// <reference path="../typings.d.ts" />

properties.every(property => property in obj);
export const restoreNodeLocation = (node, startLine, offset) => {
// fix #292
export const getPositionAt = (code, offset) => {
let currOffset = 0;
const lines = code.split('\n');
// eslint-disable-next-line unicorn/no-for-loop
for (let index = 0; index < lines.length; index++) {
const line = index + 1;
const nextOffset = currOffset + lines[index].length;
if (nextOffset >= offset) {
return {
line,
column: offset - currOffset,
};
}
currOffset = nextOffset + 1; // add a line break `\n` offset
}
};
export const restoreNodeLocation = (node, point) => {
if (node && typeof node === 'object') {
for (const value of Object.values(node)) {
restoreNodeLocation(value, startLine, offset);
restoreNodeLocation(value, point);
}

@@ -71,19 +88,16 @@ }

}
const { loc: { start: startLoc, end: endLoc }, range, } = node;
const start = range[0] + offset;
const end = range[1] + offset;
const restoredStartLine = startLine + startLoc.line;
const restoredEndLine = startLine + endLoc.line;
let { loc: { start: startLoc, end: endLoc }, range: [start, end], } = node;
const range = [(start += point.offset), (end += point.offset)];
return Object.assign(node, {
start,
end,
range: [start, end],
range,
loc: {
start: {
line: restoredStartLine,
column: startLoc.column + (restoredStartLine === 1 ? offset : 0),
line: point.line + startLoc.line,
column: startLoc.column + (startLoc.line === 1 ? point.column : 0),
},
end: {
line: restoredEndLine,
column: endLoc.column + (restoredEndLine === 1 ? offset : 0),
line: point.line + endLoc.line,
column: endLoc.column + (endLoc.line === 1 ? point.column : 0),
},

@@ -90,0 +104,0 @@ },

@@ -5,3 +5,3 @@ import path from 'path';

import unified from 'unified';
import { arrayify, hasProperties, isJsxNode, last, normalizeParser, normalizePosition, restoreNodeLocation, } from './helpers';
import { arrayify, getPositionAt, hasProperties, isJsxNode, last, normalizeParser, normalizePosition, restoreNodeLocation, } from './helpers';
import { COMMENT_CONTENT_REGEX, COMMENT_CONTENT_REGEX_GLOBAL, isComment, } from './regexp';

@@ -108,3 +108,3 @@ import { traverse } from './traverse';

for (const normalizedNode of arrayify(this.normalizeJsxNode(node, parent, options))) {
this._nodeToAst(normalizedNode, options);
this._nodeToAst(code, normalizedNode, options);
}

@@ -216,3 +216,3 @@ },

// @internal
_nodeToAst(node, options) {
_nodeToAst(code, node, options) {
if (node.data && node.data.jsxType === 'JSXElementWithHTMLComments') {

@@ -249,7 +249,12 @@ this._services.JSXElementsWithHTMLComments.push(node);

}
const offset = start - program.range[0];
const startPoint = {
line: startLine,
// #279 related
column: getPositionAt(code, start).column,
offset: start,
};
for (const prop of AST_PROPS)
this._ast[prop].push(
// ts doesn't understand the mixed type
...program[prop].map((item) => restoreNodeLocation(item, startLine, offset)));
...program[prop].map((item) => restoreNodeLocation(item, startPoint)));
}

@@ -256,0 +261,0 @@ }

import type { JSXElement, JSXFragment } from '@babel/types';
import type { AST, Linter } from 'eslint';
import type { Node, Parent, Point } from 'unist';
export declare type Arrayable<T> = T[] | readonly T[];
export declare type ValueOf<T> = T extends {
[key: string]: infer M;
} ? M : T extends {
[key: number]: infer N;
} ? N : never;
export declare type JsxNode = (JSXElement | JSXFragment) & {
range: [number, number];
};
export declare type Arrayable<T> = T[] | readonly T[];
export declare type ParserFn = (code: string, options: Linter.ParserOptions) => AST.Program | Linter.ESLintParseResult;

@@ -9,0 +14,0 @@ export declare type ParserConfig = {

{
"name": "eslint-mdx",
"version": "1.11.0",
"version": "1.11.1",
"description": "ESLint Parser for MDX",

@@ -41,3 +41,3 @@ "repository": "git+https://github.com/mdx-js/eslint-mdx.git",

},
"gitHead": "1281f4fb34ec2f55f7c698a6165cba60debcee9f"
"gitHead": "b825393fa3f794809fd399fb49e835aee3424b37"
}

@@ -35,2 +35,3 @@ <p align="center">

- [Install](#install)
- [Notice](#notice)
- [Usage](#usage)

@@ -71,2 +72,8 @@ - [Parser Options](#parser-options)

## Notice
If you're using multi languages, `js/jsx/ts/tsx/vue`, etc for example, you'd better to always use [`overrides`](https://eslint.org/docs/user-guide/configuring/configuration-files#how-do-overrides-work) feature of ESLint, because configs may be overridden by following configs.
See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-736139224) for more details.
## Usage

@@ -83,3 +90,6 @@

"settings": {
"mdx/code-blocks": true
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}

@@ -96,3 +106,6 @@ }

"settings": {
"mdx/code-blocks": true
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
},

@@ -134,2 +147,5 @@ "overrides": [

'mdx/code-blocks': true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
'mdx/language-mapper': {},
},

@@ -136,0 +152,0 @@ overrides: [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc