Skip to content

Commit

Permalink
var => const, let
Browse files Browse the repository at this point in the history
  • Loading branch information
puzrin committed Nov 28, 2023
1 parent 6eddd25 commit 88b8669
Show file tree
Hide file tree
Showing 63 changed files with 830 additions and 927 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ ignorePatterns:
- benchmark/extra/

rules:
no-var: 2
prefer-const: 2
no-const-assign: 2
accessor-pairs: 2
array-bracket-spacing: [ 2, "always", { "singleValue": true, "objectsInArrays": true, "arraysInArrays": true } ]
block-scoped-var: 2
Expand Down
20 changes: 10 additions & 10 deletions benchmark/benchmark.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);

var fs = require('fs');
var util = require('util');
var Benchmark = require('benchmark');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
const fs = require('fs');
const util = require('util');
const Benchmark = require('benchmark');
const ansi = require('ansi');
const cursor = ansi(process.stdout);

var IMPLS = [];
const IMPLS = [];

for (const name of fs.readdirSync(new URL('./implementations', import.meta.url)).sort()) {
const filepath = new URL(`./implementations/${name}/index.mjs`, import.meta.url);
Expand All @@ -31,11 +31,11 @@ fs.readdirSync(new URL('./samples', import.meta.url)).sort().forEach(sample => {

content.string = fs.readFileSync(filepath, 'utf8');

var title = `(${content.string.length} bytes)`;
const title = `(${content.string.length} bytes)`;

function onComplete() { cursor.write('\n'); }

var suite = new Benchmark.Suite(
const suite = new Benchmark.Suite(
title,
{
onStart: () => { console.log('\nSample: %s %s', sample, title); },
Expand Down Expand Up @@ -68,7 +68,7 @@ fs.readdirSync(new URL('./samples', import.meta.url)).sort().forEach(sample => {


function select(patterns) {
var result = [];
const result = [];

if (!(patterns instanceof Array)) {
patterns = [ patterns ];
Expand All @@ -91,7 +91,7 @@ function select(patterns) {


function run(files) {
var selected = select(files);
const selected = select(files);

if (selected.length > 0) {
console.log('Selected samples: (%d of %d)', selected.length, SAMPLES.length);
Expand Down
4 changes: 2 additions & 2 deletions benchmark/implementations/commonmark-reference/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const require = createRequire(import.meta.url);

const commonmark = require('../../extra/lib/node_modules/commonmark');

var parser = new commonmark.Parser();
var renderer = new commonmark.HtmlRenderer();
const parser = new commonmark.Parser();
const renderer = new commonmark.HtmlRenderer();

export function run(data) {
return renderer.render(parser.parse(data));
Expand Down
4 changes: 2 additions & 2 deletions benchmark/implementations/current-commonmark/index.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import markdownit from '../../../index.mjs';

var md = markdownit('commonmark');
const md = markdownit('commonmark');

// Replace normalizers to more primitive, for more "honest" compare.
// Default ones can cause 1.5x slowdown.
var encode = md.utils.lib.mdurl.encode;
const encode = md.utils.lib.mdurl.encode;

md.normalizeLink = function (url) { return encode(url); };
md.normalizeLinkText = function (str) { return str; };
Expand Down
2 changes: 1 addition & 1 deletion benchmark/implementations/current/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import markdownit from '../../../index.mjs';

var md = markdownit({
const md = markdownit({
html: true,
linkify: true,
typographer: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const require = createRequire(import.meta.url);

const markdownit = require('../../extra/lib/node_modules/markdown-it');

var md = markdownit('commonmark');
const md = markdownit('commonmark');

export function run(data) {
return md.render(data);
Expand Down
6 changes: 3 additions & 3 deletions benchmark/profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import { readFileSync } from 'fs';
import markdownit from '../index.mjs';

var md = markdownit({
const md = markdownit({
html: true,
linkify: false,
typographer: false
});

var data = readFileSync(new URL('../test/fixtures/commonmark/spec.txt', import.meta.url), 'utf8');
const data = readFileSync(new URL('../test/fixtures/commonmark/spec.txt', import.meta.url), 'utf8');

for (var i = 0; i < 20; i++) {
for (let i = 0; i < 20; i++) {
md.render(data);
}
10 changes: 5 additions & 5 deletions bin/markdown-it.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import markdownit from '../index.mjs';

////////////////////////////////////////////////////////////////////////////////

var cli = new argparse.ArgumentParser({
const cli = new argparse.ArgumentParser({
prog: 'markdown-it',
add_help: true
});
Expand Down Expand Up @@ -48,13 +48,13 @@ cli.add_argument('-o', '--output', {
default: '-'
});

var options = cli.parse_args();
const options = cli.parse_args();


function readFile(filename, encoding, callback) {
if (options.file === '-') {
// read from stdin
var chunks = [];
const chunks = [];

process.stdin.on('data', function (chunk) { chunks.push(chunk); });

Expand All @@ -70,7 +70,7 @@ function readFile(filename, encoding, callback) {
////////////////////////////////////////////////////////////////////////////////

readFile(options.file, 'utf8', function (err, input) {
var output, md;
let output;

if (err) {
if (err.code === 'ENOENT') {
Expand All @@ -86,7 +86,7 @@ readFile(options.file, 'utf8', function (err, input) {
process.exit(1);
}

md = markdownit({
const md = markdownit({
html: !options.no_html,
xhtmlOut: false,
typographer: options.typographer,
Expand Down
28 changes: 14 additions & 14 deletions lib/common/html_re.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
// Regexps to match html elements

var attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*';
const attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*';

var unquoted = '[^"\'=<>`\\x00-\\x20]+';
var single_quoted = "'[^']*'";
var double_quoted = '"[^"]*"';
const unquoted = '[^"\'=<>`\\x00-\\x20]+';
const single_quoted = "'[^']*'";
const double_quoted = '"[^"]*"';

var attr_value = '(?:' + unquoted + '|' + single_quoted + '|' + double_quoted + ')';
const attr_value = '(?:' + unquoted + '|' + single_quoted + '|' + double_quoted + ')';

var attribute = '(?:\\s+' + attr_name + '(?:\\s*=\\s*' + attr_value + ')?)';
const attribute = '(?:\\s+' + attr_name + '(?:\\s*=\\s*' + attr_value + ')?)';

var open_tag = '<[A-Za-z][A-Za-z0-9\\-]*' + attribute + '*\\s*\\/?>';
const open_tag = '<[A-Za-z][A-Za-z0-9\\-]*' + attribute + '*\\s*\\/?>';

var close_tag = '<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>';
var comment = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->';
var processing = '<[?][\\s\\S]*?[?]>';
var declaration = '<![A-Z]+\\s+[^>]*>';
var cdata = '<!\\[CDATA\\[[\\s\\S]*?\\]\\]>';
const close_tag = '<\\/[A-Za-z][A-Za-z0-9\\-]*\\s*>';
const comment = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->';
const processing = '<[?][\\s\\S]*?[?]>';
const declaration = '<![A-Z]+\\s+[^>]*>';
const cdata = '<!\\[CDATA\\[[\\s\\S]*?\\]\\]>';

var HTML_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + '|' + comment +
const HTML_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + '|' + comment +
'|' + processing + '|' + declaration + '|' + cdata + ')');
var HTML_OPEN_CLOSE_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + ')');
const HTML_OPEN_CLOSE_TAG_RE = new RegExp('^(?:' + open_tag + '|' + close_tag + ')');

export { HTML_TAG_RE, HTML_OPEN_CLOSE_TAG_RE };
30 changes: 14 additions & 16 deletions lib/common/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function _class(obj) { return Object.prototype.toString.call(obj); }

function isString(obj) { return _class(obj) === '[object String]'; }

var _hasOwnProperty = Object.prototype.hasOwnProperty;
const _hasOwnProperty = Object.prototype.hasOwnProperty;

function has(object, key) {
return _hasOwnProperty.call(object, key);
Expand All @@ -19,7 +19,7 @@ function has(object, key) {
// Merge objects
//
function assign(obj /*from1, from2, from3, ...*/) {
var sources = Array.prototype.slice.call(arguments, 1);
const sources = Array.prototype.slice.call(arguments, 1);

sources.forEach(function (source) {
if (!source) { return; }
Expand Down Expand Up @@ -65,27 +65,25 @@ function fromCodePoint(c) {
/*eslint no-bitwise:0*/
if (c > 0xffff) {
c -= 0x10000;
var surrogate1 = 0xd800 + (c >> 10),
surrogate2 = 0xdc00 + (c & 0x3ff);
const surrogate1 = 0xd800 + (c >> 10),
surrogate2 = 0xdc00 + (c & 0x3ff);

return String.fromCharCode(surrogate1, surrogate2);
}
return String.fromCharCode(c);
}


var UNESCAPE_MD_RE = /\\([!"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~])/g;
var ENTITY_RE = /&([a-z#][a-z0-9]{1,31});/gi;
var UNESCAPE_ALL_RE = new RegExp(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source, 'gi');
const UNESCAPE_MD_RE = /\\([!"#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~])/g;
const ENTITY_RE = /&([a-z#][a-z0-9]{1,31});/gi;
const UNESCAPE_ALL_RE = new RegExp(UNESCAPE_MD_RE.source + '|' + ENTITY_RE.source, 'gi');

var DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i;
const DIGITAL_ENTITY_TEST_RE = /^#((?:x[a-f0-9]{1,8}|[0-9]{1,8}))$/i;


function replaceEntityPattern(match, name) {
var decoded, code;

if (name.charCodeAt(0) === 0x23/* # */ && DIGITAL_ENTITY_TEST_RE.test(name)) {
code = name[1].toLowerCase() === 'x' ?
const code = name[1].toLowerCase() === 'x' ?
parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);

if (isValidEntityCode(code)) {
Expand All @@ -95,7 +93,7 @@ function replaceEntityPattern(match, name) {
return match;
}

decoded = decodeHTML(match);
const decoded = decodeHTML(match);
if (decoded !== match) {
return decoded;
}
Expand Down Expand Up @@ -125,9 +123,9 @@ function unescapeAll(str) {

////////////////////////////////////////////////////////////////////////////////

var HTML_ESCAPE_TEST_RE = /[&<>"]/;
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
var HTML_REPLACEMENTS = {
const HTML_ESCAPE_TEST_RE = /[&<>"]/;
const HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
const HTML_REPLACEMENTS = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
Expand All @@ -147,7 +145,7 @@ function escapeHtml(str) {

////////////////////////////////////////////////////////////////////////////////

var REGEXP_ESCAPE_RE = /[.?*+^$[\]\\(){}|-]/g;
const REGEXP_ESCAPE_RE = /[.?*+^$[\]\\(){}|-]/g;

function escapeRE(str) {
return str.replace(REGEXP_ESCAPE_RE, '\\$&');
Expand Down
19 changes: 10 additions & 9 deletions lib/helpers/parse_link_destination.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import { unescapeAll } from '../common/utils.mjs';

export default function parseLinkDestination(str, start, max) {
var code, level,
pos = start,
result = {
ok: false,
pos: 0,
lines: 0,
str: ''
};
let code;
let pos = start;

const result = {
ok: false,
pos: 0,
lines: 0,
str: ''
};

if (str.charCodeAt(pos) === 0x3C /* < */) {
pos++;
Expand Down Expand Up @@ -39,7 +40,7 @@ export default function parseLinkDestination(str, start, max) {

// this should be ... } else { ... branch

level = 0;
let level = 0;
while (pos < max) {
code = str.charCodeAt(pos);

Expand Down
10 changes: 6 additions & 4 deletions lib/helpers/parse_link_label.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
//

export default function parseLinkLabel(state, start, disableNested) {
var level, found, marker, prevPos,
labelEnd = -1,
max = state.posMax,
oldPos = state.pos;
let level, found, marker, prevPos;

const max = state.posMax;
const oldPos = state.pos;

state.pos = start + 1;
level = 1;
Expand Down Expand Up @@ -36,6 +36,8 @@ export default function parseLinkLabel(state, start, disableNested) {
}
}

let labelEnd = -1;

if (found) {
labelEnd = state.pos;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/helpers/parse_link_title.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { unescapeAll } from '../common/utils.mjs';


export default function parseLinkTitle(str, start, max) {
var code,
marker,
lines = 0,
pos = start,
result = {
ok: false,
pos: 0,
lines: 0,
str: ''
};
let code, marker;
let lines = 0;
let pos = start;

const result = {
ok: false,
pos: 0,
lines: 0,
str: ''
};

if (pos >= max) { return result; }

Expand Down

0 comments on commit 88b8669

Please sign in to comment.