123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- const ALL_IRREGULARS = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000\u2028\u2029]/u;
- const IRREGULAR_WHITESPACE = /[\f\v\u0085\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000]+/mgu;
- const IRREGULAR_LINE_TERMINATORS = /[\u2028\u2029]/mgu;
- const LINE_BREAK = astUtils.createGlobalLinebreakMatcher();
- module.exports = {
- meta: {
- type: "problem",
- docs: {
- description: "Disallow irregular whitespace",
- recommended: true,
- url: "https://eslint.org/docs/latest/rules/no-irregular-whitespace"
- },
- schema: [
- {
- type: "object",
- properties: {
- skipComments: {
- type: "boolean",
- default: false
- },
- skipStrings: {
- type: "boolean",
- default: true
- },
- skipTemplates: {
- type: "boolean",
- default: false
- },
- skipRegExps: {
- type: "boolean",
- default: false
- },
- skipJSXText: {
- type: "boolean",
- default: false
- }
- },
- additionalProperties: false
- }
- ],
- messages: {
- noIrregularWhitespace: "Irregular whitespace not allowed."
- }
- },
- create(context) {
-
- let errors = [];
-
- const options = context.options[0] || {};
- const skipComments = !!options.skipComments;
- const skipStrings = options.skipStrings !== false;
- const skipRegExps = !!options.skipRegExps;
- const skipTemplates = !!options.skipTemplates;
- const skipJSXText = !!options.skipJSXText;
- const sourceCode = context.sourceCode;
- const commentNodes = sourceCode.getAllComments();
-
- function removeWhitespaceError(node) {
- const locStart = node.loc.start;
- const locEnd = node.loc.end;
- errors = errors.filter(({ loc: { start: errorLocStart } }) => (
- errorLocStart.line < locStart.line ||
- errorLocStart.line === locStart.line && errorLocStart.column < locStart.column ||
- errorLocStart.line === locEnd.line && errorLocStart.column >= locEnd.column ||
- errorLocStart.line > locEnd.line
- ));
- }
-
- function removeInvalidNodeErrorsInLiteral(node) {
- const shouldCheckStrings = skipStrings && (typeof node.value === "string");
- const shouldCheckRegExps = skipRegExps && Boolean(node.regex);
- if (shouldCheckStrings || shouldCheckRegExps) {
-
- if (ALL_IRREGULARS.test(node.raw)) {
- removeWhitespaceError(node);
- }
- }
- }
-
- function removeInvalidNodeErrorsInTemplateLiteral(node) {
- if (typeof node.value.raw === "string") {
- if (ALL_IRREGULARS.test(node.value.raw)) {
- removeWhitespaceError(node);
- }
- }
- }
-
- function removeInvalidNodeErrorsInComment(node) {
- if (ALL_IRREGULARS.test(node.value)) {
- removeWhitespaceError(node);
- }
- }
-
- function removeInvalidNodeErrorsInJSXText(node) {
- if (ALL_IRREGULARS.test(node.raw)) {
- removeWhitespaceError(node);
- }
- }
-
- function checkForIrregularWhitespace(node) {
- const sourceLines = sourceCode.lines;
- sourceLines.forEach((sourceLine, lineIndex) => {
- const lineNumber = lineIndex + 1;
- let match;
- while ((match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null) {
- errors.push({
- node,
- messageId: "noIrregularWhitespace",
- loc: {
- start: {
- line: lineNumber,
- column: match.index
- },
- end: {
- line: lineNumber,
- column: match.index + match[0].length
- }
- }
- });
- }
- });
- }
-
- function checkForIrregularLineTerminators(node) {
- const source = sourceCode.getText(),
- sourceLines = sourceCode.lines,
- linebreaks = source.match(LINE_BREAK);
- let lastLineIndex = -1,
- match;
- while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
- const lineIndex = linebreaks.indexOf(match[0], lastLineIndex + 1) || 0;
- errors.push({
- node,
- messageId: "noIrregularWhitespace",
- loc: {
- start: {
- line: lineIndex + 1,
- column: sourceLines[lineIndex].length
- },
- end: {
- line: lineIndex + 2,
- column: 0
- }
- }
- });
- lastLineIndex = lineIndex;
- }
- }
-
- function noop() {}
- const nodes = {};
- if (ALL_IRREGULARS.test(sourceCode.getText())) {
- nodes.Program = function(node) {
-
- checkForIrregularWhitespace(node);
- checkForIrregularLineTerminators(node);
- };
- nodes.Literal = removeInvalidNodeErrorsInLiteral;
- nodes.TemplateElement = skipTemplates ? removeInvalidNodeErrorsInTemplateLiteral : noop;
- nodes.JSXText = skipJSXText ? removeInvalidNodeErrorsInJSXText : noop;
- nodes["Program:exit"] = function() {
- if (skipComments) {
-
- commentNodes.forEach(removeInvalidNodeErrorsInComment);
- }
-
- errors.forEach(error => context.report(error));
- };
- } else {
- nodes.Program = noop;
- }
- return nodes;
- }
- };
|