-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: automatically updating tsconfig.json
's paths
mapping for core workspaces.
#587
Open
rui-rayqiu
wants to merge
13
commits into
forcedotcom:main
Choose a base branch
from
rui-rayqiu:rui/tsconfig-update
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a263140
feat: add auto updating for tsconfig on core
rui-rayqiu 71a258e
feat: initial implementation
rui-rayqiu b8ac916
feat: add tsconfig paths indexer
rui-rayqiu 74d6b2f
feat: remove deleted path mappings
rui-rayqiu 488d037
test: add test for context
rui-rayqiu adde3fb
test: add basic tests for updating tsconfig
rui-rayqiu c6a2cf5
test: add more tests
rui-rayqiu e5a0b7c
test: add more test
rui-rayqiu 09deead
fix: minor fix on comments
rui-rayqiu 2c81bdb
fix: minor comment fix
rui-rayqiu 8eec70e
fix: simplify logic and add more tests
rui-rayqiu bdd4218
fix: attempt to fix tests on Windows
rui-rayqiu 2fb8f07
fix: fix Windows file path
rui-rayqiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
280 changes: 280 additions & 0 deletions
280
packages/lwc-language-server/src/__tests__/typescript.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
import * as path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import { shared } from '@salesforce/lightning-lsp-common'; | ||
import { readAsTextDocument } from './test-utils'; | ||
import TSConfigPathIndexer from '../typescript/tsconfig-path-indexer'; | ||
import { collectImportsForDocument } from '../typescript/imports'; | ||
import { TextDocument } from 'vscode-languageserver-textdocument'; | ||
|
||
const { WorkspaceType } = shared; | ||
const TEST_WORKSPACE_PARENT_DIR = path.resolve('../..'); | ||
const CORE_ROOT = path.resolve(TEST_WORKSPACE_PARENT_DIR, 'test-workspaces', 'core-like-workspace', 'coreTS', 'core'); | ||
|
||
const tsConfigForce = path.resolve(CORE_ROOT, 'ui-force-components', 'tsconfig.json'); | ||
const tsConfigGlobal = path.resolve(CORE_ROOT, 'ui-global-components', 'tsconfig.json'); | ||
|
||
function readTSConfigFile(tsconfigPath: string): object { | ||
if (!fs.pathExistsSync(tsconfigPath)) { | ||
return null; | ||
} | ||
return JSON.parse(fs.readFileSync(tsconfigPath, 'utf8')); | ||
} | ||
|
||
function restoreTSConfigFiles(): void { | ||
const tsconfig = { | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: {}, | ||
}, | ||
}; | ||
const tsconfigPaths = [tsConfigForce, tsConfigGlobal]; | ||
for (const tsconfigPath of tsconfigPaths) { | ||
fs.writeJSONSync(tsconfigPath, tsconfig, { | ||
spaces: 4, | ||
}); | ||
} | ||
} | ||
|
||
function createTextDocumentFromString(content: string, uri?: string): TextDocument { | ||
return TextDocument.create(uri ? uri : 'mockUri', 'typescript', 0, content); | ||
} | ||
|
||
beforeEach(async () => { | ||
restoreTSConfigFiles(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
restoreTSConfigFiles(); | ||
}); | ||
|
||
describe('TSConfigPathIndexer', () => { | ||
describe('new', () => { | ||
it('initializes with the root of a core root dir', () => { | ||
const expectedPath: string = path.resolve('../../test-workspaces/core-like-workspace/coreTS/core'); | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
expect(tsconfigPathIndexer.coreModulesWithTSConfig.length).toEqual(2); | ||
expect(tsconfigPathIndexer.coreModulesWithTSConfig[0]).toEqual(path.resolve(expectedPath, 'ui-force-components')); | ||
expect(tsconfigPathIndexer.coreModulesWithTSConfig[1]).toEqual(path.resolve(expectedPath, 'ui-global-components')); | ||
expect(tsconfigPathIndexer.workspaceType).toEqual(WorkspaceType.CORE_ALL); | ||
expect(tsconfigPathIndexer.coreRoot).toEqual(expectedPath); | ||
}); | ||
|
||
it('initializes with the root of a core project dir', () => { | ||
const expectedPath: string = path.resolve('../../test-workspaces/core-like-workspace/coreTS/core'); | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([path.resolve(CORE_ROOT, 'ui-force-components')]); | ||
expect(tsconfigPathIndexer.coreModulesWithTSConfig.length).toEqual(1); | ||
expect(tsconfigPathIndexer.coreModulesWithTSConfig[0]).toEqual(path.resolve(expectedPath, 'ui-force-components')); | ||
expect(tsconfigPathIndexer.workspaceType).toEqual(WorkspaceType.CORE_PARTIAL); | ||
expect(tsconfigPathIndexer.coreRoot).toEqual(expectedPath); | ||
}); | ||
}); | ||
|
||
describe('instance methods', () => { | ||
describe('init', () => { | ||
it('no-op on sfdx workspace root', async () => { | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([path.resolve(TEST_WORKSPACE_PARENT_DIR, 'test-workspaces', 'sfdx-workspace')]); | ||
const spy = jest.spyOn(tsconfigPathIndexer, 'componentEntries', 'get'); | ||
await tsconfigPathIndexer.init(); | ||
expect(spy).not.toHaveBeenCalled(); | ||
expect(tsconfigPathIndexer.coreRoot).toBeUndefined(); | ||
}); | ||
|
||
it('generates paths mappings for all modules on core', async () => { | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
}, | ||
}, | ||
}); | ||
const tsConfigGlobalObj = readTSConfigFile(tsConfigGlobal); | ||
expect(tsConfigGlobalObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'one/app-nav-bar': ['./modules/one/app-nav-bar/app-nav-bar'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('removes paths mapping for deleted module on core', async () => { | ||
const oldTSConfig = { | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'force/deleted': ['./modules/force/deleted/deleted'], | ||
'one/deleted': ['../ui-global-components/modules/one/deleted/deleted'], | ||
}, | ||
}, | ||
}; | ||
fs.writeJSONSync(tsConfigForce, oldTSConfig, { | ||
spaces: 4, | ||
}); | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('keep existing path mapping for any js cmp', async () => { | ||
const oldTSConfig = { | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'force/input-phone-js': ['./modules/force/input-phone-js/input-phone-js'], | ||
}, | ||
}, | ||
}; | ||
fs.writeJSONSync(tsConfigForce, oldTSConfig, { | ||
spaces: 4, | ||
}); | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
'force/input-phone-js': ['./modules/force/input-phone-js/input-phone-js'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('update existing path mapping for cross-namespace cmp', async () => { | ||
const oldTSConfig = { | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'one/app-nav-bar': ['../ui-global-components/modules/one/deletedOldPath/deletedOldPath'], | ||
}, | ||
}, | ||
}; | ||
fs.writeJSONSync(tsConfigForce, oldTSConfig, { | ||
spaces: 4, | ||
}); | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
'one/app-nav-bar': ['../ui-global-components/modules/one/app-nav-bar/app-nav-bar'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('updateTSConfigFileForDocument', () => { | ||
it('no-op on sfdx workspace root', async () => { | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([path.resolve(TEST_WORKSPACE_PARENT_DIR, 'test-workspaces', 'sfdx-workspace')]); | ||
await tsconfigPathIndexer.init(); | ||
const filePath = path.resolve(CORE_ROOT, 'ui-force-components', 'modules', 'force', 'input-phone', 'input-phone.ts'); | ||
const spy = jest.spyOn(tsconfigPathIndexer as any, 'addNewPathMapping'); | ||
await tsconfigPathIndexer.updateTSConfigFileForDocument(readAsTextDocument(filePath)); | ||
expect(spy).not.toHaveBeenCalled(); | ||
expect(tsconfigPathIndexer.coreRoot).toBeUndefined(); | ||
}); | ||
|
||
it('updates tsconfig for all imports', async () => { | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const filePath = path.resolve(CORE_ROOT, 'ui-force-components', 'modules', 'force', 'input-phone', 'input-phone.ts'); | ||
await tsconfigPathIndexer.updateTSConfigFileForDocument(readAsTextDocument(filePath)); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'one/app-nav-bar': ['../ui-global-components/modules/one/app-nav-bar/app-nav-bar'], | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('do not update tsconfig for import that is not found', async () => { | ||
const tsconfigPathIndexer = new TSConfigPathIndexer([CORE_ROOT]); | ||
await tsconfigPathIndexer.init(); | ||
const fileContent = ` | ||
import { util } from 'ns/notFound'; | ||
`; | ||
const filePath = path.resolve(CORE_ROOT, 'ui-force-components', 'modules', 'force', 'input-phone', 'input-phone.ts'); | ||
await tsconfigPathIndexer.updateTSConfigFileForDocument(createTextDocumentFromString(fileContent, filePath)); | ||
const tsConfigForceObj = readTSConfigFile(tsConfigForce); | ||
expect(tsConfigForceObj).toEqual({ | ||
extends: '../tsconfig.json', | ||
compilerOptions: { | ||
paths: { | ||
'clients/context-library-lwc': ['./modules/clients/context-library-lwc/context-library-lwc'], | ||
'force/input-phone': ['./modules/force/input-phone/input-phone'], | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('imports', () => { | ||
describe('collectImportsForDocument', () => { | ||
it('should exclude special imports', async () => { | ||
const document = createTextDocumentFromString(` | ||
import {api} from 'lwc'; | ||
import {obj1} from './abc'; | ||
import {obj2} from '../xyz'; | ||
import {obj3} from 'lightning/confirm'; | ||
import {obj4} from '@salesforce/label/x'; | ||
import {obj5} from 'x.html'; | ||
import {obj6} from 'y.css'; | ||
import {obj7} from 'namespace/cmpName'; | ||
`); | ||
const imports = await collectImportsForDocument(document); | ||
expect(imports.size).toEqual(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
expect(imports.has('namespace/cmpName')); | ||
}); | ||
|
||
it('should work for partial file content', async () => { | ||
const document = createTextDocumentFromString(` | ||
import from | ||
`); | ||
const imports = await collectImportsForDocument(document); | ||
expect(imports.size).toEqual(0); | ||
}); | ||
|
||
it('dynamic imports', async () => { | ||
const document = createTextDocumentFromString(` | ||
const { | ||
default: myDefault, | ||
foo, | ||
bar, | ||
} = await import("force/wireUtils"); | ||
`); | ||
const imports = await collectImportsForDocument(document); | ||
expect(imports.size).toEqual(1); | ||
expect(imports.has('force/wireUtils')); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I understand why this is outside of the namespace root.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is to test that if a ts file that is outside of the VSCode workspace is modified, we do not update tsconfig.json. Here the context workspace root is
CORE_PROJECT_ROOT
which isui-global-components
so if a file inui-force-components
is modified the return value ofisLWCTypeScript
should be false. I'll add an inline comment to explain this.