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.14.1 to 1.15.0

lib/processor.d.ts

16

CHANGELOG.md

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

# [1.15.0](https://github.com/mdx-js/eslint-mdx/compare/v1.14.1...v1.15.0) (2021-08-21)
### Bug Fixes
* should only fallback on parse error ([#335](https://github.com/mdx-js/eslint-mdx/issues/335)) ([0feb2d3](https://github.com/mdx-js/eslint-mdx/commit/0feb2d395a5f6efc7e515afc2ae0cf07ec77af78)), closes [#334](https://github.com/mdx-js/eslint-mdx/issues/334)
### Features
* resolve custom remark config on parsing ([#337](https://github.com/mdx-js/eslint-mdx/issues/337)) ([94fcb4c](https://github.com/mdx-js/eslint-mdx/commit/94fcb4c1346d9bfdec61a8335b65e536d5b59ac4))
## [1.14.1](https://github.com/mdx-js/eslint-mdx/compare/v1.14.0...v1.14.1) (2021-07-14)

@@ -8,0 +24,0 @@

97

lib/es2015.js
import path from 'path';
import fs from 'fs';
import { cosmiconfigSync } from 'cosmiconfig';
import remarkMdx from 'remark-mdx';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import unified from 'unified';

@@ -103,2 +106,79 @@

const requirePkg = (plugin, prefix, filePath) => {
if (filePath && /^\.\.?([/\\]|$)/.test(plugin)) {
plugin = path.resolve(path.dirname(filePath), plugin);
}
prefix = prefix.endsWith("-") ? prefix : prefix + "-";
const packages = [
plugin,
plugin.startsWith("@") ? plugin.replace("/", "/" + prefix) : prefix + plugin
];
let error;
for (const pkg of packages) {
try {
return require(pkg);
} catch (err) {
if (!error) {
error = err;
}
}
}
throw error;
};
const getPhysicalFilename = (filename) => {
try {
if (fs.statSync(filename).isFile()) {
return filename;
}
} catch (err) {
if (err.code === "ENOTDIR") {
return getPhysicalFilename(path.dirname(filename));
}
}
return filename;
};
const remarkProcessor = unified().use(remarkParse).freeze();
const explorer = cosmiconfigSync("remark", {
packageProp: "remarkConfig"
});
const processorCache = new Map();
const getRemarkProcessor = (searchFrom, isMdx) => {
const initCacheKey = `${String(isMdx)}-${searchFrom}`;
let cachedProcessor = processorCache.get(initCacheKey);
if (cachedProcessor) {
return cachedProcessor;
}
const result = explorer.search(searchFrom);
const cacheKey = result ? `${String(isMdx)}-${result.filepath}` : "";
cachedProcessor = processorCache.get(cacheKey);
if (cachedProcessor) {
return cachedProcessor;
}
if (result) {
const { plugins = [], settings } = result.config || {};
if (plugins.length > 0) {
try {
plugins.push([require.resolve("remark-lint-file-extension"), false]);
} catch (e) {
}
}
const initProcessor = remarkProcessor().use({ settings }).use(remarkStringify);
if (isMdx) {
initProcessor.use(remarkMdx);
}
cachedProcessor = plugins.reduce((processor, pluginWithSettings) => {
const [plugin, ...pluginSettings] = arrayify(pluginWithSettings);
return processor.use(typeof plugin === "string" ? requirePkg(plugin, "remark", result.filepath) : plugin, ...pluginSettings);
}, initProcessor).freeze();
} else {
const initProcessor = remarkProcessor().use(remarkStringify);
if (isMdx) {
initProcessor.use(remarkMdx);
}
cachedProcessor = initProcessor.freeze();
}
processorCache.set(initCacheKey, cachedProcessor).set(cacheKey, cachedProcessor);
return cachedProcessor;
};
const dotAllPolyfill = "[\0-\uFFFF]";

@@ -189,9 +269,9 @@ const attributeName = "[a-zA-Z_:][a-zA-Z0-9:._-]*";

} else {
if (!index) {
offset++;
hasOpenTag = true;
}
try {
jsxNodes.push(...arrayify(parser.normalizeJsxNode(node, parent)));
} catch (e) {
if (!index) {
offset++;
hasOpenTag = true;
}
if (offset) {

@@ -269,4 +349,2 @@ jsxNodes.push(node);

var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const mdProcessor = unified().use(remarkParse).freeze();
const mdxProcessor = mdProcessor().use(remarkMdx).freeze();
const AST_PROPS = ["body", "comments", "tokens"];

@@ -277,2 +355,3 @@ const ES_NODE_TYPES = ["export", "import", "jsx"];

const MARKDOWN_EXTENSIONS = [".md"];
const PLACEHOLDER_FILE_PATH = "__placeholder__.mdx";
const DEFAULT_PARSER_OPTIONS = {

@@ -286,3 +365,3 @@ comment: true,

tokens: true,
filePath: "__placeholder__.mdx",
filePath: PLACEHOLDER_FILE_PATH,
loc: true,

@@ -359,3 +438,3 @@ range: true

}
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code);
const root = getRemarkProcessor(getPhysicalFilename(options.filePath), isMdx).parse(code);
this._ast = __spreadProps(__spreadValues({}, normalizePosition(root.position)), {

@@ -521,2 +600,2 @@ type: "Program",

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 };
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, PLACEHOLDER_FILE_PATH, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, getPhysicalFilename, getPositionAt, getRemarkProcessor, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, processorCache, remarkProcessor, requirePkg, restoreNodeLocation, selfClosingTag, traverse };
import path from 'path';
import fs from 'fs';
import { cosmiconfigSync } from 'cosmiconfig';
import remarkMdx from 'remark-mdx';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import unified from 'unified';

@@ -103,2 +106,79 @@

const requirePkg = (plugin, prefix, filePath) => {
if (filePath && /^\.\.?([/\\]|$)/.test(plugin)) {
plugin = path.resolve(path.dirname(filePath), plugin);
}
prefix = prefix.endsWith("-") ? prefix : prefix + "-";
const packages = [
plugin,
plugin.startsWith("@") ? plugin.replace("/", "/" + prefix) : prefix + plugin
];
let error;
for (const pkg of packages) {
try {
return require(pkg);
} catch (err) {
if (!error) {
error = err;
}
}
}
throw error;
};
const getPhysicalFilename = (filename) => {
try {
if (fs.statSync(filename).isFile()) {
return filename;
}
} catch (err) {
if (err.code === "ENOTDIR") {
return getPhysicalFilename(path.dirname(filename));
}
}
return filename;
};
const remarkProcessor = unified().use(remarkParse).freeze();
const explorer = cosmiconfigSync("remark", {
packageProp: "remarkConfig"
});
const processorCache = new Map();
const getRemarkProcessor = (searchFrom, isMdx) => {
const initCacheKey = `${String(isMdx)}-${searchFrom}`;
let cachedProcessor = processorCache.get(initCacheKey);
if (cachedProcessor) {
return cachedProcessor;
}
const result = explorer.search(searchFrom);
const cacheKey = result ? `${String(isMdx)}-${result.filepath}` : "";
cachedProcessor = processorCache.get(cacheKey);
if (cachedProcessor) {
return cachedProcessor;
}
if (result) {
const { plugins = [], settings } = result.config || {};
if (plugins.length > 0) {
try {
plugins.push([require.resolve("remark-lint-file-extension"), false]);
} catch (e) {
}
}
const initProcessor = remarkProcessor().use({ settings }).use(remarkStringify);
if (isMdx) {
initProcessor.use(remarkMdx);
}
cachedProcessor = plugins.reduce((processor, pluginWithSettings) => {
const [plugin, ...pluginSettings] = arrayify(pluginWithSettings);
return processor.use(typeof plugin === "string" ? requirePkg(plugin, "remark", result.filepath) : plugin, ...pluginSettings);
}, initProcessor).freeze();
} else {
const initProcessor = remarkProcessor().use(remarkStringify);
if (isMdx) {
initProcessor.use(remarkMdx);
}
cachedProcessor = initProcessor.freeze();
}
processorCache.set(initCacheKey, cachedProcessor).set(cacheKey, cachedProcessor);
return cachedProcessor;
};
const dotAllPolyfill = "[\0-\uFFFF]";

@@ -189,9 +269,9 @@ const attributeName = "[a-zA-Z_:][a-zA-Z0-9:._-]*";

} else {
if (!index) {
offset++;
hasOpenTag = true;
}
try {
jsxNodes.push(...arrayify(parser.normalizeJsxNode(node, parent)));
} catch (e) {
if (!index) {
offset++;
hasOpenTag = true;
}
if (offset) {

@@ -269,4 +349,2 @@ jsxNodes.push(node);

var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
const mdProcessor = unified().use(remarkParse).freeze();
const mdxProcessor = mdProcessor().use(remarkMdx).freeze();
const AST_PROPS = ["body", "comments", "tokens"];

@@ -277,2 +355,3 @@ const ES_NODE_TYPES = ["export", "import", "jsx"];

const MARKDOWN_EXTENSIONS = [".md"];
const PLACEHOLDER_FILE_PATH = "__placeholder__.mdx";
const DEFAULT_PARSER_OPTIONS = {

@@ -286,3 +365,3 @@ comment: true,

tokens: true,
filePath: "__placeholder__.mdx",
filePath: PLACEHOLDER_FILE_PATH,
loc: true,

@@ -359,3 +438,3 @@ range: true

}
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code);
const root = getRemarkProcessor(getPhysicalFilename(options.filePath), isMdx).parse(code);
this._ast = __spreadProps(__spreadValues({}, normalizePosition(root.position)), {

@@ -521,2 +600,2 @@ type: "Program",

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 };
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, PLACEHOLDER_FILE_PATH, Parser, SELF_CLOSING_TAG_REGEX, Traverse, arrayify, closeTag, comment, commentClose, commentContent, commentOpen, first, getPhysicalFilename, getPositionAt, getRemarkProcessor, hasProperties, isCloseTag, isComment, isJsxNode, isOpenCloseTag, isOpenTag, isSelfClosingTag, last, normalizeParser, normalizePosition, openTag, parse, parseForESLint, parser, processorCache, remarkProcessor, requirePkg, restoreNodeLocation, selfClosingTag, traverse };
export * from './helpers';
export * from './parser';
export * from './processor';
export * from './regexp';
export * from './traverse';
export * from './types';

@@ -6,2 +6,3 @@ "use strict";

tslib_1.__exportStar(require("./parser"), exports);
tslib_1.__exportStar(require("./processor"), exports);
tslib_1.__exportStar(require("./regexp"), exports);

@@ -8,0 +9,0 @@ tslib_1.__exportStar(require("./traverse"), exports);

4

lib/parser.d.ts
import type { AST, Linter } from 'eslint';
import unified from 'unified';
import type { Node, Parent, ParserOptions } from './types';
export declare const mdProcessor: unified.FrozenProcessor<unified.Settings>;
export declare const mdxProcessor: unified.FrozenProcessor<unified.Settings>;
export declare const AST_PROPS: readonly ["body", "comments", "tokens"];

@@ -11,2 +8,3 @@ export declare const ES_NODE_TYPES: readonly string[];

export declare const MARKDOWN_EXTENSIONS: readonly string[];
export declare const PLACEHOLDER_FILE_PATH = "__placeholder__.mdx";
export declare const DEFAULT_PARSER_OPTIONS: ParserOptions;

@@ -13,0 +11,0 @@ export declare class Parser {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseForESLint = exports.parse = exports.parser = exports.Parser = exports.DEFAULT_PARSER_OPTIONS = exports.MARKDOWN_EXTENSIONS = exports.DEFAULT_EXTENSIONS = exports.LOC_ERROR_PROPERTIES = exports.ES_NODE_TYPES = exports.AST_PROPS = exports.mdxProcessor = exports.mdProcessor = void 0;
exports.parseForESLint = exports.parse = exports.parser = exports.Parser = exports.DEFAULT_PARSER_OPTIONS = exports.PLACEHOLDER_FILE_PATH = exports.MARKDOWN_EXTENSIONS = exports.DEFAULT_EXTENSIONS = exports.LOC_ERROR_PROPERTIES = exports.ES_NODE_TYPES = exports.AST_PROPS = void 0;
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const remark_mdx_1 = tslib_1.__importDefault(require("remark-mdx"));
const remark_parse_1 = tslib_1.__importDefault(require("remark-parse"));
const unified_1 = tslib_1.__importDefault(require("unified"));
const helpers_1 = require("./helpers");
const processor_1 = require("./processor");
const regexp_1 = require("./regexp");
const traverse_1 = require("./traverse");
exports.mdProcessor = unified_1.default().use(remark_parse_1.default).freeze();
exports.mdxProcessor = exports.mdProcessor().use(remark_mdx_1.default).freeze();
exports.AST_PROPS = ['body', 'comments', 'tokens'];

@@ -19,2 +15,3 @@ exports.ES_NODE_TYPES = ['export', 'import', 'jsx'];

exports.MARKDOWN_EXTENSIONS = ['.md'];
exports.PLACEHOLDER_FILE_PATH = '__placeholder__.mdx';
exports.DEFAULT_PARSER_OPTIONS = {

@@ -28,3 +25,3 @@ comment: true,

tokens: true,
filePath: '__placeholder__.mdx',
filePath: exports.PLACEHOLDER_FILE_PATH,
// required for @typescript-eslint/parser

@@ -100,3 +97,3 @@ // reference: https://github.com/typescript-eslint/typescript-eslint/pull/2028

}
const root = (isMdx ? exports.mdxProcessor : exports.mdProcessor).parse(code);
const root = processor_1.getRemarkProcessor(processor_1.getPhysicalFilename(options.filePath), isMdx).parse(code);
this._ast = Object.assign(Object.assign({}, helpers_1.normalizePosition(root.position)), { type: 'Program', sourceType: options.sourceType || 'module', body: [], comments: [], tokens: [] });

@@ -103,0 +100,0 @@ this._services = {

@@ -18,3 +18,3 @@ "use strict";

if (parent && ((_a = parent.position.indent) === null || _a === void 0 ? void 0 : _a.length) > 0) {
end.offset += parent.position.indent.reduce((acc, indent, index) => acc + (index ? indent + 1 : 0), 0);
end.offset += parent.position.indent.reduce((acc, indent, index) => acc + (index ? /* istanbul ignore next */ indent + 1 : 0), 0);
}

@@ -57,7 +57,2 @@ return {

else {
// #272, we consider the first jsx node as open tag although it's not precise
if (!index) {
offset++;
hasOpenTag = true;
}
try {

@@ -68,2 +63,8 @@ // fix #138

catch (_a) {
// #272, we consider the first jsx node as open tag although it's not precise
// and #334, if there is no error thrown, do not fallback
if (!index) {
offset++;
hasOpenTag = true;
}
// #272 related

@@ -70,0 +71,0 @@ /* istanbul ignore else */

import type { JSXElement, JSXFragment } from '@babel/types';
import type { AST, Linter } from 'eslint';
import type { Attacher } from 'unified';
import type { Node as _Node, Parent as _Parent, Point } from 'unist';

@@ -53,1 +54,6 @@ export interface Node<T = string> extends _Node {

}
export declare type RemarkPlugin = Attacher | string;
export interface RemarkConfig {
settings: Record<string, string>;
plugins: Array<RemarkPlugin | [RemarkPlugin, ...unknown[]]>;
}
{
"name": "eslint-mdx",
"version": "1.14.1",
"version": "1.15.0",
"description": "ESLint Parser for MDX",

@@ -35,8 +35,10 @@ "repository": "git+https://github.com/mdx-js/eslint-mdx.git",

"dependencies": {
"cosmiconfig": "^7.0.0",
"remark-mdx": "^1.6.22",
"remark-parse": "^8.0.3",
"tslib": "^2.3.0",
"remark-stringify": "^8.1.1",
"tslib": "^2.3.1",
"unified": "^9.2.1"
},
"gitHead": "442cc022e45cdc355157f530f8e486489727ae89"
"gitHead": "a7afce656975c9927f4174f1a480fddaa57f80a3"
}

Sorry, the diff of this file is not supported yet

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