Skip to content

Commit

Permalink
fix(babel-plugin-component): remove import validation (#2719)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson authored Apr 14, 2022
1 parent 1e55dd9 commit 7d22e62
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 120 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

22 changes: 0 additions & 22 deletions packages/@lwc/babel-plugin-component/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand Down
16 changes: 1 addition & 15 deletions packages/@lwc/babel-plugin-component/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand Down Expand Up @@ -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);
},
Expand Down
38 changes: 10 additions & 28 deletions packages/@lwc/babel-plugin-component/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 0 additions & 22 deletions packages/@lwc/errors/src/compiler/error-info/lwc-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 7d22e62

Please sign in to comment.