Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,24 +194,25 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const start = fragment.offsetAt(fragment.getGeneratedPosition(range.start));
const end = fragment.offsetAt(fragment.getGeneratedPosition(range.end));
const errorCodes: number[] = context.diagnostics.map((diag) => Number(diag.code));
const codeFixes = lang.getCodeFixesAtPosition(
tsDoc.filePath,
start,
end,
errorCodes,
{},
userPreferences
);

const componentQuickFix = errorCodes.includes(2304) // "Cannot find name '...'."
? this.getComponentImportQuickFix(start, end, lang, tsDoc.filePath, userPreferences) ??
[]
: [];
let codeFixes = errorCodes.includes(2304) // "Cannot find name '...'."
? this.getComponentImportQuickFix(start, end, lang, tsDoc.filePath, userPreferences)
: undefined;
codeFixes =
// either-or situation
codeFixes ||
lang.getCodeFixesAtPosition(
tsDoc.filePath,
start,
end,
errorCodes,
{},
userPreferences
);

const docs = new SnapshotFragmentMap(this.lsAndTsDocResolver);
docs.set(tsDoc.filePath, { fragment, snapshot: tsDoc });

const codeActionsPromises = codeFixes.concat(componentQuickFix).map(async (fix) => {
const codeActionsPromises = codeFixes.map(async (fix) => {
const documentChangesPromises = fix.changes.map(async (change) => {
const { snapshot, fragment } = await docs.retrieve(change.fileName);
return TextDocumentEdit.create(
Expand Down Expand Up @@ -311,7 +312,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
lang: ts.LanguageService,
filePath: string,
userPreferences: ts.UserPreferences
): ts.CodeFixAction[] | undefined {
): readonly ts.CodeFixAction[] | undefined {
const sourceFile = lang.getProgram()?.getSourceFile(filePath);

if (!sourceFile) {
Expand All @@ -326,7 +327,10 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
},
(node): node is ts.JsxOpeningLikeElement | ts.JsxClosingElement | ts.Identifier =>
this.configManager.getConfig().svelte.useNewTransformation
? ts.isNewExpression(node.parent) && ts.isIdentifier(node)
? ts.isCallExpression(node.parent) &&
ts.isIdentifier(node.parent.expression) &&
node.parent.expression.text === '__sveltets_2_ensureComponent' &&
ts.isIdentifier(node)
: ts.isJsxClosingElement(node) || ts.isJsxOpeningLikeElement(node)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,16 @@ function isNoJsxCannotHaveMultipleAttrsError(diagnostic: Diagnostic) {
* Some diagnostics have JSX-specific nomenclature. Enhance them for more clarity.
*/
function enhanceIfNecessary(diagnostic: Diagnostic): Diagnostic {
if (diagnostic.code === DiagnosticCode.CANNOT_BE_USED_AS_JSX_COMPONENT) {
if (
diagnostic.code === DiagnosticCode.CANNOT_BE_USED_AS_JSX_COMPONENT ||
(diagnostic.code === DiagnosticCode.TYPE_X_NOT_ASSIGNABLE_TO_TYPE_Y &&
diagnostic.message.includes('Svelte2TsxComponent'))
) {
return {
...diagnostic,
message:
'Type definitions are missing for this Svelte Component. ' +
// eslint-disable-next-line max-len
"It needs a class definition with at least the property '$$prop_def' which should contain a map of input property definitions.\n" +
'Example:\n' +
' class ComponentName { $$prop_def: { propertyName: string; } }\n' +
'If you are using Svelte 3.31+, use SvelteComponentTyped:\n' +
'If you are using Svelte 3.31+, use SvelteComponentTyped to add a definition:\n' +
' import type { SvelteComponentTyped } from "svelte";\n' +
' class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}\n\n' +
'Underlying error:\n' +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"range": {
"start": { "line": 13, "character": 1 },
"end": { "line": 13, "character": 11 }
},
"severity": 1,
"source": "ts",
"message": "Type definitions are missing for this Svelte Component. If you are using Svelte 3.31+, use SvelteComponentTyped to add a definition:\n import type { SvelteComponentTyped } from \"svelte\";\n class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}\n\nUnderlying error:\n'DoesntWork' cannot be used as a JSX component.\n Its instance type 'DoesntWork' is not a valid JSX element.\n Property '$$prop_def' is missing in type 'DoesntWork' but required in type 'ElementClass'.",
"code": 2786,
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"range": {
"start": { "line": 13, "character": 1 },
"end": { "line": 13, "character": 11 }
},
"severity": 1,
"source": "ts",
"message": "Type definitions are missing for this Svelte Component. If you are using Svelte 3.31+, use SvelteComponentTyped to add a definition:\n import type { SvelteComponentTyped } from \"svelte\";\n class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}\n\nUnderlying error:\nArgument of type 'typeof DoesntWork' is not assignable to parameter of type 'new (args: { target: any; props?: any; }) => Svelte2TsxComponent<any, any, any>'.\n Type 'DoesntWork' is missing the following properties from type 'Svelte2TsxComponent<any, any, any>': $$prop_def, $$events_def, $$slot_def, $on, and 5 more.",
"code": 2345,
"tags": []
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>hi</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script lang="ts">
import { SvelteComponentTyped } from "svelte";
import Imported from './imported.svelte';

class Works extends SvelteComponentTyped<any, any, any> {}
class DoesntWork {}
</script>

<!-- valid -->
<Works />
<Imported />

<!-- invalid -->
<DoesntWork />
36 changes: 18 additions & 18 deletions packages/svelte2tsx/src/htmlxtojsx_v2/nodes/InlineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,32 @@ export class InlineComponent {
this.addNameConstDeclaration = () =>
(this.startTransformation[0] = `{ const ${this._name} = __sveltets_2_createComponentAny({`);
this.startEndTransformation.push('});');
} else if (this.node.name === 'svelte:component') {
this._name = '$$_sveltecomponent' + this.computeDepth();
this.startTransformation.push(
`{ const ${this._name}_ = new `,
[this.node.expression.start, this.node.expression.end],
'({ target: __sveltets_2_any(), props: {'
);
} else {
const isSvelteComponentTag = this.node.name === 'svelte:component';
// We don't know if the thing we use to create the Svelte component with
// is actually a proper Svelte component, which would lead to errors
// when accessing things like $$prop_def. Therefore widen the type
// here, falling back to a any-typed component to ensure the user doesn't
// get weird follup-errors all over the place. The diagnostic error
// should still error on the new X() part.
this.startEndTransformation.push(`}});${this._name}_;`);
this.addNameConstDeclaration = () =>
(this.startEndTransformation[0] = `}});const ${this._name} = __sveltets_2_typeAsComponent(${this._name}_);`);
} else {
this._name = '$$_' + this.node.name + this.computeDepth();
const nodeNameStart = this.str.original.indexOf(this.node.name, this.node.start);
// will be on the __sveltets_2_ensureComponent part, giving a more helpful message
this._name =
'$$_' +
(isSvelteComponentTag ? 'sveltecomponent' : this.node.name) +
this.computeDepth();
const constructorName = this._name + 'C';
const nodeNameStart = isSvelteComponentTag
? this.node.expression.start
: this.str.original.indexOf(this.node.name, this.node.start);
const nodeNameEnd = isSvelteComponentTag
? this.node.expression.end
: nodeNameStart + this.node.name.length;
this.startTransformation.push(
'{ new ',
[nodeNameStart, nodeNameStart + this.node.name.length],
'({ target: __sveltets_2_any(), props: {'
`{ const ${constructorName} = __sveltets_2_ensureComponent(`,
[nodeNameStart, nodeNameEnd],
`); new ${constructorName}({ target: __sveltets_2_any(), props: {`
);
this.addNameConstDeclaration = () =>
(this.startTransformation[0] = `{ const ${this._name} = new `);
(this.startTransformation[2] = `); const ${this._name} = new ${constructorName}({ target: __sveltets_2_any(), props: {`);
this.startEndTransformation.push('}});');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/svelte-shims.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,6 @@ declare function __sveltets_2_ensureTransition(transitionCall: __sveltets_2_Svel

declare function __sveltets_2_ensureType<T>(type: AConstructorTypeOf<T>, el: T): {};

declare function __sveltets_2_typeAsComponent<Component>(c: Component): Component extends Svelte2TsxComponent ? Component : Svelte2TsxComponent;
declare function __sveltets_2_ensureComponent<T extends new (args: {target: any, props?: any}) => Svelte2TsxComponent<any, any, any>>(type: T): T extends never ? Svelte2TsxComponent<any, any, any> : T;

declare function __sveltets_2_ensureArray<T extends ArrayLike<unknown>>(array: T): T extends ArrayLike<infer U> ? U[] : any[];

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{ new Hello({ target: __sveltets_2_any(), props: { "|-wtf":"foo",}}); Hello}
{ const $$_Hello0C = __sveltets_2_ensureComponent(Hello); new $$_Hello0C({ target: __sveltets_2_any(), props: { "|-wtf":"foo",}}); Hello}
{ svelteHTML.createElement("div", { "--custom-prop":"foo",}); }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ const $$_Component0 = new Component({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,thing:{ a },} = $$_Component0.$$slot_def.default;$$_$$;
{ const $$_Component0C = __sveltets_2_ensureComponent(Component); const $$_Component0 = new $$_Component0C({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,thing:{ a },} = $$_Component0.$$slot_def.default;$$_$$;
{ svelteHTML.createElement("h1", {}); a ; }
}Component}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ const $$_Component0 = new Component({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,name:n,thing,} = $$_Component0.$$slot_def.default;$$_$$;
{ const $$_Component0C = __sveltets_2_ensureComponent(Component); const $$_Component0 = new $$_Component0C({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,name:n,thing,} = $$_Component0.$$slot_def.default;$$_$$;
{ svelteHTML.createElement("h1", {}); thing; n; }
}Component}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ new Component({ target: __sveltets_2_any(), props: {}});
{ const $$_Component0C = __sveltets_2_ensureComponent(Component); new $$_Component0C({ target: __sveltets_2_any(), props: {}});
{ svelteHTML.createElement("h1", {}); }
Component}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ const $$_Component0 = new Component({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,var:new_var,} = $$_Component0.$$slot_def.default;$$_$$;
{ const $$_Component0C = __sveltets_2_ensureComponent(Component); const $$_Component0 = new $$_Component0C({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,var:new_var,} = $$_Component0.$$slot_def.default;$$_$$;
{ svelteHTML.createElement("h1", {}); new_var; }
{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,slotvar:newvar,} = $$_Component0.$$slot_def["someslot"];$$_$$;{ svelteHTML.createElement("div", { });newvar;
{ svelteHTML.createElement("h2", {}); newvar; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{ const $$_Parent0 = new Parent({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,foo,bar:baz,} = $$_Parent0.$$slot_def.default;$$_$$;
{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,bla,} = $$_Parent0.$$slot_def["named"];$$_$$;{ new Component({ target: __sveltets_2_any(), props: { }});
{ const $$_Parent0C = __sveltets_2_ensureComponent(Parent); const $$_Parent0 = new $$_Parent0C({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,foo,bar:baz,} = $$_Parent0.$$slot_def.default;$$_$$;
{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,bla,} = $$_Parent0.$$slot_def["named"];$$_$$;{ const $$_Component1C = __sveltets_2_ensureComponent(Component); new $$_Component1C({ target: __sveltets_2_any(), props: { }});
foo; baz; bla;
}Component}
{ const $$_Component1 = new Component({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,blubb,} = $$_Component1.$$slot_def.default;$$_$$;
{ const $$_Component1C = __sveltets_2_ensureComponent(Component); const $$_Component1 = new $$_Component1C({ target: __sveltets_2_any(), props: { }});{const {/*Ωignore_startΩ*/$$_$$/*Ωignore_endΩ*/,blubb,} = $$_Component1.$$slot_def.default;$$_$$;
blubb;
}Component}
}Parent}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading