From 7c9087ac8dd3e0f3cde39df56c92c37290626333 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Wed, 2 Mar 2022 10:53:58 -0800 Subject: [PATCH] fix(babel-plugin-component): remove import validation --- .../actual.js | 1 - .../error.json | 9 ----- .../actual.js | 2 - .../error.json | 9 ----- .../actual.js | 3 -- .../error.json | 9 ----- .../babel-plugin-component/src/constants.js | 22 ----------- .../@lwc/babel-plugin-component/src/index.js | 16 +------- .../@lwc/babel-plugin-component/src/utils.js | 38 +++++-------------- .../src/compiler/error-info/lwc-class.ts | 22 ----------- 10 files changed, 11 insertions(+), 120 deletions(-) delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/actual.js delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/error.json delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/actual.js delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/error.json delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/actual.js delete mode 100644 packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/error.json diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/actual.js b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/actual.js deleted file mode 100644 index 8e49de93d2..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/actual.js +++ /dev/null @@ -1 +0,0 @@ -import engine from "lwc"; diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/error.json b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/error.json deleted file mode 100644 index dac020cf10..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-default-import-on-lwc/error.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "message": "LWC1089: Invalid import. \"lwc\" doesn't have default export.", - "loc": { - "line": 1, - "column": 7, - "start": 7, - "length": 6 - } -} \ No newline at end of file diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/actual.js b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/actual.js deleted file mode 100644 index 50640acf39..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -import * as engine from "lwc"; -export default class extends engine.LightningElement {} diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/error.json b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/error.json deleted file mode 100644 index abb5eb4fd1..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-if-using-namespace-import-on-lwc/error.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "message": "LWC1090: Invalid import. Namespace imports are not allowed on \"lwc\", instead use named imports \"import { LightningElement } from 'lwc'\".", - "loc": { - "line": 1, - "column": 7, - "start": 7, - "length": 11 - } -} \ No newline at end of file diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/actual.js b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/actual.js deleted file mode 100644 index eaed862f7c..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/actual.js +++ /dev/null @@ -1,3 +0,0 @@ -import { registerTemplate } from "lwc"; -import tmpl from "./localTemplate.html"; -registerTemplate(tmpl); diff --git a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/error.json b/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/error.json deleted file mode 100644 index 372b8ed5bf..0000000000 --- a/packages/@lwc/babel-plugin-component/src/__tests__/fixtures/component/throws-when-a-non-supported-lwc-api-is-imported/error.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "message": "LWC1091: Invalid import. \"registerTemplate\" is not part of the lwc api.", - "loc": { - "line": 1, - "column": 0, - "start": 0, - "length": 0 - } -} \ No newline at end of file diff --git a/packages/@lwc/babel-plugin-component/src/constants.js b/packages/@lwc/babel-plugin-component/src/constants.js index 3c301c1b4c..02dc47c228 100644 --- a/packages/@lwc/babel-plugin-component/src/constants.js +++ b/packages/@lwc/babel-plugin-component/src/constants.js @@ -34,27 +34,6 @@ const LWC_PACKAGE_EXPORTS = { WIRE_DECORATOR: 'wire', }; -const LWC_SUPPORTED_APIS = new Set([ - // From "@lwc/engine-core" - ...Object.values(LWC_PACKAGE_EXPORTS), - 'getComponentDef', - 'getComponentConstructor', - 'isComponentConstructor', - 'createContextProvider', - 'readonly', - 'register', - 'setFeatureFlagForTest', - 'unwrap', - - // From "@lwc/engine-dom" - 'hydrateComponent', - 'buildCustomElementConstructor', - 'createElement', - - // From "@lwc/engine-server" - 'renderComponent', -]); - const LWC_COMPONENT_PROPERTIES = { PUBLIC_PROPS: 'publicProps', PUBLIC_METHODS: 'publicMethods', @@ -79,7 +58,6 @@ module.exports = { DISALLOWED_PROP_SET, LWC_PACKAGE_ALIAS, LWC_PACKAGE_EXPORTS, - LWC_SUPPORTED_APIS, LWC_COMPONENT_PROPERTIES, REGISTER_COMPONENT_ID, REGISTER_DECORATORS_ID, diff --git a/packages/@lwc/babel-plugin-component/src/index.js b/packages/@lwc/babel-plugin-component/src/index.js index 8b93efa9c0..9bff08d6a9 100644 --- a/packages/@lwc/babel-plugin-component/src/index.js +++ b/packages/@lwc/babel-plugin-component/src/index.js @@ -4,10 +4,7 @@ * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -const { LWCClassErrors } = require('@lwc/errors'); - const component = require('./component'); -const { LWC_SUPPORTED_APIS } = require('./constants'); const { decorators, removeImportedDecoratorSpecifiers, @@ -16,7 +13,7 @@ const { const dedupeImports = require('./dedupe-imports'); const dynamicImports = require('./dynamic-imports'); const compilerVersionNumber = require('./compiler-version-number'); -const { generateError, getEngineImportSpecifiers } = require('./utils'); +const { getEngineImportSpecifiers } = require('./utils'); /** * The transform is done in 2 passes: @@ -44,17 +41,6 @@ module.exports = function LwcClassTransform(api) { enter(path) { const engineImportSpecifiers = getEngineImportSpecifiers(path); - // Validate what is imported from 'lwc'. This validation will eventually be moved out from the compiler - // and into a lint rule. - engineImportSpecifiers.forEach(({ name }) => { - if (!LWC_SUPPORTED_APIS.has(name)) { - throw generateError(path, { - errorInfo: LWCClassErrors.INVALID_IMPORT_PROHIBITED_API, - messageArgs: [name], - }); - } - }); - // Validate the usage of LWC decorators. validateImportedLwcDecoratorUsage(engineImportSpecifiers); }, diff --git a/packages/@lwc/babel-plugin-component/src/utils.js b/packages/@lwc/babel-plugin-component/src/utils.js index 04bf2f8652..a209f2ea47 100644 --- a/packages/@lwc/babel-plugin-component/src/utils.js +++ b/packages/@lwc/babel-plugin-component/src/utils.js @@ -4,10 +4,10 @@ * SPDX-License-Identifier: MIT * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT */ -const { LWCClassErrors, generateErrorMessage } = require('@lwc/errors'); +const { generateErrorMessage } = require('@lwc/errors'); const lineColumn = require('line-column'); -const { LWC_PACKAGE_ALIAS, LWC_PACKAGE_EXPORTS } = require('./constants'); +const { LWC_PACKAGE_ALIAS } = require('./constants'); function isClassMethod(classMethod, properties = {}) { const { kind = 'method', name } = properties; @@ -51,34 +51,16 @@ function getEngineImportsStatements(path) { function getEngineImportSpecifiers(path) { const imports = getEngineImportsStatements(path); - - return imports - .reduce((acc, importStatement) => { + return ( + imports // Flat-map the specifier list for each import statement - return [...acc, ...importStatement.get('specifiers')]; - }, []) - .reduce((acc, specifier) => { - // Validate engine import specifier - if (specifier.isImportNamespaceSpecifier()) { - throw generateError(specifier, { - errorInfo: LWCClassErrors.INVALID_IMPORT_NAMESPACE_IMPORTS_NOT_ALLOWED, - messageArgs: [ - LWC_PACKAGE_ALIAS, - LWC_PACKAGE_EXPORTS.BASE_COMPONENT, - LWC_PACKAGE_ALIAS, - ], - }); - } else if (specifier.isImportDefaultSpecifier()) { - throw generateError(specifier, { - errorInfo: LWCClassErrors.INVALID_IMPORT_MISSING_DEFAULT_EXPORT, - messageArgs: [LWC_PACKAGE_ALIAS], - }); - } - + .flatMap((importStatement) => importStatement.get('specifiers')) // Get the list of specifiers with their name - const imported = specifier.get('imported').node.name; - return [...acc, { name: imported, path: specifier }]; - }, []); + .map((specifier) => { + const imported = specifier.get('imported').node.name; + return { name: imported, path: specifier }; + }) + ); } function normalizeFilename(source) { diff --git a/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts b/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts index fc1b991826..0b83106078 100644 --- a/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts +++ b/packages/@lwc/errors/src/compiler/error-info/lwc-class.ts @@ -18,28 +18,6 @@ export const LWCClassErrors = { 'Invalid import. The argument "{0}" must be a stringLiteral for dynamic imports when strict mode is enabled.', url: '', }, - - INVALID_IMPORT_MISSING_DEFAULT_EXPORT: { - code: 1089, - message: 'Invalid import. "{0}" doesn\'t have default export.', - level: DiagnosticLevel.Error, - url: '', - }, - - INVALID_IMPORT_NAMESPACE_IMPORTS_NOT_ALLOWED: { - code: 1090, - message: - 'Invalid import. Namespace imports are not allowed on "{0}", instead use named imports "import { {1} } from \'{2}\'".', - level: DiagnosticLevel.Error, - url: '', - }, - - INVALID_IMPORT_PROHIBITED_API: { - code: 1091, - message: 'Invalid import. "{0}" is not part of the lwc api.', - level: DiagnosticLevel.Error, - url: '', - }, }; export const DecoratorErrors = {