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

Add @ts-ignore-enable @ts-ignore-disable #40267

Closed
wants to merge 3 commits into from
Closed
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
66 changes: 64 additions & 2 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ namespace ts {
/**
* Test for whether a single line comment's text contains a directive.
*/
const commentDirectiveRegExSingleLine = /^\s*\/\/\/?\s*@(ts-expect-error|ts-ignore)/;
const commentDirectiveRegExSingleLine = /^\s*\/\/\/?\s*@(ts-expect-error|ts-ignore(?=($|[^-]))|ts-ignore-enable|ts-ignore-disable)/;

/**
* Test for whether a multi-line comment's last line contains a directive.
Expand Down Expand Up @@ -951,7 +951,63 @@ namespace ts {
isIdentifier: () => token === SyntaxKind.Identifier || token > SyntaxKind.LastReservedWord,
isReservedWord: () => token >= SyntaxKind.FirstReservedWord && token <= SyntaxKind.LastReservedWord,
isUnterminated: () => (tokenFlags & TokenFlags.Unterminated) !== 0,
getCommentDirectives: () => commentDirectives,
getCommentDirectives: () => {
// Or we could not handle the condition here, rather than like createCommentDirectivesMap.
// Put here, we could reduce couple
// Put there, we could reduce `magic`
let isIgnoreStart = false;
if (commentDirectives) {
// It is obvious the code time could be improved, but I leave it here for reading and understanding.

const removedIndex = new Set();
commentDirectives.forEach((d, index) => {
switch (d.type) {
case CommentDirectiveType.IgnoreStart:
if (isIgnoreStart) removedIndex.add(index);
else isIgnoreStart = true;
break;
case CommentDirectiveType.IgnoreEnd:
if (!isIgnoreStart) removedIndex.add(index);
else isIgnoreStart = false;
break;
default:
// this line make expect-error not work.
if (isIgnoreStart) removedIndex.add(index);
break;
}
});
const cleanInput = commentDirectives.filter((_, i) => !removedIndex.has(i));

let curRegionEnd = end;
for (let i = cleanInput.length - 1; i >= 0; i--) {
if (cleanInput[i].type === CommentDirectiveType.IgnoreEnd) {
curRegionEnd = cleanInput[i].range.pos - 1;
}
if (cleanInput[i].type === CommentDirectiveType.IgnoreStart) {
const added = [];
let tmpPos = cleanInput[i].range.pos;
let tmpLineStart = tmpPos;
let lastOne;
while (tmpPos < curRegionEnd) {
if (isLineBreak(text.charCodeAt(tmpPos))) {
if (tmpLineStart !== tmpPos && lastOne) {
added.push(lastOne);
}
lastOne = {
range: { pos: tmpLineStart, end: tmpPos },
type: CommentDirectiveType.Ignore,
};
tmpLineStart = tmpPos + 1;
}
tmpPos++;
}
cleanInput.splice(i, curRegionEnd === end ? 1 : 2, ...added);
}
}
return cleanInput;
}
return commentDirectives;
},
getNumericLiteralFlags: () => tokenFlags & TokenFlags.NumericLiteralFlags,
getTokenFlags: () => tokenFlags,
reScanGreaterToken,
Expand Down Expand Up @@ -2180,6 +2236,12 @@ namespace ts {

case "ts-ignore":
return CommentDirectiveType.Ignore;

case "ts-ignore-enable":
return CommentDirectiveType.IgnoreStart;

case "ts-ignore-disable":
return CommentDirectiveType.IgnoreEnd;
}

return undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3482,6 +3482,10 @@ namespace ts {
export const enum CommentDirectiveType {
ExpectError,
Ignore,

// this pair would be replaced by CommentDirectiveType.Ignore when anything would be returned from scanner.
IgnoreStart,
IgnoreEnd
}

/*@internal*/
Expand Down
22 changes: 22 additions & 0 deletions tests/baselines/reference/ts-ignore-enable.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
tests/cases/conformance/directives/ts-ignore-enable.ts(11,5): error TS2322: Type 'string' is not assignable to type 'number'.


==== tests/cases/conformance/directives/ts-ignore-enable.ts (1 errors) ====
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy: number = 'nope';

// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy: string = 'nope';

var invalidCommentedPlain: number = 'nope';

// @ts-ignore-disable

var invalidPlain: number = 'nope';
~~~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.

var validCommentedPlain: string = 'nope';

var validPlain: string = 'nope';

28 changes: 28 additions & 0 deletions tests/baselines/reference/ts-ignore-enable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [ts-ignore-enable.ts]
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy: number = 'nope';

// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy: string = 'nope';

var invalidCommentedPlain: number = 'nope';

// @ts-ignore-disable

var invalidPlain: number = 'nope';

var validCommentedPlain: string = 'nope';

var validPlain: string = 'nope';


//// [ts-ignore-enable.js]
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy = 'nope';
// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy = 'nope';
var invalidCommentedPlain = 'nope';
// @ts-ignore-disable
var invalidPlain = 'nope';
var validCommentedPlain = 'nope';
var validPlain = 'nope';
23 changes: 23 additions & 0 deletions tests/baselines/reference/ts-ignore-enable.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/conformance/directives/ts-ignore-enable.ts ===
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy: number = 'nope';
>invalidCommentedFancy : Symbol(invalidCommentedFancy, Decl(ts-ignore-enable.ts, 1, 3))

// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy: string = 'nope';
>validCommentedFancy : Symbol(validCommentedFancy, Decl(ts-ignore-enable.ts, 4, 3))

var invalidCommentedPlain: number = 'nope';
>invalidCommentedPlain : Symbol(invalidCommentedPlain, Decl(ts-ignore-enable.ts, 6, 3))

// @ts-ignore-disable

var invalidPlain: number = 'nope';
>invalidPlain : Symbol(invalidPlain, Decl(ts-ignore-enable.ts, 10, 3))

var validCommentedPlain: string = 'nope';
>validCommentedPlain : Symbol(validCommentedPlain, Decl(ts-ignore-enable.ts, 12, 3))

var validPlain: string = 'nope';
>validPlain : Symbol(validPlain, Decl(ts-ignore-enable.ts, 14, 3))

29 changes: 29 additions & 0 deletions tests/baselines/reference/ts-ignore-enable.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/conformance/directives/ts-ignore-enable.ts ===
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy: number = 'nope';
>invalidCommentedFancy : number
>'nope' : "nope"

// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy: string = 'nope';
>validCommentedFancy : string
>'nope' : "nope"

var invalidCommentedPlain: number = 'nope';
>invalidCommentedPlain : number
>'nope' : "nope"

// @ts-ignore-disable

var invalidPlain: number = 'nope';
>invalidPlain : number
>'nope' : "nope"

var validCommentedPlain: string = 'nope';
>validCommentedPlain : string
>'nope' : "nope"

var validPlain: string = 'nope';
>validPlain : string
>'nope' : "nope"

15 changes: 15 additions & 0 deletions tests/cases/conformance/directives/ts-ignore-enable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-ignore-enable with additional commenting
var invalidCommentedFancy: number = 'nope';

// @ts-expect-error this line would be overwritten by @ts-ignore-enable
var validCommentedFancy: string = 'nope';

var invalidCommentedPlain: number = 'nope';

// @ts-ignore-disable

var invalidPlain: number = 'nope';

var validCommentedPlain: string = 'nope';

var validPlain: string = 'nope';