Skip to content

Commit

Permalink
feat(compiler): Implement error codes system onto compiler modules (#726
Browse files Browse the repository at this point in the history
)

* feat(compiler): first pass errors for postcss

* feat(compiler): first pass errors for jest-transformer

* feat(compiler): first pass errors for babel-plugin-transform-lwc-class

* feat(compiler): first pass errors for lwc-template-compiler

* fix(compiler): first pass errors for lwc-compiler

* feat: modify generateCompilerError signature to use config object

* feat: add location and filename to lwc-template-compiler

* feat: move error wrapping for postcss to compiler boundary

* feat: move error wrapping for lwc-jest-transformer

* feat: move error wrapping for babel plugin

* feat: implement error codes for template-compiler warnings and cleanup

* feat: refactor warnOnElement in template-compiler

* feat(compiler): normalize at compiler level and update tests

* fix: add temporary adapter to prevent breakage
  • Loading branch information
jye-sf authored Oct 23, 2018
1 parent ce341b8 commit 3ee81d5
Show file tree
Hide file tree
Showing 107 changed files with 3,072 additions and 1,128 deletions.
3 changes: 2 additions & 1 deletion packages/babel-plugin-transform-lwc-class/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"dependencies": {
"@babel/helper-module-imports": "7.0.0",
"@babel/plugin-proposal-class-properties": "7.1.0"
"@babel/plugin-proposal-class-properties": "7.1.0",
"lwc-errors": "0.30.1"
},
"devDependencies": {
"common-tags": "^1.8.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Boolean public property must default to false.',
message: 'Boolean public property must default to false.',
loc: {
line: 2,
column: 9
Expand All @@ -227,7 +227,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Invalid property name "is". "is" is a reserved attribute.',
message: 'Invalid property name "is". "is" is a reserved attribute.',
loc: {
line: 2,
column: 9
Expand All @@ -242,7 +242,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Invalid property name onChangeHandler. Properties starting with "on" are reserved for event handlers.',
message: 'Invalid property name onChangeHandler. Properties starting with "on" are reserved for event handlers.',
loc: {
line: 2,
column: 9
Expand Down Expand Up @@ -278,7 +278,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Invalid property name dataFooBar. Properties starting with "data" are reserved attributes.',
message: 'Invalid property name dataFooBar. Properties starting with "data" are reserved attributes.',
loc: {
line: 2,
column: 9
Expand All @@ -293,7 +293,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Ambiguous attribute name tabindex. tabindex will never be called from template because its corresponding property is camel cased. Consider renaming to "tabIndex".',
message: 'Ambiguous attribute name tabindex. tabindex will never be called from template because its corresponding property is camel cased. Consider renaming to "tabIndex".',
loc: {
line: 2,
column: 9
Expand All @@ -308,7 +308,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Ambiguous attribute name maxlength. maxlength will never be called from template because its corresponding property is camel cased. Consider renaming to "maxLength".',
message: 'Ambiguous attribute name maxlength. maxlength will never be called from template because its corresponding property is camel cased. Consider renaming to "maxLength".',
loc: {
line: 2,
column: 9
Expand Down Expand Up @@ -344,7 +344,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Invalid property name "slot". "slot" is a reserved attribute.',
message: 'Invalid property name "slot". "slot" is a reserved attribute.',
loc: {
line: 2,
column: 9
Expand All @@ -359,7 +359,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Invalid property name part. "part" is a future reserved attribute for web components.',
message: 'Invalid property name part. "part" is a future reserved attribute for web components.',
loc: {
line: 2,
column: 9
Expand All @@ -376,7 +376,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: @api method or property cannot be used with @track',
message: '@api method or property cannot be used with @track',
loc: {
line: 2,
column: 20,
Expand All @@ -392,7 +392,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Duplicate @api property "foo".',
message: 'Duplicate @api property "foo".',
loc: {
line: 2,
column: 9
Expand All @@ -413,7 +413,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Duplicate @api property "foo".',
message: 'Duplicate @api property "foo".',
loc: {
line: 2,
column: 9
Expand All @@ -429,7 +429,7 @@ describe('Transform property', () => {
}
`, {
error: {
message: 'test.js: Duplicate @api property "foo".',
message: 'Duplicate @api property "foo".',
loc: {
line: 2,
column: 9
Expand Down Expand Up @@ -463,7 +463,7 @@ Test.publicMethods = ["foo"];`
}
`, {
error: {
message: 'test.js: @api cannot be applied to a computed property, getter, setter or method.',
message: '@api cannot be applied to a computed property, getter, setter or method.',
loc: {
line: 2,
column: 9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Element import', () => {
import engine from 'lwc';
`, {
error: {
message: `test.js: Invalid import. "lwc" doesn't have default export.`,
message: `Invalid import. "lwc" doesn't have default export.`,
loc: {
line: 1,
column: 7,
Expand All @@ -20,7 +20,7 @@ describe('Element import', () => {
export default class extends engine.LightningElement {}
`, {
error: {
message: `test.js: Invalid import. Namespace imports are not allowed on "lwc", instead use named imports "import { LightningElement } from 'lwc'".`,
message: `Invalid import. Namespace imports are not allowed on "lwc", instead use named imports "import { LightningElement } from 'lwc'".`,
loc: {
line: 1,
column: 7,
Expand All @@ -34,7 +34,7 @@ describe('Element import', () => {
export default class extends LightningElement {}
`, {
error: {
message: `test.js: LWC component class can't be an anonymous.`,
message: `LWC component class can't be an anonymous.`,
loc: {
line: 3,
column: 15
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('observedAttributes array', () => {
}
`, {
error: {
message: `test.js: Invalid static property "observedAttributes". "observedAttributes" cannot be used to track attribute changes. Define setters for "foo", "title", "tabIndex" instead.`,
message: `Invalid static property "observedAttributes". "observedAttributes" cannot be used to track attribute changes. Define setters for "foo", "title", "tabIndex" instead.`,
loc: {
line: 1,
column: 7,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('decorators', () => {
}
`, {
error: {
message: 'test.js: Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"',
message: 'Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"',
loc: {
line: 10,
column: 0
Expand All @@ -24,7 +24,7 @@ describe('decorators', () => {
}
`, {
error: {
message: 'test.js: Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"',
message: 'Invalid decorator usage. Supported decorators (api, wire, track) should be imported from "lwc"',
loc: {
line: 10,
column: 0
Expand All @@ -37,7 +37,7 @@ describe('decorators', () => {
track(true)
`, {
error: {
message: 'test.js: "track" can only be used as a class decorator',
message: '"track" can only be used as a class decorator',
loc: {
line: 10,
column: 0
Expand All @@ -53,7 +53,7 @@ describe('decorators', () => {
}
`, {
error: {
message: 'test.js: "track" can only be used as a class decorator',
message: '"track" can only be used as a class decorator',
loc: {
line: 10,
column: 0
Expand All @@ -67,7 +67,7 @@ describe('decorators', () => {
class foo {}
`, {
error: {
message: 'test.js: "@track" can only be applied on class properties',
message: '"@track" can only be applied on class properties',
loc: {
line: 10,
column: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Test.track = {
}
`, {
error: {
message: 'test.js: @track decorator can only be applied to class properties.',
message: '@track decorator can only be applied to class properties.',
loc: {
line: 2,
column: 11
Expand All @@ -74,7 +74,7 @@ Test.track = {
}
`, {
error: {
message: 'test.js: @track decorator can only be applied to class properties.',
message: '@track decorator can only be applied to class properties.',
loc: {
line: 4,
column: 11
Expand All @@ -93,7 +93,7 @@ Test.track = {
}
`, {
error: {
message: 'test.js: @track decorator can only be applied to class properties.',
message: '@track decorator can only be applied to class properties.',
loc: {
line: 4,
column: 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire expects an adapter as first parameter. @wire(adapter: WireAdapter, config?: any).',
message: '@wire expects an adapter as first parameter. @wire(adapter: WireAdapter, config?: any).',
loc: {
line: 2,
column: 4,
Expand Down Expand Up @@ -198,7 +198,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire expects a function identifier to be imported as first parameter.',
message: '@wire expects a function identifier to be imported as first parameter.',
loc: {
line: 4,
column: 6,
Expand All @@ -214,7 +214,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire expects a configuration object expression as second parameter.',
message: '@wire expects a configuration object expression as second parameter.',
loc: {
line: 2,
column: 20,
Expand All @@ -232,7 +232,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire method or property cannot be used with @api',
message: '@wire method or property cannot be used with @api',
loc: {
line: 2,
column: 20,
Expand All @@ -250,7 +250,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire method or property cannot be used with @track',
message: '@wire method or property cannot be used with @track',
loc: {
line: 2,
column: 20,
Expand All @@ -268,7 +268,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: Method or property can only have 1 @wire decorator',
message: 'Method or property can only have 1 @wire decorator',
loc: {
line: 2,
column: 20,
Expand Down Expand Up @@ -359,7 +359,7 @@ Test.wire = {
}
`, {
error: {
message: 'test.js: @wire method or property cannot be used with @api',
message: '@wire method or property cannot be used with @api',
loc: {
line: 2,
column: 20,
Expand Down
16 changes: 9 additions & 7 deletions packages/babel-plugin-transform-lwc-class/src/component.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { basename } = require('path');
const moduleImports = require("@babel/helper-module-imports");
const { findClassMethod, getEngineImportSpecifiers, isComponentClass, isDefaultExport } = require('./utils');
const { findClassMethod, generateError, getEngineImportSpecifiers, isComponentClass, isDefaultExport } = require('./utils');
const { GLOBAL_ATTRIBUTE_MAP, LWC_PACKAGE_EXPORTS, LWC_COMPONENT_PROPERTIES } = require('./constants');
const { LWCClassErrors } = require('lwc-errors');
const CLASS_PROPERTY_OBSERVED_ATTRIBUTES = 'observedAttributes';

module.exports = function ({ types: t }) {
Expand All @@ -23,9 +24,10 @@ module.exports = function ({ types: t }) {
const { propName = value } = (GLOBAL_ATTRIBUTE_MAP.get(value) || {});
return `"${propName}"`;
});
throw path.buildCodeFrameError(
`Invalid static property "observedAttributes". "observedAttributes" cannot be used to track attribute changes. Define setters for ${observedAttributeNames.join(', ')} instead.`
);
throw generateError(path, {
errorInfo: LWCClassErrors.INVALID_STATIC_OBSERVEDATTRIBUTES,
messageArgs: [observedAttributeNames.join(', ')]
});
}
},
Class(path, state) {
Expand All @@ -34,9 +36,9 @@ module.exports = function ({ types: t }) {
if (isComponent) {
const classRef = path.node.id;
if (!classRef) {
throw path.buildCodeFrameError(
`LWC component class can't be an anonymous.`
);
throw generateError(path, {
errorInfo: LWCClassErrors.LWC_CLASS_CANNOT_BE_ANONYMOUS
});
}

if (isDefaultExport(path)) {
Expand Down
Loading

0 comments on commit 3ee81d5

Please sign in to comment.