Skip to content
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

Implement #1240: implicitly reference "node" types and implement fallback resolution for bundled types #1257

Merged
merged 1 commit into from Mar 1, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -118,6 +118,7 @@
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.1.0",
"@types/source-map-support": "^0.5.0",
"@yarnpkg/fslib": "^2.4.0",
"ava": "^3.15.0",
"axios": "^0.21.1",
"chai": "^4.0.1",
Expand All @@ -139,6 +140,7 @@
"peerDependencies": {
"@swc/core": ">=1.2.45",
"@swc/wasm": ">=1.2.45",
"@types/node": "*",
"typescript": ">=2.7"
},
"peerDependenciesMeta": {
Expand Down
36 changes: 36 additions & 0 deletions src/index.spec.ts
Expand Up @@ -12,6 +12,7 @@ import semver = require('semver');
import ts = require('typescript');
import proxyquire = require('proxyquire');
import type * as tsNodeTypes from './index';
import * as fs from 'fs';
import {
unlinkSync,
existsSync,
Expand All @@ -21,6 +22,7 @@ import {
copyFileSync,
writeFileSync,
} from 'fs';
import { NodeFS, npath } from '@yarnpkg/fslib';
import * as promisify from 'util.promisify';
import { sync as rimrafSync } from 'rimraf';
import type _createRequire from 'create-require';
Expand All @@ -31,6 +33,8 @@ import { PassThrough } from 'stream';
import * as getStream from 'get-stream';
import { once } from 'lodash';

const xfs = new NodeFS(fs);

type TestExecReturn = {
stdout: string;
stderr: string;
Expand Down Expand Up @@ -715,6 +719,38 @@ test.suite('ts-node', (test) => {
);
});
}
test('implicitly loads @types/node even when not installed within local directory', async ({
context: { tempDir },
}) => {
const { err, stdout, stderr } = await exec(
`${BIN_PATH} -pe process.env.foo`,
{
cwd: tempDir,
env: { ...process.env, foo: 'hello world' },
}
);
expect(err).to.equal(null);
expect(stdout).to.equal('hello world\n');
});
test('implicitly loads local @types/node', async ({
context: { tempDir },
}) => {
await xfs.copyPromise(
npath.toPortablePath(tempDir),
npath.toPortablePath(join(TEST_DIR, 'local-types-node'))
);
const { err, stdout, stderr } = await exec(
`${BIN_PATH} -pe process.env.foo`,
{
cwd: tempDir,
env: { ...process.env, foo: 'hello world' },
}
);
expect(err).to.not.equal(null);
expect(stderr).to.contain(
"Property 'env' does not exist on type 'LocalNodeTypes_Process'"
);
});
}
);

Expand Down
29 changes: 27 additions & 2 deletions src/index.ts
Expand Up @@ -855,7 +855,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
): (_ts.ResolvedTypeReferenceDirective | undefined)[] => {
// Note: seems to be called with empty typeDirectiveNames array for all files.
return typeDirectiveNames.map((typeDirectiveName) => {
const {
let {
resolvedTypeReferenceDirective,
} = ts.resolveTypeReferenceDirective(
typeDirectiveName,
Expand All @@ -864,6 +864,28 @@ export function create(rawOptions: CreateOptions = {}): Service {
serviceHost,
redirectedReference
);
if (typeDirectiveName === 'node' && !resolvedTypeReferenceDirective) {
// Resolve @types/node relative to project first, then __dirname (copy logic from elsewhere / refactor into reusable function)
const typesNodePackageJsonPath = require.resolve(
'@types/node/package.json',
{
paths: [configFilePath ?? cwd, __dirname],
}
);
const typeRoots = [resolve(typesNodePackageJsonPath, '../..')];
({
resolvedTypeReferenceDirective,
} = ts.resolveTypeReferenceDirective(
typeDirectiveName,
containingFile,
{
...config.options,
typeRoots,
},
serviceHost,
redirectedReference
));
}
if (resolvedTypeReferenceDirective) {
fixupResolvedModule(resolvedTypeReferenceDirective);
}
Expand Down Expand Up @@ -1504,7 +1526,10 @@ function readConfig(
const skipDefaultCompilerOptions = configFilePath != null;
const defaultCompilerOptionsForNodeVersion = skipDefaultCompilerOptions
? undefined
: getDefaultTsconfigJsonForNodeVersion(ts).compilerOptions;
: {
...getDefaultTsconfigJsonForNodeVersion(ts).compilerOptions,
types: ['node'],
};

// Merge compilerOptions from all sources
config.compilerOptions = Object.assign(
Expand Down
4 changes: 4 additions & 0 deletions tests/local-types-node/node_modules/@types/node/index.d.ts

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

3 changes: 3 additions & 0 deletions tests/local-types-node/node_modules/@types/node/package.json

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