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
8 changes: 8 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,20 @@ export default tseslint.config(
{ name: "clearImmediate" },
{ name: "performance" },
],
"local/no-direct-import": "error",
},
},
{
files: ["src/harness/**", "src/testRunner/**"],
rules: {
"no-restricted-globals": "off",
"local/no-direct-import": "off",
},
},
{
files: ["src/**/_namespaces/**"],
rules: {
"local/no-direct-import": "off",
},
},
{
Expand Down
81 changes: 81 additions & 0 deletions scripts/eslint/rules/no-direct-import.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const { createRule } = require("./utils.cjs");
const path = require("path");

/** @import { TSESTree } from "@typescript-eslint/utils" */
void 0;

module.exports = createRule({
name: "no-direct-import",
meta: {
docs: {
description: ``,
},
messages: {
noDirectImport: `This import relatively references another project; did you mean to import from a local _namespace module?`,
},
schema: [],
type: "problem",
},
defaultOptions: [],

create(context) {
const containingFileName = context.filename;
const containingDirectory = path.dirname(containingFileName);

/** @type {(node: TSESTree.ImportDeclaration | TSESTree.TSImportEqualsDeclaration) => void} */
const check = node => {
let source;
if (node.type === "TSImportEqualsDeclaration") {
const moduleReference = node.moduleReference;
if (
moduleReference.type === "TSExternalModuleReference"
&& moduleReference.expression.type === "Literal"
&& typeof moduleReference.expression.value === "string"
) {
source = moduleReference.expression;
}
}
else {
source = node.source;
}

if (!source) return;

const p = source.value;

// These appear in place of public API imports.
if (p.endsWith("../typescript/typescript.js")) return;

// The below is similar to https://github.com/microsoft/DefinitelyTyped-tools/blob/main/packages/eslint-plugin/src/rules/no-bad-reference.ts

// Any relative path that goes to the wrong place will contain "..".
if (!p.includes("..")) return;

const parts = p.split("/");
let cwd = containingDirectory;
for (const part of parts) {
if (part === "" || part === ".") {
continue;
}
if (part === "..") {
cwd = path.dirname(cwd);
}
else {
cwd = path.join(cwd, part);
}

if (path.basename(cwd) === "src") {
context.report({
messageId: "noDirectImport",
node: source,
});
}
}
};

return {
ImportDeclaration: check,
TSImportEqualsDeclaration: check,
};
},
});
175 changes: 101 additions & 74 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ const asyncGeneratorHelper: UnscopedEmitHelper = {
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
Expand Down Expand Up @@ -1102,8 +1102,8 @@ const generatorHelper: UnscopedEmitHelper = {
priority: 6,
text: `
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
Expand Down
13 changes: 9 additions & 4 deletions src/compiler/factory/nodeFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1985,10 +1985,15 @@ export function createNodeFactory(flags: NodeFactoryFlags, baseFactory: BaseNode
node.parameters = createNodeArray(parameters);
node.body = body;

node.transformFlags = propagateChildrenFlags(node.modifiers) |
propagateChildrenFlags(node.parameters) |
(propagateChildFlags(node.body) & ~TransformFlags.ContainsPossibleTopLevelAwait) |
TransformFlags.ContainsES2015;
if (!node.body) {
node.transformFlags = TransformFlags.ContainsTypeScript;
}
else {
node.transformFlags = propagateChildrenFlags(node.modifiers) |
propagateChildrenFlags(node.parameters) |
(propagateChildFlags(node.body) & ~TransformFlags.ContainsPossibleTopLevelAwait) |
TransformFlags.ContainsES2015;
}

node.typeParameters = undefined; // initialized by parser for grammar errors
node.type = undefined; // initialized by parser for grammar errors
Expand Down
Loading