From c102c9026f682fce1b0d3fb724398c703c06da8f Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 5 Jul 2019 15:58:39 +0300 Subject: [PATCH 1/7] Replace rehype with svg-parser https://npm.anvaka.com/#/view/2d/rehype https://packagephobia.now.sh/result?p=rehype https://packagephobia.now.sh/result?p=svg-parser https://npm.anvaka.com/#/view/2d/svg-parser Did not notice any changes in speed. Though startup time is probably better. --- .../src/__snapshots__/index.test.js.snap | 6 +-- packages/hast-util-to-babel-ast/src/all.js | 18 ------- .../src/getAttributes.js | 26 ++++----- .../hast-util-to-babel-ast/src/handlers.js | 53 ------------------- packages/hast-util-to-babel-ast/src/index.js | 51 ++++++++++++++++-- .../hast-util-to-babel-ast/src/index.test.js | 13 +---- packages/hast-util-to-babel-ast/src/one.js | 17 ------ packages/plugin-jsx/package.json | 4 +- packages/plugin-jsx/src/index.js | 13 +---- yarn.lock | 12 +++++ 10 files changed, 78 insertions(+), 135 deletions(-) delete mode 100644 packages/hast-util-to-babel-ast/src/all.js delete mode 100644 packages/hast-util-to-babel-ast/src/handlers.js delete mode 100644 packages/hast-util-to-babel-ast/src/one.js diff --git a/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap b/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap index 22fdc510..dc1bf524 100644 --- a/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap +++ b/packages/hast-util-to-babel-ast/src/__snapshots__/index.test.js.snap @@ -1,9 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`hast-util-to-babel-ast should correctly transform svg 1`] = ` -"{ - /* Generator: Sketch 46.2 (44496) - http://www.bohemiancoding.com/sketch */ - }{\\"Dismiss\\"}{\\"Created with Sketch.\\"};" -`; +exports[`hast-util-to-babel-ast should correctly transform svg 1`] = `"{\\"Dismiss\\"}{\\"Created with Sketch.\\"};"`; exports[`hast-util-to-babel-ast should handle spaces and tab 1`] = `";"`; diff --git a/packages/hast-util-to-babel-ast/src/all.js b/packages/hast-util-to-babel-ast/src/all.js deleted file mode 100644 index 255f12e4..00000000 --- a/packages/hast-util-to-babel-ast/src/all.js +++ /dev/null @@ -1,18 +0,0 @@ -import one from './one' - -/* Transform the children of `parent`. */ -function all(h, parent) { - const nodes = parent.children || [] - const { length } = nodes - const values = [] - let index = -1 - - while (++index < length) { - const result = one(h, nodes[index], parent) - values.push(result) - } - - return values.filter(node => node) -} - -export default all diff --git a/packages/hast-util-to-babel-ast/src/getAttributes.js b/packages/hast-util-to-babel-ast/src/getAttributes.js index fbc0c89b..da59898b 100644 --- a/packages/hast-util-to-babel-ast/src/getAttributes.js +++ b/packages/hast-util-to-babel-ast/src/getAttributes.js @@ -8,11 +8,11 @@ function convertAriaAttribute(kebabKey) { return `${aria}-${parts.join('').toLowerCase()}` } -function getKey(key, value, node) { +function getKey(key, value, nodeName) { const lowerCaseKey = key.toLowerCase() const mappedElementAttribute = - ELEMENT_ATTRIBUTE_MAPPING[node.name] && - ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey] + ELEMENT_ATTRIBUTE_MAPPING[nodeName] && + ELEMENT_ATTRIBUTE_MAPPING[nodeName][lowerCaseKey] const mappedAttribute = ATTRIBUTE_MAPPING[lowerCaseKey] if (mappedElementAttribute || mappedAttribute) { @@ -49,22 +49,22 @@ function getValue(key, value) { return t.stringLiteral(replaceSpaces(value)) } -const getAttributes = node => { - const keys = Object.keys(node.properties) - const attributes = [] +const getProperties = (nodeName, attributes) => { + const keys = Object.keys(attributes) + const properties = [] let index = -1 while (++index < keys.length) { const key = keys[index] - const value = node.properties[key] - const attribute = t.jsxAttribute( - getKey(key, value, node), - getValue(key, value, node), + const value = attributes[key] + const property = t.jsxAttribute( + getKey(key, value, nodeName), + getValue(key, value), ) - attributes.push(attribute) + properties.push(property) } - return attributes + return properties } -export default getAttributes +export default getProperties diff --git a/packages/hast-util-to-babel-ast/src/handlers.js b/packages/hast-util-to-babel-ast/src/handlers.js deleted file mode 100644 index 7c218e52..00000000 --- a/packages/hast-util-to-babel-ast/src/handlers.js +++ /dev/null @@ -1,53 +0,0 @@ -import * as t from '@babel/types' -import all from './all' -import getAttributes from './getAttributes' -import { ELEMENT_TAG_NAME_MAPPING } from './mappings' - -export const root = (h, node) => t.program(all(h, node)) - -export const comment = (h, node, parent) => { - if (parent.type === 'root') { - return null - } - - const expression = t.jsxEmptyExpression() - t.addComment(expression, 'inner', node.value) - return t.jsxExpressionContainer(expression) -} - -export const text = (h, node, parent) => { - if (parent.type === 'root') { - return null - } - - if (node.value.match(/^\s+$/)) { - return null - } - - return t.jsxExpressionContainer(t.stringLiteral(node.value)) -} - -export const element = (h, node, parent) => { - const children = all(h, node) - const selfClosing = children.length === 0 - - const name = ELEMENT_TAG_NAME_MAPPING[node.tagName] || node.tagName - - const openingElement = t.jsxOpeningElement( - t.jsxIdentifier(name), - getAttributes(node), - selfClosing, - ) - - const closingElement = !selfClosing - ? t.jsxClosingElement(t.jsxIdentifier(name)) - : null - - const jsxElement = t.jsxElement(openingElement, closingElement, children) - - if (parent.type === 'root') { - return t.expressionStatement(jsxElement) - } - - return jsxElement -} diff --git a/packages/hast-util-to-babel-ast/src/index.js b/packages/hast-util-to-babel-ast/src/index.js index bdc03a1c..3b9b1fcd 100644 --- a/packages/hast-util-to-babel-ast/src/index.js +++ b/packages/hast-util-to-babel-ast/src/index.js @@ -1,10 +1,53 @@ -import * as handlers from './handlers' -import one from './one' +import * as t from '@babel/types' +import getAttributes from './getAttributes' +import { ELEMENT_TAG_NAME_MAPPING } from './mappings' -const h = { handlers } +/* Transform the children of `parent`. */ +function all(parent) { + const nodes = parent.children || [] + const { length } = nodes + const values = [] + let index = -1 + + while (++index < length) { + const result = one(nodes[index], parent) + values.push(result) + } + + return values.filter(node => node) +} + +function one(node, parent) { + if (typeof node === 'string') { + return t.jsxExpressionContainer(t.stringLiteral(node)) + } else { + const children = all(node) + const selfClosing = children.length === 0 + + const name = ELEMENT_TAG_NAME_MAPPING[node.name] || node.name + + const openingElement = t.jsxOpeningElement( + t.jsxIdentifier(name), + getAttributes(node.name, node.attributes), + selfClosing, + ) + + const closingElement = !selfClosing + ? t.jsxClosingElement(t.jsxIdentifier(name)) + : null + + const jsxElement = t.jsxElement(openingElement, closingElement, children) + + if (parent == null) { + return t.expressionStatement(jsxElement) + } + + return jsxElement + } +} function toBabelAST(tree) { - return one(h, tree) + return t.program([one(tree)]) } export default toBabelAST diff --git a/packages/hast-util-to-babel-ast/src/index.test.js b/packages/hast-util-to-babel-ast/src/index.test.js index 09a7ce7b..4a12e906 100644 --- a/packages/hast-util-to-babel-ast/src/index.test.js +++ b/packages/hast-util-to-babel-ast/src/index.test.js @@ -1,18 +1,9 @@ -import unified from 'unified' -import parse from 'rehype-parse' -import vfile from 'vfile' +import { parse } from 'svg-parser' import generate from '@babel/generator' import hastToBabelAst from './index' function transform(code) { - const hastTree = unified() - .use(parse, { - fragment: true, - space: 'svg', - emitParseErrors: true, - duplicateAttribute: false, - }) - .parse(vfile({ path: 'test.svg', contents: code })) + const hastTree = parse(code) const babelTree = hastToBabelAst(hastTree) diff --git a/packages/hast-util-to-babel-ast/src/one.js b/packages/hast-util-to-babel-ast/src/one.js deleted file mode 100644 index f3fd0ed3..00000000 --- a/packages/hast-util-to-babel-ast/src/one.js +++ /dev/null @@ -1,17 +0,0 @@ -function one(h, node, parent) { - const type = node && node.type - const fn = h.handlers[type] - - /* Fail on non-nodes. */ - if (!type) { - throw new Error(`Expected node, got \`${node}\``) - } - - if (!fn) { - throw new Error(`Node of type ${type} is unknown`) - } - - return fn(h, node, parent) -} - -export default one diff --git a/packages/plugin-jsx/package.json b/packages/plugin-jsx/package.json index 2141c3ab..72effe45 100644 --- a/packages/plugin-jsx/package.json +++ b/packages/plugin-jsx/package.json @@ -24,8 +24,6 @@ "@babel/core": "^7.4.5", "@svgr/babel-preset": "^4.3.1", "@svgr/hast-util-to-babel-ast": "^4.3.1", - "rehype-parse": "^6.0.0", - "unified": "^7.1.0", - "vfile": "^4.0.1" + "svg-parser": "^1.0.5" } } diff --git a/packages/plugin-jsx/src/index.js b/packages/plugin-jsx/src/index.js index 142ea53d..62603ebe 100644 --- a/packages/plugin-jsx/src/index.js +++ b/packages/plugin-jsx/src/index.js @@ -1,20 +1,11 @@ -import unified from 'unified' -import parse from 'rehype-parse' -import vfile from 'vfile' +import { parse } from 'svg-parser' import hastToBabelAst from '@svgr/hast-util-to-babel-ast' import { transformFromAstSync, createConfigItem } from '@babel/core' import svgrBabelPreset from '@svgr/babel-preset' export default function jsxPlugin(code, config, state) { const filePath = state.filePath || 'unknown' - const hastTree = unified() - .use(parse, { - fragment: true, - space: 'svg', - emitParseErrors: true, - duplicateAttribute: false, - }) - .parse(vfile({ path: filePath, contents: code })) + const hastTree = parse(code) const babelTree = hastToBabelAst(hastTree) diff --git a/yarn.lock b/yarn.lock index d7a573ff..0253608c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6641,6 +6641,11 @@ loader-utils@^1.0.2, loader-utils@^1.1.0, loader-utils@^1.2.3: emojis-list "^2.0.0" json5 "^1.0.1" +locate-character@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-1.0.0.tgz#116443c23f29fae28900cac285ba0c71f2693f6d" + integrity sha1-EWRDwj8p+uKJAMrChboMcfJpP20= + locate-path@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e" @@ -9931,6 +9936,13 @@ supports-color@^6.1.0: dependencies: has-flag "^3.0.0" +svg-parser@^1.0.5: + version "1.0.5" + resolved "https://registry.yarnpkg.com/svg-parser/-/svg-parser-1.0.5.tgz#7ba73ebdaac7a542b842d68598872d0777e5b7ff" + integrity sha1-e6c+varHpUK4QtaFmIctB3flt/8= + dependencies: + locate-character "^1.0.0" + svgo@^1.0.0, svgo@^1.0.5, svgo@^1.2.2: version "1.2.2" resolved "https://registry.yarnpkg.com/svgo/-/svgo-1.2.2.tgz#0253d34eccf2aed4ad4f283e11ee75198f9d7316" From 78a835c18bc8513aa21518f2dd8c780cb0b1e165 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 5 Jul 2019 16:08:45 +0300 Subject: [PATCH 2/7] Fix lint --- packages/hast-util-to-babel-ast/src/index.js | 34 ++++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/hast-util-to-babel-ast/src/index.js b/packages/hast-util-to-babel-ast/src/index.js index 3b9b1fcd..fc2a5821 100644 --- a/packages/hast-util-to-babel-ast/src/index.js +++ b/packages/hast-util-to-babel-ast/src/index.js @@ -20,30 +20,30 @@ function all(parent) { function one(node, parent) { if (typeof node === 'string') { return t.jsxExpressionContainer(t.stringLiteral(node)) - } else { - const children = all(node) - const selfClosing = children.length === 0 + } - const name = ELEMENT_TAG_NAME_MAPPING[node.name] || node.name + const children = all(node) + const selfClosing = children.length === 0 - const openingElement = t.jsxOpeningElement( - t.jsxIdentifier(name), - getAttributes(node.name, node.attributes), - selfClosing, - ) + const name = ELEMENT_TAG_NAME_MAPPING[node.name] || node.name - const closingElement = !selfClosing - ? t.jsxClosingElement(t.jsxIdentifier(name)) - : null + const openingElement = t.jsxOpeningElement( + t.jsxIdentifier(name), + getAttributes(node.name, node.attributes), + selfClosing, + ) - const jsxElement = t.jsxElement(openingElement, closingElement, children) + const closingElement = !selfClosing + ? t.jsxClosingElement(t.jsxIdentifier(name)) + : null - if (parent == null) { - return t.expressionStatement(jsxElement) - } + const jsxElement = t.jsxElement(openingElement, closingElement, children) - return jsxElement + if (parent == null) { + return t.expressionStatement(jsxElement) } + + return jsxElement } function toBabelAST(tree) { From 449ea7f1930c121e429d08e808d46c7b4f77e33d Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 5 Jul 2019 16:14:06 +0300 Subject: [PATCH 3/7] Rename getAttributes to getProps --- .../src/{getAttributes.js => getProps.js} | 10 +++++----- packages/hast-util-to-babel-ast/src/index.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) rename packages/hast-util-to-babel-ast/src/{getAttributes.js => getProps.js} (91%) diff --git a/packages/hast-util-to-babel-ast/src/getAttributes.js b/packages/hast-util-to-babel-ast/src/getProps.js similarity index 91% rename from packages/hast-util-to-babel-ast/src/getAttributes.js rename to packages/hast-util-to-babel-ast/src/getProps.js index da59898b..411524ed 100644 --- a/packages/hast-util-to-babel-ast/src/getAttributes.js +++ b/packages/hast-util-to-babel-ast/src/getProps.js @@ -49,9 +49,9 @@ function getValue(key, value) { return t.stringLiteral(replaceSpaces(value)) } -const getProperties = (nodeName, attributes) => { +const getProps = (nodeName, attributes) => { const keys = Object.keys(attributes) - const properties = [] + const props = [] let index = -1 while (++index < keys.length) { @@ -61,10 +61,10 @@ const getProperties = (nodeName, attributes) => { getKey(key, value, nodeName), getValue(key, value), ) - properties.push(property) + props.push(property) } - return properties + return props } -export default getProperties +export default getProps diff --git a/packages/hast-util-to-babel-ast/src/index.js b/packages/hast-util-to-babel-ast/src/index.js index fc2a5821..05666583 100644 --- a/packages/hast-util-to-babel-ast/src/index.js +++ b/packages/hast-util-to-babel-ast/src/index.js @@ -1,5 +1,5 @@ import * as t from '@babel/types' -import getAttributes from './getAttributes' +import getProps from './getProps' import { ELEMENT_TAG_NAME_MAPPING } from './mappings' /* Transform the children of `parent`. */ @@ -29,7 +29,7 @@ function one(node, parent) { const openingElement = t.jsxOpeningElement( t.jsxIdentifier(name), - getAttributes(node.name, node.attributes), + getProps(node.name, node.attributes), selfClosing, ) From 9ae1c1b5a9e16826bc66f4a5c2b53a6f5b7bf17b Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Fri, 5 Jul 2019 16:36:03 +0300 Subject: [PATCH 4/7] Upgrade to svg-parser without dependencies --- package.json | 1 - packages/plugin-jsx/README.md | 2 +- packages/plugin-jsx/package.json | 2 +- .../src/pages/docs/custom-transformations.mdx | 2 +- website/src/pages/docs/motivation.mdx | 2 +- yarn.lock | 84 ++----------------- 6 files changed, 9 insertions(+), 84 deletions(-) diff --git a/package.json b/package.json index 22248fa1..28883f6b 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "jest": "^24.8.0", "lerna": "^3.14.1", "react": "^16.8.6", - "rehype-parse": "^6.0.0", "unified": "^7.1.0", "vfile": "^4.0.1" } diff --git a/packages/plugin-jsx/README.md b/packages/plugin-jsx/README.md index 3e2a00f2..40df5e14 100644 --- a/packages/plugin-jsx/README.md +++ b/packages/plugin-jsx/README.md @@ -26,7 +26,7 @@ npm install --save-dev @svgr/plugin-jsx `@svgr/plugin-jsx` consists in three phases: -- Parsing the SVG code using [rehype](https://github.com/rehypejs/rehype) +- Parsing the SVG code using [svg-parser](https://github.com/Rich-Harris/svg-parser) - Converting the [HAST](https://github.com/syntax-tree/hast) into a [Babel AST](https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md) - Applying [`@svgr/babel-preset`](../babel-preset/README.md) transformations diff --git a/packages/plugin-jsx/package.json b/packages/plugin-jsx/package.json index 72effe45..07fabd8a 100644 --- a/packages/plugin-jsx/package.json +++ b/packages/plugin-jsx/package.json @@ -24,6 +24,6 @@ "@babel/core": "^7.4.5", "@svgr/babel-preset": "^4.3.1", "@svgr/hast-util-to-babel-ast": "^4.3.1", - "svg-parser": "^1.0.5" + "svg-parser": "^1.0.6" } } diff --git a/website/src/pages/docs/custom-transformations.mdx b/website/src/pages/docs/custom-transformations.mdx index 0c77d1ae..85e8d96c 100644 --- a/website/src/pages/docs/custom-transformations.mdx +++ b/website/src/pages/docs/custom-transformations.mdx @@ -30,7 +30,7 @@ yarn add @svgr/plugin-jsx `@svgr/plugin-jsx` consists in three phases: -- Parsing the SVG code using [rehype](https://github.com/rehypejs/rehype) +- Parsing the SVG code using [svg-parser](https://github.com/Rich-Harris/svg-parser) - Converting the [HAST](https://github.com/syntax-tree/hast) into a [Babel AST](https://github.com/babel/babel/blob/master/packages/babel-parser/ast/spec.md) - Applying [`@svgr/babel-preset`](../babel-preset/README.md) transformations diff --git a/website/src/pages/docs/motivation.mdx b/website/src/pages/docs/motivation.mdx index cf901c4b..1206a60d 100644 --- a/website/src/pages/docs/motivation.mdx +++ b/website/src/pages/docs/motivation.mdx @@ -10,6 +10,6 @@ React supports SVG out of the box, it's simpler, easier and much more powerful to have components instead of SVG files. Wrapped in a React component, your SVG is inlined in the page and you can style it using CSS. -SVGR differs from other library by its solid architecture. It uses [rehype](https://github.com/rehypejs/rehype/) + [Babel](https://babeljs.io) to transform SVG code into JavaScript code. +SVGR differs from other library by its solid architecture. It uses [svg-parser](https://github.com/Rich-Harris/svg-parser) + [Babel](https://babeljs.io) to transform SVG code into JavaScript code. SVGR is included in [create-react-app v2](https://github.com/facebook/create-react-app) and gives you the power to [import SVG directly as a React component](https://facebook.github.io/create-react-app/docs/adding-images-fonts-and-files#adding-svgs). diff --git a/yarn.lock b/yarn.lock index 0253608c..347df3c2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2934,11 +2934,6 @@ caseless@~0.12.0: resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc" integrity sha1-G2gcIf+EAzyCZUMJBolCDRhxUdw= -ccount@^1.0.3: - version "1.0.4" - resolved "https://registry.yarnpkg.com/ccount/-/ccount-1.0.4.tgz#9cf2de494ca84060a2a8d2854edd6dfb0445f386" - integrity sha512-fpZ81yYfzentuieinmGnphk0pLkOTMm6MZdVqwd77ROvhko6iujLNGrHH5E7utq3ygWklwfmwuG+A7P+NpqT6w== - chalk@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/chalk/-/chalk-1.1.3.tgz#a8115c55e4a702fe4d150abd3872822a7e09fc98" @@ -3176,11 +3171,6 @@ combined-stream@^1.0.6, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -comma-separated-tokens@^1.0.0: - version "1.0.7" - resolved "https://registry.yarnpkg.com/comma-separated-tokens/-/comma-separated-tokens-1.0.7.tgz#419cd7fb3258b1ed838dc0953167a25e152f5b59" - integrity sha512-Jrx3xsP4pPv4AwJUDWY9wOXGtwPXARej6Xd99h4TUGotmf8APuquKMpK+dnD3UgyxK7OEWaisjZz+3b5jtL6xQ== - command-exists@^1.2.6: version "1.2.8" resolved "https://registry.yarnpkg.com/command-exists/-/command-exists-1.2.8.tgz#715acefdd1223b9c9b37110a149c6392c2852291" @@ -5226,32 +5216,6 @@ hash.js@^1.0.0, hash.js@^1.0.3: inherits "^2.0.3" minimalistic-assert "^1.0.1" -hast-util-from-parse5@^5.0.0: - version "5.0.1" - resolved "https://registry.yarnpkg.com/hast-util-from-parse5/-/hast-util-from-parse5-5.0.1.tgz#7da8841d707dcf7be73715f7f3b14e021c4e469a" - integrity sha512-UfPzdl6fbxGAxqGYNThRUhRlDYY7sXu6XU9nQeX4fFZtV+IHbyEJtd+DUuwOqNV4z3K05E/1rIkoVr/JHmeWWA== - dependencies: - ccount "^1.0.3" - hastscript "^5.0.0" - property-information "^5.0.0" - web-namespaces "^1.1.2" - xtend "^4.0.1" - -hast-util-parse-selector@^2.2.0: - version "2.2.2" - resolved "https://registry.yarnpkg.com/hast-util-parse-selector/-/hast-util-parse-selector-2.2.2.tgz#66aabccb252c47d94975f50a281446955160380b" - integrity sha512-jIMtnzrLTjzqgVEQqPEmwEZV+ea4zHRFTP8Z2Utw0I5HuBOXHzUPPQWr6ouJdJqDKLbFU/OEiYwZ79LalZkmmw== - -hastscript@^5.0.0: - version "5.1.0" - resolved "https://registry.yarnpkg.com/hastscript/-/hastscript-5.1.0.tgz#a19b3cca6a26a2bcd0f1b1eac574af9427c1c7df" - integrity sha512-7mOQX5VfVs/gmrOGlN8/EDfp1GqV6P3gTNVt+KnX4gbYhpASTM8bklFdFQCbFRAadURXAmw0R1QQdBdqp7jswQ== - dependencies: - comma-separated-tokens "^1.0.0" - hast-util-parse-selector "^2.2.0" - property-information "^5.0.1" - space-separated-tokens "^1.0.0" - hex-color-regex@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/hex-color-regex/-/hex-color-regex-1.1.0.tgz#4c06fccb4602fe2602b3c93df82d7e7dbf1a8a8e" @@ -6641,11 +6605,6 @@ loader-utils@^1.0.2, loader-utils@^1.1.0, loader-utils@^1.2.3: emojis-list "^2.0.0" json5 "^1.0.1" -locate-character@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/locate-character/-/locate-character-1.0.0.tgz#116443c23f29fae28900cac285ba0c71f2693f6d" - integrity sha1-EWRDwj8p+uKJAMrChboMcfJpP20= - locate-path@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-2.0.0.tgz#2b568b265eec944c6d9c0de9c3dbbbca0354cd8e" @@ -8000,11 +7959,6 @@ parse5@4.0.0: resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608" integrity sha512-VrZ7eOd3T1Fk4XWNXMgiGBK/z0MG48BWG2uQNU4I72fkQuKUTZpl+u9k+CxEG0twMVzSmXEEz12z5Fnw1jIQFA== -parse5@^5.0.0: - version "5.1.0" - resolved "https://registry.yarnpkg.com/parse5/-/parse5-5.1.0.tgz#c59341c9723f414c452975564c7c00a68d58acd2" - integrity sha512-fxNG2sQjHvlVAYmzBZS9YlDp6PTSSDwa98vkD4QgVDDCAo84z5X1t5XyJQ62ImdLXx5NdIIfihey6xpum9/gRQ== - parseurl@~1.3.3: version "1.3.3" resolved "https://registry.yarnpkg.com/parseurl/-/parseurl-1.3.3.tgz#9da19e7bee8d12dff0513ed5b76957793bc2e8d4" @@ -8591,13 +8545,6 @@ prop-types@^15.6.2, prop-types@^15.7.2: object-assign "^4.1.1" react-is "^16.8.1" -property-information@^5.0.0, property-information@^5.0.1: - version "5.1.0" - resolved "https://registry.yarnpkg.com/property-information/-/property-information-5.1.0.tgz#e4755eee5319f03f7f6f5a9bc1a6a7fea6609e2c" - integrity sha512-tODH6R3+SwTkAQckSp2S9xyYX8dEKYkeXw+4TmJzTxnNzd6mQPu1OD4f9zPrvw/Rm4wpPgI+Zp63mNSGNzUgHg== - dependencies: - xtend "^4.0.1" - proto-list@~1.2.1: version "1.2.4" resolved "https://registry.yarnpkg.com/proto-list/-/proto-list-1.2.4.tgz#212d5bfe1318306a420f6402b8e26ff39647a849" @@ -9034,15 +8981,6 @@ regjsparser@^0.6.0: dependencies: jsesc "~0.5.0" -rehype-parse@^6.0.0: - version "6.0.0" - resolved "https://registry.yarnpkg.com/rehype-parse/-/rehype-parse-6.0.0.tgz#f681555f2598165bee2c778b39f9073d17b16bca" - integrity sha512-V2OjMD0xcSt39G4uRdMTqDXXm6HwkUbLMDayYKA/d037j8/OtVSQ+tqKwYWOuyBeoCs/3clXRe30VUjeMDTBSA== - dependencies: - hast-util-from-parse5 "^5.0.0" - parse5 "^5.0.0" - xtend "^4.0.1" - remove-trailing-separator@^1.0.1: version "1.1.0" resolved "https://registry.yarnpkg.com/remove-trailing-separator/-/remove-trailing-separator-1.1.0.tgz#c24bce2a283adad5bc3f58e0d48249b92379d8ef" @@ -9618,11 +9556,6 @@ source-map@^0.5.0, source-map@^0.5.3, source-map@^0.5.6: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.7.tgz#8a039d2d1021d22d1ea14c80d8ea468ba2ef3fcc" integrity sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w= -space-separated-tokens@^1.0.0: - version "1.1.4" - resolved "https://registry.yarnpkg.com/space-separated-tokens/-/space-separated-tokens-1.1.4.tgz#27910835ae00d0adfcdbd0ad7e611fb9544351fa" - integrity sha512-UyhMSmeIqZrQn2UdjYpxEkwY9JUrn8pP+7L4f91zRzOQuI8MF1FGLfYU9DKCYeLdo7LXMxwrX5zKFy7eeeVHuA== - spdx-correct@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4" @@ -9936,12 +9869,10 @@ supports-color@^6.1.0: dependencies: has-flag "^3.0.0" -svg-parser@^1.0.5: - version "1.0.5" - resolved "https://registry.yarnpkg.com/svg-parser/-/svg-parser-1.0.5.tgz#7ba73ebdaac7a542b842d68598872d0777e5b7ff" - integrity sha1-e6c+varHpUK4QtaFmIctB3flt/8= - dependencies: - locate-character "^1.0.0" +svg-parser@^1.0.6: + version "1.0.6" + resolved "https://registry.yarnpkg.com/svg-parser/-/svg-parser-1.0.6.tgz#fdf104b9263b30e81e4738218bcb10293a816764" + integrity sha512-EPd4xbh+MnF0eaiJgQziJHCKEWnrNEYMIuvRoiyPi/XmSO+gb65gMdGid9/JrucsQ80Y84NTChxE5FW0WxuO3w== svgo@^1.0.0, svgo@^1.0.5, svgo@^1.2.2: version "1.2.2" @@ -10623,11 +10554,6 @@ wcwidth@^1.0.0, wcwidth@^1.0.1: dependencies: defaults "^1.0.3" -web-namespaces@^1.1.2: - version "1.1.3" - resolved "https://registry.yarnpkg.com/web-namespaces/-/web-namespaces-1.1.3.tgz#9bbf5c99ff0908d2da031f1d732492a96571a83f" - integrity sha512-r8sAtNmgR0WKOKOxzuSgk09JsHlpKlB+uHi937qypOu3PZ17UxPrierFKDye/uNHjNTTEshu5PId8rojIPj/tA== - webidl-conversions@^4.0.2: version "4.0.2" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-4.0.2.tgz#a855980b1f0b6b359ba1d5d9fb39ae941faa63ad" @@ -10819,7 +10745,7 @@ xml-name-validator@^3.0.0: resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a" integrity sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw== -xtend@^4.0.0, xtend@^4.0.1, xtend@~4.0.1: +xtend@^4.0.0, xtend@~4.0.1: version "4.0.1" resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af" integrity sha1-pcbVMr5lbiPbgg77lDofBJmNY68= From 4669920bc7a4d6c42c179af14e786d47fe12b112 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 9 Jul 2019 16:10:33 +0300 Subject: [PATCH 5/7] Upgrade svg-parser with hast support --- packages/hast-util-to-babel-ast/src/all.js | 18 +++++ .../src/getAttributes.js | 70 +++++++++++++++++++ .../hast-util-to-babel-ast/src/handlers.js | 53 ++++++++++++++ packages/hast-util-to-babel-ast/src/index.js | 51 ++------------ packages/hast-util-to-babel-ast/src/one.js | 17 +++++ packages/plugin-jsx/package.json | 2 +- yarn.lock | 8 +-- 7 files changed, 167 insertions(+), 52 deletions(-) create mode 100644 packages/hast-util-to-babel-ast/src/all.js create mode 100644 packages/hast-util-to-babel-ast/src/getAttributes.js create mode 100644 packages/hast-util-to-babel-ast/src/handlers.js create mode 100644 packages/hast-util-to-babel-ast/src/one.js diff --git a/packages/hast-util-to-babel-ast/src/all.js b/packages/hast-util-to-babel-ast/src/all.js new file mode 100644 index 00000000..255f12e4 --- /dev/null +++ b/packages/hast-util-to-babel-ast/src/all.js @@ -0,0 +1,18 @@ +import one from './one' + +/* Transform the children of `parent`. */ +function all(h, parent) { + const nodes = parent.children || [] + const { length } = nodes + const values = [] + let index = -1 + + while (++index < length) { + const result = one(h, nodes[index], parent) + values.push(result) + } + + return values.filter(node => node) +} + +export default all diff --git a/packages/hast-util-to-babel-ast/src/getAttributes.js b/packages/hast-util-to-babel-ast/src/getAttributes.js new file mode 100644 index 00000000..fbc0c89b --- /dev/null +++ b/packages/hast-util-to-babel-ast/src/getAttributes.js @@ -0,0 +1,70 @@ +import * as t from '@babel/types' +import { isNumeric, kebabCase, replaceSpaces } from './util' +import stringToObjectStyle from './stringToObjectStyle' +import { ATTRIBUTE_MAPPING, ELEMENT_ATTRIBUTE_MAPPING } from './mappings' + +function convertAriaAttribute(kebabKey) { + const [aria, ...parts] = kebabKey.split('-') + return `${aria}-${parts.join('').toLowerCase()}` +} + +function getKey(key, value, node) { + const lowerCaseKey = key.toLowerCase() + const mappedElementAttribute = + ELEMENT_ATTRIBUTE_MAPPING[node.name] && + ELEMENT_ATTRIBUTE_MAPPING[node.name][lowerCaseKey] + const mappedAttribute = ATTRIBUTE_MAPPING[lowerCaseKey] + + if (mappedElementAttribute || mappedAttribute) { + return t.jsxIdentifier(mappedElementAttribute || mappedAttribute) + } + + const kebabKey = kebabCase(key) + + if (kebabKey.startsWith('aria-')) { + return t.jsxIdentifier(convertAriaAttribute(kebabKey)) + } + + if (kebabKey.startsWith('data-')) { + return t.jsxIdentifier(kebabKey) + } + + return t.jsxIdentifier(key) +} + +function getValue(key, value) { + // Handle className + if (Array.isArray(value)) { + return t.stringLiteral(replaceSpaces(value.join(' '))) + } + + if (key === 'style') { + return t.jsxExpressionContainer(stringToObjectStyle(value)) + } + + if (isNumeric(value)) { + return t.jsxExpressionContainer(t.numericLiteral(Number(value))) + } + + return t.stringLiteral(replaceSpaces(value)) +} + +const getAttributes = node => { + const keys = Object.keys(node.properties) + const attributes = [] + let index = -1 + + while (++index < keys.length) { + const key = keys[index] + const value = node.properties[key] + const attribute = t.jsxAttribute( + getKey(key, value, node), + getValue(key, value, node), + ) + attributes.push(attribute) + } + + return attributes +} + +export default getAttributes diff --git a/packages/hast-util-to-babel-ast/src/handlers.js b/packages/hast-util-to-babel-ast/src/handlers.js new file mode 100644 index 00000000..7c218e52 --- /dev/null +++ b/packages/hast-util-to-babel-ast/src/handlers.js @@ -0,0 +1,53 @@ +import * as t from '@babel/types' +import all from './all' +import getAttributes from './getAttributes' +import { ELEMENT_TAG_NAME_MAPPING } from './mappings' + +export const root = (h, node) => t.program(all(h, node)) + +export const comment = (h, node, parent) => { + if (parent.type === 'root') { + return null + } + + const expression = t.jsxEmptyExpression() + t.addComment(expression, 'inner', node.value) + return t.jsxExpressionContainer(expression) +} + +export const text = (h, node, parent) => { + if (parent.type === 'root') { + return null + } + + if (node.value.match(/^\s+$/)) { + return null + } + + return t.jsxExpressionContainer(t.stringLiteral(node.value)) +} + +export const element = (h, node, parent) => { + const children = all(h, node) + const selfClosing = children.length === 0 + + const name = ELEMENT_TAG_NAME_MAPPING[node.tagName] || node.tagName + + const openingElement = t.jsxOpeningElement( + t.jsxIdentifier(name), + getAttributes(node), + selfClosing, + ) + + const closingElement = !selfClosing + ? t.jsxClosingElement(t.jsxIdentifier(name)) + : null + + const jsxElement = t.jsxElement(openingElement, closingElement, children) + + if (parent.type === 'root') { + return t.expressionStatement(jsxElement) + } + + return jsxElement +} diff --git a/packages/hast-util-to-babel-ast/src/index.js b/packages/hast-util-to-babel-ast/src/index.js index 05666583..bdc03a1c 100644 --- a/packages/hast-util-to-babel-ast/src/index.js +++ b/packages/hast-util-to-babel-ast/src/index.js @@ -1,53 +1,10 @@ -import * as t from '@babel/types' -import getProps from './getProps' -import { ELEMENT_TAG_NAME_MAPPING } from './mappings' +import * as handlers from './handlers' +import one from './one' -/* Transform the children of `parent`. */ -function all(parent) { - const nodes = parent.children || [] - const { length } = nodes - const values = [] - let index = -1 - - while (++index < length) { - const result = one(nodes[index], parent) - values.push(result) - } - - return values.filter(node => node) -} - -function one(node, parent) { - if (typeof node === 'string') { - return t.jsxExpressionContainer(t.stringLiteral(node)) - } - - const children = all(node) - const selfClosing = children.length === 0 - - const name = ELEMENT_TAG_NAME_MAPPING[node.name] || node.name - - const openingElement = t.jsxOpeningElement( - t.jsxIdentifier(name), - getProps(node.name, node.attributes), - selfClosing, - ) - - const closingElement = !selfClosing - ? t.jsxClosingElement(t.jsxIdentifier(name)) - : null - - const jsxElement = t.jsxElement(openingElement, closingElement, children) - - if (parent == null) { - return t.expressionStatement(jsxElement) - } - - return jsxElement -} +const h = { handlers } function toBabelAST(tree) { - return t.program([one(tree)]) + return one(h, tree) } export default toBabelAST diff --git a/packages/hast-util-to-babel-ast/src/one.js b/packages/hast-util-to-babel-ast/src/one.js new file mode 100644 index 00000000..f3fd0ed3 --- /dev/null +++ b/packages/hast-util-to-babel-ast/src/one.js @@ -0,0 +1,17 @@ +function one(h, node, parent) { + const type = node && node.type + const fn = h.handlers[type] + + /* Fail on non-nodes. */ + if (!type) { + throw new Error(`Expected node, got \`${node}\``) + } + + if (!fn) { + throw new Error(`Node of type ${type} is unknown`) + } + + return fn(h, node, parent) +} + +export default one diff --git a/packages/plugin-jsx/package.json b/packages/plugin-jsx/package.json index 07fabd8a..fefc8ec5 100644 --- a/packages/plugin-jsx/package.json +++ b/packages/plugin-jsx/package.json @@ -24,6 +24,6 @@ "@babel/core": "^7.4.5", "@svgr/babel-preset": "^4.3.1", "@svgr/hast-util-to-babel-ast": "^4.3.1", - "svg-parser": "^1.0.6" + "svg-parser": "^2.0.0" } } diff --git a/yarn.lock b/yarn.lock index 347df3c2..af0764c8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9869,10 +9869,10 @@ supports-color@^6.1.0: dependencies: has-flag "^3.0.0" -svg-parser@^1.0.6: - version "1.0.6" - resolved "https://registry.yarnpkg.com/svg-parser/-/svg-parser-1.0.6.tgz#fdf104b9263b30e81e4738218bcb10293a816764" - integrity sha512-EPd4xbh+MnF0eaiJgQziJHCKEWnrNEYMIuvRoiyPi/XmSO+gb65gMdGid9/JrucsQ80Y84NTChxE5FW0WxuO3w== +svg-parser@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/svg-parser/-/svg-parser-2.0.0.tgz#8252dc3252b0fcf127df22ea5d9cf3dae532ddfb" + integrity sha512-0vSo6x2Sm8c5CIkif/xGWpeDjzTzaR4F0QPjWOnK9zgiFZXYoK1Qvii+Sr464H1/RgKdI3usDH8EgTUBhAlKLw== svgo@^1.0.0, svgo@^1.0.5, svgo@^1.2.2: version "1.2.2" From 5779618ef189b86b4856679255f8d777df7f034f Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 9 Jul 2019 16:14:29 +0300 Subject: [PATCH 6/7] Remove unified --- package.json | 4 +- packages/hast-util-to-babel-ast/README.md | 8 +- yarn.lock | 111 +--------------------- 3 files changed, 4 insertions(+), 119 deletions(-) diff --git a/package.json b/package.json index 28883f6b..c828eab1 100644 --- a/package.json +++ b/package.json @@ -36,8 +36,6 @@ "eslint-plugin-react": "^7.13.0", "jest": "^24.8.0", "lerna": "^3.14.1", - "react": "^16.8.6", - "unified": "^7.1.0", - "vfile": "^4.0.1" + "react": "^16.8.6" } } diff --git a/packages/hast-util-to-babel-ast/README.md b/packages/hast-util-to-babel-ast/README.md index 32265e90..f2df68d3 100644 --- a/packages/hast-util-to-babel-ast/README.md +++ b/packages/hast-util-to-babel-ast/README.md @@ -15,14 +15,10 @@ npm install --save-dev @svgr/hast-util-to-babel-ast ## Usage ```js +import { parse } from 'svg-parser'; import toBabelAST from '@svgr/hast-util-to-babel-ast' -const hastTree = unified() - .use(parse, { - fragment: true, - space: 'svg', - }) - .parse(vfile({ path: filePath, contents: `` })) +const hastTree = parse(``) const babelTree = hastToBabelAst(hastTree) ``` diff --git a/yarn.lock b/yarn.lock index af0764c8..ac4eb4da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1941,28 +1941,6 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw== -"@types/unist@*", "@types/unist@^2.0.0", "@types/unist@^2.0.2": - version "2.0.3" - resolved "https://registry.yarnpkg.com/@types/unist/-/unist-2.0.3.tgz#9c088679876f374eb5983f150d4787aa6fb32d7e" - integrity sha512-FvUupuM3rlRsRtCN+fDudtmytGO6iHJuuRKS1Ss0pG5z8oX0diNEw94UEL7hgDbpN94rgaK5R7sWm6RrSkZuAQ== - -"@types/vfile-message@*": - version "1.0.1" - resolved "https://registry.yarnpkg.com/@types/vfile-message/-/vfile-message-1.0.1.tgz#e1e9895cc6b36c462d4244e64e6d0b6eaf65355a" - integrity sha512-mlGER3Aqmq7bqR1tTTIVHq8KSAFFRyGbrxuM8C/H82g6k7r2fS+IMEkIu3D7JHzG10NvPdR8DNx0jr0pwpp4dA== - dependencies: - "@types/node" "*" - "@types/unist" "*" - -"@types/vfile@^3.0.0": - version "3.0.2" - resolved "https://registry.yarnpkg.com/@types/vfile/-/vfile-3.0.2.tgz#19c18cd232df11ce6fa6ad80259bc86c366b09b9" - integrity sha512-b3nLFGaGkJ9rzOcuXRfHkZMdjsawuDD0ENL9fzTophtBg8FJHSGbH7daXkEpcwy3v7Xol3pAvsmlYyFhR4pqJw== - dependencies: - "@types/node" "*" - "@types/unist" "*" - "@types/vfile-message" "*" - "@types/yargs@^12.0.2", "@types/yargs@^12.0.9": version "12.0.12" resolved "https://registry.yarnpkg.com/@types/yargs/-/yargs-12.0.12.tgz#45dd1d0638e8c8f153e87d296907659296873916" @@ -2553,11 +2531,6 @@ babylon-walk@^1.0.2: babel-types "^6.15.0" lodash.clone "^4.5.0" -bail@^1.0.0: - version "1.0.4" - resolved "https://registry.yarnpkg.com/bail/-/bail-1.0.4.tgz#7181b66d508aa3055d3f6c13f0a0c720641dde9b" - integrity sha512-S8vuDB4w6YpRhICUDET3guPlQpaJl7od94tpZ0Fvnyp+MKW/HyDTcRDck+29C9g+d/qQHnddRH3+94kZdrW0Ww== - balanced-match@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" @@ -4589,7 +4562,7 @@ extend-shallow@^3.0.0, extend-shallow@^3.0.2: assign-symbols "^1.0.0" is-extendable "^1.0.1" -extend@^3.0.0, extend@~3.0.2: +extend@~3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g== @@ -5568,11 +5541,6 @@ is-buffer@^1.0.2, is-buffer@^1.1.5: resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w== -is-buffer@^2.0.0: - version "2.0.3" - resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.3.tgz#4ecf3fcf749cbd1e472689e109ac66261a25e725" - integrity sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw== - is-callable@^1.1.4: version "1.1.4" resolved "https://registry.yarnpkg.com/is-callable/-/is-callable-1.1.4.tgz#1e1adf219e1eeb684d691f9d6a05ff0d30a24d75" @@ -9003,11 +8971,6 @@ repeating@^2.0.0: dependencies: is-finite "^1.0.0" -replace-ext@1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/replace-ext/-/replace-ext-1.0.0.tgz#de63128373fcbf7c3ccfa4de5a480c45a67958eb" - integrity sha1-3mMSg3P8v3w8z6TeWkgMRaZ5WOs= - request-promise-core@1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/request-promise-core/-/request-promise-core-1.1.2.tgz#339f6aababcafdb31c799ff158700336301d3346" @@ -10154,11 +10117,6 @@ trim-right@^1.0.1: resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003" integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM= -trough@^1.0.0: - version "1.0.4" - resolved "https://registry.yarnpkg.com/trough/-/trough-1.0.4.tgz#3b52b1f13924f460c3fbfd0df69b587dbcbc762e" - integrity sha512-tdzBRDGWcI1OpPVmChbdSKhvSVurznZ8X36AYURAcl+0o2ldlCY2XPzyXNNxwJwwyIU+rIglTCG4kxtNKBQH7Q== - tslib@^1.9.0: version "1.9.3" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286" @@ -10257,20 +10215,6 @@ unicode-trie@^0.3.1: pako "^0.2.5" tiny-inflate "^1.0.0" -unified@^7.1.0: - version "7.1.0" - resolved "https://registry.yarnpkg.com/unified/-/unified-7.1.0.tgz#5032f1c1ee3364bd09da12e27fdd4a7553c7be13" - integrity sha512-lbk82UOIGuCEsZhPj8rNAkXSDXd6p0QLzIuSsCdxrqnqU56St4eyOB+AlXsVgVeRmetPTYydIuvFfpDIed8mqw== - dependencies: - "@types/unist" "^2.0.0" - "@types/vfile" "^3.0.0" - bail "^1.0.0" - extend "^3.0.0" - is-plain-obj "^1.1.0" - trough "^1.0.0" - vfile "^3.0.0" - x-is-string "^0.1.0" - union-value@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/union-value/-/union-value-1.0.0.tgz#5c71c34cb5bad5dcebe3ea0cd08207ba5aa1aea4" @@ -10305,18 +10249,6 @@ unique-slug@^2.0.0: dependencies: imurmurhash "^0.1.4" -unist-util-stringify-position@^1.0.0, unist-util-stringify-position@^1.1.1: - version "1.1.2" - resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-1.1.2.tgz#3f37fcf351279dcbca7480ab5889bb8a832ee1c6" - integrity sha512-pNCVrk64LZv1kElr0N1wPiHEUoXNVFERp+mlTg/s9R5Lwg87f9bM/3sQB99w+N9D/qnM9ar3+AKDBwo/gm/iQQ== - -unist-util-stringify-position@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-2.0.1.tgz#de2a2bc8d3febfa606652673a91455b6a36fb9f3" - integrity sha512-Zqlf6+FRI39Bah8Q6ZnNGrEHUhwJOkHde2MHVk96lLyftfJJckaPslKgzhVcviXj8KcE9UJM9F+a4JEiBUTYgA== - dependencies: - "@types/unist" "^2.0.2" - universal-user-agent@^2.0.0, universal-user-agent@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-2.1.0.tgz#5abfbcc036a1ba490cb941f8fd68c46d3669e8e4" @@ -10476,42 +10408,6 @@ verror@1.10.0: core-util-is "1.0.2" extsprintf "^1.2.0" -vfile-message@^1.0.0: - version "1.1.1" - resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-1.1.1.tgz#5833ae078a1dfa2d96e9647886cd32993ab313e1" - integrity sha512-1WmsopSGhWt5laNir+633LszXvZ+Z/lxveBf6yhGsqnQIhlhzooZae7zV6YVM1Sdkw68dtAW3ow0pOdPANugvA== - dependencies: - unist-util-stringify-position "^1.1.1" - -vfile-message@^2.0.0: - version "2.0.1" - resolved "https://registry.yarnpkg.com/vfile-message/-/vfile-message-2.0.1.tgz#951881861c22fc1eb39f873c0b93e336a64e8f6d" - integrity sha512-KtasSV+uVU7RWhUn4Lw+wW1Zl/nW8JWx7JCPps10Y9JRRIDeDXf8wfBLoOSsJLyo27DqMyAi54C6Jf/d6Kr2Bw== - dependencies: - "@types/unist" "^2.0.2" - unist-util-stringify-position "^2.0.0" - -vfile@^3.0.0: - version "3.0.1" - resolved "https://registry.yarnpkg.com/vfile/-/vfile-3.0.1.tgz#47331d2abe3282424f4a4bb6acd20a44c4121803" - integrity sha512-y7Y3gH9BsUSdD4KzHsuMaCzRjglXN0W2EcMf0gpvu6+SbsGhMje7xDc8AEoeXy6mIwCKMI6BkjMsRjzQbhMEjQ== - dependencies: - is-buffer "^2.0.0" - replace-ext "1.0.0" - unist-util-stringify-position "^1.0.0" - vfile-message "^1.0.0" - -vfile@^4.0.1: - version "4.0.1" - resolved "https://registry.yarnpkg.com/vfile/-/vfile-4.0.1.tgz#fc3d43a1c71916034216bf65926d5ee3c64ed60c" - integrity sha512-lRHFCuC4SQBFr7Uq91oJDJxlnftoTLQ7eKIpMdubhYcVMho4781a8MWXLy3qZrZ0/STD1kRiKc0cQOHm4OkPeA== - dependencies: - "@types/unist" "^2.0.0" - is-buffer "^2.0.0" - replace-ext "1.0.0" - unist-util-stringify-position "^2.0.0" - vfile-message "^2.0.0" - vlq@^0.2.2: version "0.2.3" resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.3.tgz#8f3e4328cf63b1540c0d67e1b2778386f8975b26" @@ -10735,11 +10631,6 @@ ws@^5.1.1, ws@^5.2.0: dependencies: async-limiter "~1.0.0" -x-is-string@^0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/x-is-string/-/x-is-string-0.1.0.tgz#474b50865af3a49a9c4657f05acd145458f77d82" - integrity sha1-R0tQhlrzpJqcRlfwWs0UVFj3fYI= - xml-name-validator@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-3.0.0.tgz#6ae73e06de4d8c6e47f9fb181f78d648ad457c6a" From cea6947e608d652c3872b9c88296df794c001564 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 9 Jul 2019 16:15:23 +0300 Subject: [PATCH 7/7] Remove getProps.js --- .../hast-util-to-babel-ast/src/getProps.js | 70 ------------------- 1 file changed, 70 deletions(-) delete mode 100644 packages/hast-util-to-babel-ast/src/getProps.js diff --git a/packages/hast-util-to-babel-ast/src/getProps.js b/packages/hast-util-to-babel-ast/src/getProps.js deleted file mode 100644 index 411524ed..00000000 --- a/packages/hast-util-to-babel-ast/src/getProps.js +++ /dev/null @@ -1,70 +0,0 @@ -import * as t from '@babel/types' -import { isNumeric, kebabCase, replaceSpaces } from './util' -import stringToObjectStyle from './stringToObjectStyle' -import { ATTRIBUTE_MAPPING, ELEMENT_ATTRIBUTE_MAPPING } from './mappings' - -function convertAriaAttribute(kebabKey) { - const [aria, ...parts] = kebabKey.split('-') - return `${aria}-${parts.join('').toLowerCase()}` -} - -function getKey(key, value, nodeName) { - const lowerCaseKey = key.toLowerCase() - const mappedElementAttribute = - ELEMENT_ATTRIBUTE_MAPPING[nodeName] && - ELEMENT_ATTRIBUTE_MAPPING[nodeName][lowerCaseKey] - const mappedAttribute = ATTRIBUTE_MAPPING[lowerCaseKey] - - if (mappedElementAttribute || mappedAttribute) { - return t.jsxIdentifier(mappedElementAttribute || mappedAttribute) - } - - const kebabKey = kebabCase(key) - - if (kebabKey.startsWith('aria-')) { - return t.jsxIdentifier(convertAriaAttribute(kebabKey)) - } - - if (kebabKey.startsWith('data-')) { - return t.jsxIdentifier(kebabKey) - } - - return t.jsxIdentifier(key) -} - -function getValue(key, value) { - // Handle className - if (Array.isArray(value)) { - return t.stringLiteral(replaceSpaces(value.join(' '))) - } - - if (key === 'style') { - return t.jsxExpressionContainer(stringToObjectStyle(value)) - } - - if (isNumeric(value)) { - return t.jsxExpressionContainer(t.numericLiteral(Number(value))) - } - - return t.stringLiteral(replaceSpaces(value)) -} - -const getProps = (nodeName, attributes) => { - const keys = Object.keys(attributes) - const props = [] - let index = -1 - - while (++index < keys.length) { - const key = keys[index] - const value = attributes[key] - const property = t.jsxAttribute( - getKey(key, value, nodeName), - getValue(key, value), - ) - props.push(property) - } - - return props -} - -export default getProps