-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into shoppingJSONValidation
- Loading branch information
Showing
83 changed files
with
1,089 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
build-system/babel-plugins/babel-plugin-deep-pure/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const pureFnName = 'pure'; | ||
const pureFnImportSourceRe = new RegExp( | ||
'(^#|/)core/types/pure(\\.{js,jsx,ts,tsx})?$' | ||
); | ||
|
||
/** | ||
* Deeply adds #__PURE__ comments to an expression passed to a function named | ||
* `pure()`. | ||
* @param {import('@babel/core')} babel | ||
* @return {import('@babel/core').PluginObj} | ||
*/ | ||
module.exports = function (babel) { | ||
const {types: t} = babel; | ||
|
||
/** | ||
* @param {import('@babel/core').NodePath} path | ||
* @return {boolean} | ||
*/ | ||
function referencesPureFnImport(path) { | ||
if (!path.isIdentifier()) { | ||
return false; | ||
} | ||
const binding = path.scope.getBinding(path.node.name); | ||
if (!binding || binding.kind !== 'module') { | ||
return false; | ||
} | ||
const bindingPath = binding.path; | ||
const parent = bindingPath.parentPath; | ||
if ( | ||
!parent?.isImportDeclaration() || | ||
!pureFnImportSourceRe.test(parent?.node.source.value) | ||
) { | ||
return false; | ||
} | ||
return ( | ||
bindingPath.isImportSpecifier() && | ||
t.isIdentifier(bindingPath.node.imported, {name: pureFnName}) | ||
); | ||
} | ||
|
||
/** | ||
* @param {import('@babel/core').NodePath<import('@babel/types').CallExpression>} path | ||
* @return {boolean} | ||
*/ | ||
function isPureFnCallExpression(path) { | ||
return ( | ||
path.node.arguments.length === 1 && | ||
referencesPureFnImport(path.get('callee')) | ||
); | ||
} | ||
|
||
/** @param {import('@babel/core').NodePath} path */ | ||
function addPureComment(path) { | ||
path.addComment('leading', ' #__PURE__ '); | ||
} | ||
|
||
/** @param {import('@babel/core').NodePath<import('@babel/types').CallExpression>} path */ | ||
function replaceWithFirstArgument(path) { | ||
path.replaceWith(path.node.arguments[0]); | ||
} | ||
|
||
return { | ||
name: 'deep-pure', | ||
visitor: { | ||
CallExpression(path) { | ||
if (isPureFnCallExpression(path)) { | ||
path.traverse({ | ||
NewExpression(path) { | ||
addPureComment(path); | ||
}, | ||
CallExpression(path) { | ||
if (isPureFnCallExpression(path)) { | ||
replaceWithFirstArgument(path); | ||
} else { | ||
addPureComment(path); | ||
} | ||
}, | ||
MemberExpression(path) { | ||
throw path.buildCodeFrameError( | ||
`${pureFnName}() expressions cannot contain member expressions` | ||
); | ||
}, | ||
}); | ||
replaceWithFirstArgument(path); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
2 changes: 2 additions & 0 deletions
2
...-plugins/babel-plugin-deep-pure/test/fixtures/transform/error-member-expression/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import {pure} from '#core/types/pure'; | ||
pure(error.memberExpression); |
6 changes: 6 additions & 0 deletions
6
...ugins/babel-plugin-deep-pure/test/fixtures/transform/error-member-expression/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
], | ||
"throws": "pure() expressions cannot contain member expressions" | ||
} |
4 changes: 4 additions & 0 deletions
4
...el-plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-import-source/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {pure} from 'something other than core/types/pure'; | ||
const ignored = pure( | ||
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo') | ||
); |
5 changes: 5 additions & 0 deletions
5
...plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-import-source/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
] | ||
} |
2 changes: 2 additions & 0 deletions
2
...l-plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-import-source/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { pure } from 'something other than core/types/pure'; | ||
const ignored = pure(foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo')); |
4 changes: 4 additions & 0 deletions
4
...babel-plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-not-import/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const pure = (v) => v; | ||
const ignored = pure( | ||
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo') | ||
); |
5 changes: 5 additions & 0 deletions
5
...el-plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-not-import/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
] | ||
} |
3 changes: 3 additions & 0 deletions
3
...abel-plugins/babel-plugin-deep-pure/test/fixtures/transform/ignored-not-import/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const pure = v => v; | ||
|
||
const ignored = pure(foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo')); |
6 changes: 6 additions & 0 deletions
6
...l-plugins/babel-plugin-deep-pure/test/fixtures/transform/transform-aliased-path/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {pure} from '#core/types/pure'; | ||
const ignored = foo(); | ||
const a = pure( | ||
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo') | ||
); | ||
const b = pure('foo'); |
5 changes: 5 additions & 0 deletions
5
...lugins/babel-plugin-deep-pure/test/fixtures/transform/transform-aliased-path/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
...-plugins/babel-plugin-deep-pure/test/fixtures/transform/transform-aliased-path/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { pure } from '#core/types/pure'; | ||
const ignored = foo(); | ||
const a = /* #__PURE__ */foo() || /* #__PURE__ */new Bar() || /* #__PURE__ */foo('bar', /* #__PURE__ */bar(), /* #__PURE__ */new Baz()) || 'foo'; | ||
const b = 'foo'; |
6 changes: 6 additions & 0 deletions
6
...-plugins/babel-plugin-deep-pure/test/fixtures/transform/transform-relative-path/input.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import {pure} from '../../../../../../../src/core/types/pure'; | ||
const ignored = foo(); | ||
const a = pure( | ||
foo() || new Bar() || pure(pure(foo('bar', bar(), new Baz())) || 'foo') | ||
); | ||
const b = pure('foo'); |
5 changes: 5 additions & 0 deletions
5
...ugins/babel-plugin-deep-pure/test/fixtures/transform/transform-relative-path/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"plugins": [ | ||
"../../../.." | ||
] | ||
} |
4 changes: 4 additions & 0 deletions
4
...plugins/babel-plugin-deep-pure/test/fixtures/transform/transform-relative-path/output.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { pure } from '../../../../../../../src/core/types/pure'; | ||
const ignored = foo(); | ||
const a = /* #__PURE__ */foo() || /* #__PURE__ */new Bar() || /* #__PURE__ */foo('bar', /* #__PURE__ */bar(), /* #__PURE__ */new Baz()) || 'foo'; | ||
const b = 'foo'; |
3 changes: 3 additions & 0 deletions
3
build-system/babel-plugins/babel-plugin-deep-pure/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const runner = require('@babel/helper-plugin-test-runner').default; | ||
|
||
runner(__dirname); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.