Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sveltejs/language-tools
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: svelte-check-2.8.0
Choose a base ref
...
head repository: sveltejs/language-tools
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: svelte-check-2.8.1
Choose a head ref
  • 17 commits
  • 57 files changed
  • 6 contributors

Commits on Jun 27, 2022

  1. (fix) rename of $stores

    dummdidumm authored Jun 27, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    wmouchere William Mouchère
    Copy the full SHA
    b9920f6 View commit details

Commits on Jul 4, 2022

  1. (fix) better component completions in certain cases (#1546)

    The new transformation shows global completions on components/elements in some situations. This extra logic should fix that.
    dummdidumm authored Jul 4, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    wmouchere William Mouchère
    Copy the full SHA
    a686332 View commit details
  2. (fix) adjust element typings (#1547)

    for new transformation: use svelte:body instad of sveltebody
    dummdidumm authored Jul 4, 2022
    Copy the full SHA
    aa6a763 View commit details

Commits on Jul 6, 2022

  1. (feat) use SvelteComponentTyped if exists (#1548)

    Check in svelteshims if we can use SvelteComponentTyped for more aligned typings
    
    This means invalid accessor calls are no longer caught, which is edge-case enough in my opinion so the benefits outweigh the drawbacks. In Svelte 4 this could be restricted in Svelte.
    dummdidumm authored Jul 6, 2022
    Copy the full SHA
    06a2757 View commit details
  2. Copy the full SHA
    2b5d321 View commit details

Commits on Jul 11, 2022

  1. (fix) stricter check for component import quick fix (#1555)

    Prevents false positives, enables workaround for #1554
    jasonlyu123 authored Jul 11, 2022
    Copy the full SHA
    3e7c986 View commit details

Commits on Jul 12, 2022

  1. (fix) don't remove the .svelte definitions when ts-plugin is disabled (

    …#1557)
    
    #1556. Also added the encoding parameter just to be safe
    jasonlyu123 authored Jul 12, 2022
    Copy the full SHA
    0136806 View commit details

Commits on Jul 19, 2022

  1. Bump svelte from 3.48.0 to 3.49.0 (#1559)

    Bumps [svelte](https://github.com/sveltejs/svelte) from 3.48.0 to 3.49.0.
    - [Release notes](https://github.com/sveltejs/svelte/releases)
    - [Changelog](https://github.com/sveltejs/svelte/blob/master/CHANGELOG.md)
    - [Commits](sveltejs/svelte@v3.48.0...v3.49.0)
    
    ---
    updated-dependencies:
    - dependency-name: svelte
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jul 19, 2022
    Copy the full SHA
    0bb0748 View commit details
  2. (fix) auto import in workspace without tsconfig/jsconfig (#1543)

    Properly fall back to the correct directory when no tsconfig/jsconfig is found
    jasonlyu123 authored Jul 19, 2022
    Copy the full SHA
    adf82ab View commit details

Commits on Jul 20, 2022

  1. Copy the full SHA
    b8e1ef8 View commit details

Commits on Jul 25, 2022

  1. Copy the full SHA
    bb81b91 View commit details

Commits on Jul 26, 2022

  1. Copy the full SHA
    612a390 View commit details

Commits on Jul 27, 2022

  1. (fix) skip parser error when there's likely another preprocessor invo…

    …lved (#1569)
    
    #1259
    #1479
    #1558
    
    This may give false negatives for people who do not use Vite/forgot to create a svelte.config.js when they use rollup or webpack, but that's likely OK since we can think about these diagnostics as being opt-in: you add a preprocessor config when you want the Svelte warnings, too. Most people who have this problem also likely use TypeScript or a CSS preprocessor. When using TS, errors are caught through the TS intellisense. When using a CSS preprocessor, the only warning they'll miss is the unused CSS warning, which is apparent anyway since those styles are not visible in the browser.
    dummdidumm authored Jul 27, 2022
    Copy the full SHA
    5904829 View commit details

Commits on Aug 15, 2022

  1. Copy the full SHA
    db23238 View commit details

Commits on Aug 17, 2022

  1. Copy the full SHA
    232862d View commit details
  2. Copy the full SHA
    967806d View commit details
  3. (feat) better ./$types auto imports (#1592)

    TypeScript doesn't resolve imports to SvelteKit's $types very well, help it doing better
    dummdidumm authored Aug 17, 2022
    Copy the full SHA
    936fa6f View commit details
Showing with 746 additions and 254 deletions.
  1. +1 −1 docs/preprocessors/typescript.md
  2. +1 −1 packages/language-server/package.json
  3. +11 −0 packages/language-server/src/lib/documents/Document.ts
  4. +4 −1 packages/language-server/src/lib/documents/configLoader.ts
  5. +10 −0 packages/language-server/src/plugins/svelte/features/getDiagnostics.ts
  6. +12 −2 packages/language-server/src/plugins/typescript/ComponentInfoProvider.ts
  7. +5 −4 packages/language-server/src/plugins/typescript/DocumentSnapshot.ts
  8. +9 −3 packages/language-server/src/plugins/typescript/LSAndTSDocResolver.ts
  9. +3 −1 packages/language-server/src/plugins/typescript/SnapshotManager.ts
  10. +12 −1 packages/language-server/src/plugins/typescript/TypeScriptPlugin.ts
  11. +35 −8 packages/language-server/src/plugins/typescript/features/CodeActionsProvider.ts
  12. +159 −40 packages/language-server/src/plugins/typescript/features/CompletionProvider.ts
  13. +3 −2 packages/language-server/src/plugins/typescript/module-loader.ts
  14. +29 −12 packages/language-server/src/plugins/typescript/service.ts
  15. +10 −7 packages/language-server/src/plugins/typescript/svelte-sys.ts
  16. +1 −1 packages/language-server/src/server.ts
  17. +4 −0 packages/language-server/test/plugins/typescript/TypescriptPlugin.test.ts
  18. +58 −0 packages/language-server/test/plugins/typescript/features/CodeActionsProvider.test.ts
  19. +73 −0 packages/language-server/test/plugins/typescript/features/CompletionProvider.test.ts
  20. +1 −1 ...anguage-server/test/plugins/typescript/features/diagnostics/fixtures/$$events-usage/expected.json
  21. +1 −1 ...guage-server/test/plugins/typescript/features/diagnostics/fixtures/$$events-usage/expectedv2.json
  22. +0 −8 ...st/plugins/typescript/features/diagnostics/fixtures/accessors-customElement-configs/expected.json
  23. +0 −8 .../plugins/typescript/features/diagnostics/fixtures/accessors-customElement-configs/expectedv2.json
  24. +5 −1 packages/language-server/test/plugins/typescript/module-loader.test.ts
  25. +9 −92 packages/language-server/test/plugins/typescript/service.test.ts
  26. +5 −2 packages/language-server/test/plugins/typescript/svelte-sys.test.ts
  27. +155 −0 packages/language-server/test/plugins/typescript/test-utils.ts
  28. +2 −1 packages/language-server/test/plugins/typescript/testfiles/code-actions/codeactions.svelte
  29. +1 −1 .../language-server/test/plugins/typescript/testfiles/completions/component-events-completion.svelte
  30. +1 −1 packages/svelte2tsx/package.json
  31. +2 −2 packages/svelte2tsx/src/htmlxtojsx_v2/nodes/Element.ts
  32. +7 −6 packages/svelte2tsx/svelte-jsx.d.ts
  33. +8 −5 packages/svelte2tsx/svelte-shims.d.ts
  34. +1 −1 packages/svelte2tsx/test/htmlx2jsx/samples/action-svelte-body/expectedv2.js
  35. +1 −1 packages/svelte2tsx/test/htmlx2jsx/samples/binding-this-svelte-body/expectedv2.js
  36. +2 −2 packages/svelte2tsx/test/htmlx2jsx/samples/if-nested-slot-let-shadowed/expectedv2.js
  37. +4 −4 packages/svelte2tsx/test/htmlx2jsx/samples/svelte-fragment/expectedv2.js
  38. +1 −1 packages/svelte2tsx/test/htmlx2jsx/samples/sveltehead-title/expectedv2.js
  39. +2 −1 packages/svelte2tsx/test/htmlx2jsx/samples/sveltekit-anchor-attrs/expected.jsx
  40. +2 −1 packages/svelte2tsx/test/htmlx2jsx/samples/sveltekit-anchor-attrs/expectedv2.js
  41. +1 −0 packages/svelte2tsx/test/htmlx2jsx/samples/sveltekit-anchor-attrs/input.svelte
  42. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/accessors-config-attr-false/expectedv2.ts
  43. +3 −3 packages/svelte2tsx/test/svelte2tsx/samples/const-tag-component/expectedv2.ts
  44. +2 −2 packages/svelte2tsx/test/svelte2tsx/samples/event-bubble-svelte-element/expectedv2.ts
  45. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/script-inside-head-after-toplevel-script/expectedv2.ts
  46. +3 −2 packages/svelte2tsx/test/svelte2tsx/samples/svelte-element/expected.tsx
  47. +3 −2 packages/svelte2tsx/test/svelte2tsx/samples/svelte-element/expectedv2.ts
  48. +1 −0 packages/svelte2tsx/test/svelte2tsx/samples/svelte-element/input.svelte
  49. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/uses-accessors-attr-not-present/expectedv2.ts
  50. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/uses-accessors-attr-present/expectedv2.ts
  51. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/uses-accessors-mustachetag-false/expectedv2.ts
  52. +1 −1 packages/svelte2tsx/test/svelte2tsx/samples/uses-accessors-mustachetag-true/expectedv2.ts
  53. +5 −5 packages/svelte2tsx/test/svelte2tsx/samples/uses-svelte-components/expectedv2.ts
  54. +59 −2 packages/typescript-plugin/src/language-service/completions.ts
  55. +1 −0 packages/typescript-plugin/src/language-service/rename.ts
  56. +7 −3 packages/typescript-plugin/src/svelte-snapshots.ts
  57. +5 −5 yarn.lock
2 changes: 1 addition & 1 deletion docs/preprocessors/typescript.md
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ export interface FooSlots {
export default class Foo extends SvelteComponentTyped<FooProps, FooEvents, FooSlots> {}
```

SvelteKit's `package` command will give you these capabilities - transpiling and creating type definitions - out of the box: https://kit.svelte.dev/docs#packaging
SvelteKit's `package` command will give you these capabilities - transpiling and creating type definitions - out of the box: https://kit.svelte.dev/docs/packaging

## Typing component events

2 changes: 1 addition & 1 deletion packages/language-server/package.json
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@
"lodash": "^4.17.21",
"prettier": "2.5.1",
"prettier-plugin-svelte": "~2.7.0",
"svelte": "^3.48.0",
"svelte": "^3.49.0",
"svelte-preprocess": "~4.10.1",
"svelte2tsx": "~0.5.0",
"typescript": "*",
11 changes: 11 additions & 0 deletions packages/language-server/src/lib/documents/Document.ts
Original file line number Diff line number Diff line change
@@ -113,6 +113,17 @@ export class Document extends WritableDocument {
return lang.replace(/^text\//, '');
}

/**
* Returns true if there's `lang="X"` on script or style or template.
*/
hasLanguageAttribute(): boolean {
return (
!!this.getLanguageAttribute('script') ||
!!this.getLanguageAttribute('style') ||
!!this.getLanguageAttribute('template')
);
}

private addDefaultLanguage(
config: SvelteConfig | undefined,
tagInfo: TagInformation | null,
5 changes: 4 additions & 1 deletion packages/language-server/src/lib/documents/configLoader.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ export interface SvelteConfig {
compilerOptions?: CompileOptions;
preprocess?: InternalPreprocessorGroup | InternalPreprocessorGroup[];
loadConfigError?: any;
isFallbackConfig?: boolean;
kit?: any;
}

const DEFAULT_OPTIONS: CompileOptions = {
@@ -257,7 +259,8 @@ export class ConfigLoader {
transpileOnly: true,
compilerOptions: { sourceMap: true, inlineSourceMap: false }
}
})
}),
isFallbackConfig: true
};
}
}
Original file line number Diff line number Diff line change
@@ -91,6 +91,16 @@ async function createParserErrorDiagnostic(error: any, document: Document) {
document.scriptInfo || document.moduleScriptInfo
);

if (
(!document.config?.preprocess || document.config.isFallbackConfig) &&
document.hasLanguageAttribute()
) {
Logger.error(
`Parsing ${document.getFilePath()} failed. No preprocess config found but lang tag exists. Skip showing error because they likely use other preprocessors.`
);
return [];
}

if (isInStyle || isInScript) {
diagnostic.message +=
'\n\nIf you expect this syntax to work, here are some suggestions: ';
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ export type ComponentPartInfo = ReturnType<ComponentEvents['getAll']>;
export interface ComponentInfoProvider {
getEvents(): ComponentPartInfo;
getSlotLets(slot?: string): ComponentPartInfo;
getProps(propName: string): ts.CompletionEntry[];
getProps(): ComponentPartInfo;
getProp(propName: string): ts.CompletionEntry[];
}

export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
@@ -45,7 +46,16 @@ export class JsOrTsComponentInfoProvider implements ComponentInfoProvider {
return this.mapPropertiesOfType(slotLetsType);
}

getProps(propName: string): ts.CompletionEntry[] {
getProps() {
const props = this.getType('$$prop_def');
if (!props) {
return [];
}

return this.mapPropertiesOfType(props);
}

getProp(propName: string): ts.CompletionEntry[] {
const props = this.getType('$$prop_def');
if (!props) {
return [];
Original file line number Diff line number Diff line change
@@ -96,12 +96,13 @@ export namespace DocumentSnapshot {
export function fromFilePath(
filePath: string,
createDocument: (filePath: string, text: string) => Document,
options: SvelteSnapshotOptions
options: SvelteSnapshotOptions,
tsSystem: ts.System
) {
if (isSvelteFilePath(filePath)) {
return DocumentSnapshot.fromSvelteFilePath(filePath, createDocument, options);
} else {
return DocumentSnapshot.fromNonSvelteFilePath(filePath);
return DocumentSnapshot.fromNonSvelteFilePath(filePath, tsSystem);
}
}

@@ -110,7 +111,7 @@ export namespace DocumentSnapshot {
* @param filePath path to the js/ts file
* @param options options that apply in case it's a svelte file
*/
export function fromNonSvelteFilePath(filePath: string) {
export function fromNonSvelteFilePath(filePath: string, tsSystem: ts.System) {
let originalText = '';

// The following (very hacky) code makes sure that the ambient module definitions
@@ -121,7 +122,7 @@ export namespace DocumentSnapshot {
// on their own.
const normalizedPath = filePath.replace(/\\/g, '/');
if (!normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
originalText = ts.sys.readFile(filePath) || '';
originalText = tsSystem.readFile(filePath) || '';
}
if (
normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts') ||
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { dirname } from 'path';
import ts from 'typescript';
import { TextDocumentContentChangeEvent } from 'vscode-languageserver';
import { Document, DocumentManager } from '../../lib/documents';
@@ -27,6 +28,7 @@ interface LSAndTSDocResolverOptions {

onProjectReloaded?: () => void;
watchTsConfig?: boolean;
tsSystem?: ts.System;
}

export class LSAndTSDocResolver {
@@ -69,7 +71,7 @@ export class LSAndTSDocResolver {
return document;
};

private globalSnapshotsManager = new GlobalSnapshotsManager();
private globalSnapshotsManager = new GlobalSnapshotsManager(this.lsDocumentContext.tsSystem);
private extendedConfigCache = new Map<string, ts.ExtendedConfigCacheEntry>();

private get lsDocumentContext(): LanguageServiceDocumentContext {
@@ -83,7 +85,7 @@ export class LSAndTSDocResolver {
extendedConfigCache: this.extendedConfigCache,
onProjectReloaded: this.options?.onProjectReloaded,
watchTsConfig: !!this.options?.watchTsConfig,
tsSystem: ts.sys
tsSystem: this.options?.tsSystem ?? ts.sys
};
}

@@ -168,7 +170,11 @@ export class LSAndTSDocResolver {

async getTSService(filePath?: string): Promise<LanguageServiceContainer> {
if (this.options?.tsconfigPath) {
return getServiceForTsconfig(this.options?.tsconfigPath, this.lsDocumentContext);
return getServiceForTsconfig(
this.options?.tsconfigPath,
dirname(this.options.tsconfigPath),
this.lsDocumentContext
);
}
if (!filePath) {
throw new Error('Cannot call getTSService without filePath and without tsconfigPath');
Original file line number Diff line number Diff line change
@@ -16,6 +16,8 @@ export class GlobalSnapshotsManager {
private emitter = new EventEmitter();
private documents = new Map<string, DocumentSnapshot>();

constructor(private readonly tsSystem: ts.System) {}

get(fileName: string) {
fileName = normalizePath(fileName);
return this.documents.get(fileName);
@@ -48,7 +50,7 @@ export class GlobalSnapshotsManager {
this.emitter.emit('change', fileName, previousSnapshot);
return previousSnapshot;
} else {
const newSnapshot = DocumentSnapshot.fromNonSvelteFilePath(fileName);
const newSnapshot = DocumentSnapshot.fromNonSvelteFilePath(fileName, this.tsSystem);

if (previousSnapshot) {
newSnapshot.version = previousSnapshot.version + 1;
Original file line number Diff line number Diff line change
@@ -455,7 +455,18 @@ export class TypeScriptPlugin
for (const { fileName, changeType } of onWatchFileChangesParas) {
const pathParts = fileName.split(/\/|\\/);
const dirPathParts = pathParts.slice(0, pathParts.length - 1);
if (ignoredBuildDirectories.some((dir) => dirPathParts.includes(dir))) {
const declarationExtensions = [ts.Extension.Dcts, ts.Extension.Dts, ts.Extension.Dmts];
const canSafelyIgnore =
declarationExtensions.every((ext) => !fileName.endsWith(ext)) &&
ignoredBuildDirectories.some((dir) => {
const index = dirPathParts.indexOf(dir);

return (
// Files in .svelte-kit/types should always come through
index > 0 && (dir !== '.svelte-kit' || dirPathParts[index + 1] !== 'types')
);
});
if (canSafelyIgnore) {
continue;
}

Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import {
CodeAction,
CodeActionContext,
CodeActionKind,
Diagnostic,
OptionalVersionedTextDocumentIdentifier,
Range,
TextDocumentEdit,
@@ -257,8 +258,19 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const start = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.start));
const end = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.end));
const errorCodes: number[] = context.diagnostics.map((diag) => Number(diag.code));
let codeFixes = errorCodes.includes(2304) // "Cannot find name '...'."
? this.getComponentImportQuickFix(start, end, lang, tsDoc.filePath, userPreferences)
const cannotFoundNameDiagnostic = context.diagnostics.filter(
(diagnostic) => diagnostic.code === 2304
); // "Cannot find name '...'."

let codeFixes = cannotFoundNameDiagnostic.length
? this.getComponentImportQuickFix(
start,
end,
lang,
tsDoc,
userPreferences,
cannotFoundNameDiagnostic
)
: undefined;
codeFixes =
// either-or situation
@@ -373,10 +385,11 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
start: number,
end: number,
lang: ts.LanguageService,
filePath: string,
userPreferences: ts.UserPreferences
tsDoc: DocumentSnapshot,
userPreferences: ts.UserPreferences,
diagnostics: Diagnostic[]
): readonly ts.CodeFixAction[] | undefined {
const sourceFile = lang.getProgram()?.getSourceFile(filePath);
const sourceFile = lang.getProgram()?.getSourceFile(tsDoc.filePath);

if (!sourceFile) {
return;
@@ -402,9 +415,23 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
}

const tagName = ts.isIdentifier(node) ? node : node.tagName;
const tagNameEnd = tagName.getEnd();
const tagNameEndOriginalPosition = tsDoc.offsetAt(
tsDoc.getOriginalPosition(tsDoc.positionAt(tagNameEnd))
);
const hasDiagnosticForTag = diagnostics.some(
({ range }) =>
tsDoc.offsetAt(range.start) <= tagNameEndOriginalPosition &&
tagNameEndOriginalPosition <= tsDoc.offsetAt(range.end)
);

if (!hasDiagnosticForTag) {
return;
}

const completion = lang.getCompletionsAtPosition(
filePath,
tagName.getEnd(),
tsDoc.filePath,
tagNameEnd,
userPreferences
);

@@ -420,7 +447,7 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const toFix = (c: ts.CompletionEntry) =>
lang
.getCompletionEntryDetails(
filePath,
tsDoc.filePath,
end,
c.name,
{},
Loading