From ca7a3c3279581ebf85989870da9d64bb6214e61b Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 15:50:31 +0000 Subject: [PATCH 1/9] Update tests and command --- package.json | 2 +- tests/utils/get-element-type.mjs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8524b81c..82b51afe 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "lint:eslint-docs": "npm run update:eslint-docs -- --check", "lint:js": "eslint .", "pretest": "mkdir -p node_modules/ && ln -fs $(pwd) node_modules/", - "test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/", + "test": "npm run eslint-check && npm run lint && mocha tests/**/*.js tests/**/*.mjs", "update:eslint-docs": "eslint-doc-generator" }, "repository": { diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index 2d496b8a..ccaef2a4 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -63,4 +63,15 @@ describe('getElementType', function () { ]) expect(getElementType({}, node)).to.equal('Box') }) + + it('returns raw type when polymorphic prop is set to component, and not the mapped value', function () { + // + const setting = mockSetting({ + Box: 'div', + }) + + // eslint-disable-next-line no-undef + const node = mockJSXOpeningElement('Box', [mockJSXAttribute('as', Link)]) + expect(getElementType(setting, node)).to.equal('Box') + }) }) From 9deac20a7c1704a93ea7537e169c535eb5131774 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 16:14:31 +0000 Subject: [PATCH 2/9] Fix bug --- lib/utils/get-element-type.js | 15 ++++++++------- tests/utils/get-element-type.mjs | 9 +++++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/utils/get-element-type.js b/lib/utils/get-element-type.js index 16d7c07f..f4c62ff4 100644 --- a/lib/utils/get-element-type.js +++ b/lib/utils/get-element-type.js @@ -16,15 +16,16 @@ function getElementType(context, node, lazyElementCheck = false) { // check if the node contains a polymorphic prop const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as' - const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node) - // if a component configuration does not exists, return the raw element - if (!settings?.github?.components?.[rawElement]) return rawElement + const rawElement = elementType(node) - const defaultComponent = settings.github.components[rawElement] - - // check if the default component is also defined in the configuration - return defaultComponent ? defaultComponent : defaultComponent + if (getProp(node.attributes, polymorphicPropName)) { + return getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? rawElement + } else if (settings?.github?.components?.[rawElement]) { + return settings.github.components[rawElement] + } else { + return rawElement + } } module.exports = {getElementType} diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index ccaef2a4..4c061630 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -64,14 +64,15 @@ describe('getElementType', function () { expect(getElementType({}, node)).to.equal('Box') }) - it('returns raw type when polymorphic prop is set to component, and not the mapped value', function () { - // + it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () { + // const setting = mockSetting({ Box: 'div', }) - // eslint-disable-next-line no-undef - const node = mockJSXOpeningElement('Box', [mockJSXAttribute('as', Link)]) + const node = mockJSXOpeningElement('Box', [ + mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'), + ]) expect(getElementType(setting, node)).to.equal('Box') }) }) From f7d159220ac1bbb2e650f4a53269484aabb5c6e3 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 18:12:30 +0000 Subject: [PATCH 3/9] Add .js extension so tests pass --- .eslintrc.js | 1 + tests/utils/get-element-type.mjs | 8 +++----- tests/utils/get-role.mjs | 4 ++-- tests/utils/object-map.mjs | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 329ac0be..4d1c4835 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,6 +10,7 @@ module.exports = { extends: [require.resolve('./lib/configs/recommended'), 'plugin:eslint-plugin/all'], plugins: ['eslint-plugin'], rules: { + 'import/extensions': 'off', 'import/no-commonjs': 'off', 'filenames/match-regex': 'off', 'i18n-text/no-en': 'off', diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index 4c061630..059af41f 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -1,10 +1,8 @@ import {expect} from 'chai' -import {getElementType} from '../../lib/utils/get-element-type' -import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks' +import {getElementType} from '../../lib/utils/get-element-type.js' +import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js' -const mocha = require('mocha') -const describe = mocha.describe -const it = mocha.it +import {describe, it} from 'mocha' function mockSetting(componentSetting = {}) { return { diff --git a/tests/utils/get-role.mjs b/tests/utils/get-role.mjs index 24986f4e..fc206f40 100644 --- a/tests/utils/get-role.mjs +++ b/tests/utils/get-role.mjs @@ -1,6 +1,6 @@ import {expect} from 'chai' -import {getRole} from '../../lib/utils/get-role' -import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks' +import {getRole} from '../../lib/utils/get-role.js' +import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js' import {describe, it} from 'mocha' describe('getRole', function () { diff --git a/tests/utils/object-map.mjs b/tests/utils/object-map.mjs index 033e6e9b..3d7b4521 100644 --- a/tests/utils/object-map.mjs +++ b/tests/utils/object-map.mjs @@ -1,6 +1,6 @@ // @ts-check import {expect} from 'chai' -import ObjectMap from '../../lib/utils/object-map' +import ObjectMap from '../../lib/utils/object-map.js' import {describe, it} from 'mocha' describe('ObjectMap', function () { From 96000419462b4d7c60ce3d6b27e0832dd17908a2 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 18:51:34 +0000 Subject: [PATCH 4/9] fix bugs --- lib/utils/get-element-type.js | 21 +++++++++++++-------- tests/utils/get-element-type.mjs | 4 +--- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/utils/get-element-type.js b/lib/utils/get-element-type.js index f4c62ff4..41488a15 100644 --- a/lib/utils/get-element-type.js +++ b/lib/utils/get-element-type.js @@ -17,15 +17,20 @@ function getElementType(context, node, lazyElementCheck = false) { // check if the node contains a polymorphic prop const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as' - const rawElement = elementType(node) - - if (getProp(node.attributes, polymorphicPropName)) { - return getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? rawElement - } else if (settings?.github?.components?.[rawElement]) { - return settings.github.components[rawElement] - } else { - return rawElement + let checkConditionalMap = true + + const prop = getProp(node.attributes, polymorphicPropName) + const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) + if (prop && !literalPropValue) { + checkConditionalMap = false } + const rawElement = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) ?? elementType(node) + + // if a component configuration does not exists, return the raw element + if (!settings?.github?.components?.[rawElement]) return rawElement + + // check if the default component is also defined in the configuration + return checkConditionalMap ? settings.github.components[rawElement] : rawElement } module.exports = {getElementType} diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index 059af41f..de8b9b96 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -1,7 +1,6 @@ import {expect} from 'chai' import {getElementType} from '../../lib/utils/get-element-type.js' import {mockJSXAttribute, mockJSXConditionalAttribute, mockJSXOpeningElement} from './mocks.js' - import {describe, it} from 'mocha' function mockSetting(componentSetting = {}) { @@ -62,12 +61,11 @@ describe('getElementType', function () { expect(getElementType({}, node)).to.equal('Box') }) - it('returns raw type when polymorphic prop is set to component even when there is a mapped value', function () { + it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () { // const setting = mockSetting({ Box: 'div', }) - const node = mockJSXOpeningElement('Box', [ mockJSXConditionalAttribute('as', 'isNavigationOpen', 'generic', 'navigation'), ]) From e6f3c9e639d19370f07c21528277eea3fd52d09d Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 19:06:26 +0000 Subject: [PATCH 5/9] update code comment --- lib/utils/get-element-type.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/utils/get-element-type.js b/lib/utils/get-element-type.js index 41488a15..74e6fe39 100644 --- a/lib/utils/get-element-type.js +++ b/lib/utils/get-element-type.js @@ -17,10 +17,11 @@ function getElementType(context, node, lazyElementCheck = false) { // check if the node contains a polymorphic prop const polymorphicPropName = settings?.github?.polymorphicPropName ?? 'as' - let checkConditionalMap = true - const prop = getProp(node.attributes, polymorphicPropName) const literalPropValue = getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) + let checkConditionalMap = true + + // If the prop is not a literal and we cannot determine it, don't fall back to the conditional map value, if it exists if (prop && !literalPropValue) { checkConditionalMap = false } From d134206ae16fe1500900f074d4a827f20d4f2b88 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 20:35:14 +0000 Subject: [PATCH 6/9] drop node 14 --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 200cc642..a1707b55 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [14, 16, 18] + node-version: [16, 18] steps: - uses: actions/checkout@v4 From dd592c6775de194a4f50ff01837013151c6f76ae Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 20:38:26 +0000 Subject: [PATCH 7/9] drop ndoe 16 --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index a1707b55..b1d763bf 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [16, 18] + node-version: [18] steps: - uses: actions/checkout@v4 From 9d2dc5d064848bb4acbb33fc90a6937ce047a0e3 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 20:44:18 +0000 Subject: [PATCH 8/9] add node 20 support --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index b1d763bf..56d95857 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - node-version: [18] + node-version: [18, 20] steps: - uses: actions/checkout@v4 From 3979a24c9416d5266b1a27907896af9b9b6e8323 Mon Sep 17 00:00:00 2001 From: Kate Higa <16447748+khiga8@users.noreply.github.com> Date: Thu, 16 May 2024 16:45:57 -0400 Subject: [PATCH 9/9] Update tests/utils/get-element-type.mjs --- tests/utils/get-element-type.mjs | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/utils/get-element-type.mjs b/tests/utils/get-element-type.mjs index de8b9b96..80215054 100644 --- a/tests/utils/get-element-type.mjs +++ b/tests/utils/get-element-type.mjs @@ -62,7 +62,6 @@ describe('getElementType', function () { }) it('returns raw type when polymorphic prop is set to non-literal expression even with component setting', function () { - // const setting = mockSetting({ Box: 'div', })